Search code examples
rmi

Closing RMI remote object


I have a RMI class which binds a service. When I want to shutdown I unbind the service but the program doesn't exit, even when a client doesn't connect. Is there something else I should be doing to release resources?

public void run() {
    try {
...
        GraphDataInterface gs = new GraphServer(config, dob, "file:./server.policy", "GraphServer");
        gs.close();
    } catch (RemoteException e) {
        System.err.println("GraphServer exception:" + e.toString());
    } catch (Exception e) {
        System.err.println("GraphServer exception:" + e.toString());
    }
}

Here is the code called by the constructor and close();

private void bindService() throws RemoteException {
    BaseRMIInterface stub = (BaseRMIInterface) UnicastRemoteObject.exportObject(this, 0);
    Registry registry = LocateRegistry.getRegistry();
    registry.rebind(name, stub);
    System.out.println(name + " bound");
}

private void unbindService() throws RemoteException, NotBoundException {
    Registry registry = LocateRegistry.getRegistry();
    registry.unbind(name);
    System.out.println(name + " unbound");
}

The output of the code is,

GraphServer bound

GraphServer unbound

but the program doesn't exit.


Solution

  • You've unbound the reference from the registry, but you also need to unexport the object itself with

    UnicastRemoteObject.unexportObject(this, true);