angel_30
angel_30

Reputation: 1

Run two RMI application on a single machine and single IDE

I've been developing a two applications that communicate with eachother through RMI, with one putting data on its local queue and the other one polling the queue for data remotely- Kind of a one-way communication producer-consumer approach. There has been some updates and now I need to make the communication two-way, and that requires using two RMI polling. Apparently I can not run two start rmiregistry on a single machine. What shall I do? Is there any workaround for this? I need to run the two applications in parallel on a two different project in Eclipse!

The RMI code for both applications are the same as below. So basically when I run the first one, and then the second one, it throw an exception in the following.

try {
            ServerRMI obj = new ServerRMI();
            Hello stub = (Hello) UnicastRemoteObject.exportObject(obj, 0);

            // Bind the remote object's stub in the registry
            Registry registry = LocateRegistry.getRegistry();
            registry.bind("Hello", stub);
            System.err.println("Server ready");
        } catch (Exception e) {
            System.err.println("Server exception: " + e.toString());
            e.printStackTrace();
        }

And the exception:

Server exception: java.rmi.AlreadyBoundException: Hello
java.rmi.AlreadyBoundException: Hello
    at sun.rmi.registry.RegistryImpl.bind(RegistryImpl.java:183)
    at sun.rmi.registry.RegistryImpl_Skel.dispatch(Unknown Source)
    at sun.rmi.server.UnicastServerRef.oldDispatch(UnicastServerRef.java:410)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:268)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$240(TCPTransport.java:683)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler$$Lambda$1/305254903.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Unknown Source)
    at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
    at sun.rmi.server.UnicastRef.invoke(Unknown Source)
    at sun.rmi.registry.RegistryImpl_Stub.bind(Unknown Source)
    at test.ServerRMI.main(ServerRMI.java:26)

Upvotes: 0

Views: 932

Answers (1)

user207421
user207421

Reputation: 311052

You don't need two RMI Registries. One is sufficient.

Upvotes: 1

Related Questions