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
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.
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.
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.
I was expecting the LocateRegistry code to create a thread
It's not that simple.
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.
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.
There are two possible ways to start the RMI registry.
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)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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With