Search code examples
javaeclipsermi

RMI server terminates after a while


I run my test RMI server from Eclipse. Currently, it just consists of the implementation of my RMI interface (IRDSA) with a very few methods and the server itself implemented in this main: (Using a private port number)

    static Registry registry;
    public static void main(String[] args) {
        // start Registry
        try {
            registry = LocateRegistry.createRegistry(RMI_PORT);
        } catch (ExportException exp) {
            System.out.format ("RMI Registry (Port %d) already active. Providing", RMI_PORT);
            try {
                Registry    existingReg = LocateRegistry.getRegistry(RMI_PORT);
                String[] bound = existingReg.list();
                for (String entry : bound)
                    System.out.println (entry);
            } catch (RemoteException e) {
                e.printStackTrace();
            } 
            return;
        } catch (RemoteException e) {
            e.printStackTrace();
            return;
        }

        try {
            IRDSA server = new RDSAServer();
            IRDSA stub = (IRDSA) UnicastRemoteObject.exportObject(server, 0);

            // Bind the remote object's stub in the registry
            registry.bind(RMI_NAME, stub);

            System.out.println("RDSAServer ready");
        } catch (Exception e) {
            System.err.println("RDSAServer exception: " + e.toString());
            e.printStackTrace();
        }
    }
}

For a test, I start a local client and it executes the methods well. This can be repeated and the server remains alive, no matter that main has finished already. But after a while, even if I keep a client connected (suspended in a break), when I return from lunch the client is still suspended but the server is terminated silently.

When des it happen exactly, and how can I avoid it?

Found a similar question, where the Registry should be static (it is, here)

Do I have to keep main alive?

Anything else?


Solution

  • Your Server-Application only consists of the main-method where you start and bind your RMI-Server. The RMI-Server will only live as long there are stub-instances where the client's calls go on. If they are garbage-collected, the server will be shut down by the JVM.

    For easy purposes, you can use a loop in your main-method so the Main-Thread will not end and also your RMI-Server will be kept alive.