1

How do I keep an RMI server running? It currently, just binds and object, then exits..

public class WutServer {
  public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);
    try {
      registry.bind("WutManager", new WutManager());
      System.out.println("Ready..");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

I am simply running this class. I didn't run rmic or anything..

How do I force it to stay running?

Verhogen
  • 27,221
  • 34
  • 90
  • 109

5 Answers5

2

Try this:

Remote stub = UnicastRemoteObject.exportObject(new WutManager(), 0);
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("WutManager", stub);

Note: WutManager should implement java.rmi.Remote.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60
  • I think it works. It doesn't stop anymore, but is there any reason why the registry needs to be created after exporting the object? – Verhogen Mar 02 '11 at 09:29
  • @verhogen: No particular reason; they're formally independent. Only dependency is that both registry and server object need to exist before you can register one with the other. :-) – Donal Fellows Mar 02 '11 at 09:42
  • Worked also for me! Thanks dude! – bpawlowski Jun 17 '13 at 13:09
  • @Verhogen If this worked, it can only mean that you didn't have an RMI server in the first place, that is to say an exported remote object. – user207421 Mar 03 '17 at 01:12
1

Your server is being DGC'd and then GC'd, which causes it to be unexported, which eventually causes the JVM to exit if it has nothing else to do. To stop that, if you are creating the Registry via LocateRegistry.createRegistry(), keep the return value of that method in a static variable. Otherwise keep a static reference to your server object.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

This is an old question, but here is a new answer.

On OSX, using latest Java 9.0.4 I find that the program exits. If I use latest Java 1.8.0.162 then it does not exit and the server remains running.

skaak
  • 2,988
  • 1
  • 8
  • 16
-1

You need to make WutServer implement the interface that clients will access it by, which in turn should inherit from the marker interface Remote. You also probably want to make the WutServer class inherit from UnicastRemoteObject; while there are other ways to build the remoting support, inheriting from UnicastRemoteObject is definitely the easiest way to get something going.

Try this instead (though you should probably separate the remote interface into another file and have it be redistributed separately):

public class WutServer extends UnicastRemoteObject implements WutServer.Wut {
    interface Wut extends Remote {
        String wut() throws RemoteException;
    }
    // Because of the exception...
    public WutServer() throws RemoteException {}
    public String wut() { return "wut"; }
    public static void main(String[] args) throws RemoteException {
        LocateRegistry.createRegistry(1099).rebind("WutManager",new WutServer());
        System.out.println("Ready...");
    }
}
Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • And the server in the question stops because it didn't actually register anything with the registry; there's no remote reference so there's nothing for any remaining non-daemon thread to do and so the process exits as per Java Language Spec. – Donal Fellows Mar 02 '11 at 09:44
  • This is RMI 101 and he must already have it all working to have got this far. It has nothing to do with the server exiting prematurely. – user207421 Mar 22 '11 at 05:44
-1

Create an object and call wait of the object at the end of the main function. That is;

public static void main(String[] args) throws RemoteException {
    Registry registry = LocateRegistry.createRegistry(1099);

    //your object to wait
    Object lockObject=new Object();

    try {            
              registry.bind("WutManager", new WutManager());            
              System.out.println("Ready..");

              //here makes your rmi server non-stop
              synchronized(lockObject){
                    lockObject.wait();
              }
     }catch (Exception e) {            
              e.printStackTrace();          
     } 
}
mdikici
  • 1,374
  • 2
  • 13
  • 20