Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMI Server won't thread and dies with LocateRegistry.createRegistry method

Tags:

java

rmi

I'm now using LocateRegistry.createRegistry(1099) rathern than using the registry in a external process. However the registry dies after the main program ends. For instance, if I make a simple program that creates the registry it'll not work because after the main executino the code ends. I was expecting the LocateRegistry code to create a thread, but it seems that this is not the case. Is this the normal behavior of using LocateRegistry or I'm missing something?

Code sample:

// ommited imports

public class RMITest {
    public static void main(String[] args) {
        LocateRegistry.createRegistry(1099);
        // JVM will exit now!!!
    }
}

The RMI Server start and suddenly dies. How

like image 274
Marcos Roriz Junior Avatar asked Nov 07 '11 21:11

Marcos Roriz Junior


People also ask

What is object serialization in RMI and RMI registry?

RMI has object communication facilities that are analogous to CORBA's IIOP, and its object serialization system provides a way for you to transfer or request an object instance by value from one remote process to another.

How do I run RMI registry?

To start the registry on the server, execute the rmiregistry command. This command produces no output and is typically run in the background. For this example, the registry is started on the host mycomputer . By default, the registry runs on port 1099.

Which one acts as a gateway for the client side of RMI?

Stub − A stub is a representation (proxy) of the remote object at client. It resides in the client system; it acts as a gateway for the client program. Skeleton − This is the object which resides on the server side. stub communicates with this skeleton to pass request to the remote object.


2 Answers

I was expecting the LocateRegistry code to create a thread

It's not that simple.

  1. Exporting the first object on a new port creates a thread that listens on that port, and unexporting the last object listening on a port causes that thread to exit. This is for all remote objects, not just local Registry objects.

  2. Unexporting can happen automatically via local GC, which in turn can be trigged by remote DGC.

Your JVM exits because you aren't saving the value returned by LocateRegistry.createRegistry() in a static variable, so it gets GC'd, so the object gets unexported, so there are no remote objects exported on port 1099, so the thread that listens on 1099 exits, so there are no non-daemon threads, so the JVM exits.

Solution: store the result of LocateRegistry.createRegistry() in a static variable. You can use that to unexport the Registry when you want your JVM to exit.

like image 98
user207421 Avatar answered Oct 06 '22 01:10

user207421


There are two possible ways to start the RMI registry.

  1. LocateRegistry.createRegistry(1099); The java application executing the registry must not finish. In your case you could start a new "never ending" thread (see below for source code)
  2. rmiregistry This is a tool included in the java distribution that starts an RMI registry service. see rmiregistry - The Java Remote Object Registry

Sample code for RMI registry server.

import java.io.IOException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class RmiTest {

    public static void main(String[] args) throws IOException {
        final Object monitor = new Object();

        new Thread(new Runnable() {
            public void run() {
                try {
                    LocateRegistry.createRegistry(1099);
                    synchronized (monitor) {
                        monitor.wait();                        
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("RMI Registry Thread finished.");
            }
        }, "RMI Registry Thread").start();
        System.out.println("Press enter to exit...");
        System.in.read();
        synchronized (monitor) {
            monitor.notify();            
        }
    }
}
like image 45
andy Avatar answered Oct 05 '22 23:10

andy