在单台计算机和单个IDE上运行两个RMI应用程序

时间:2016-02-16 00:34:24

标签: java rmi

我一直在开发一个通过RMI相互通信的两个应用程序,一个将数据放在其本地队列上,另一个用于远程轮询队列中的数据 - 单向通信生产者 - 消费者方法。有一些更新,现在我需要进行双向通信,这需要使用两个RMI轮询。显然我不能在一台机器上运行两个start rmiregistry。我该怎么办?这有什么解决方法吗?我需要在Eclipse中的两个不同项目上并行运行这两个应用程序!

两个应用程序的RMI代码如下所示。所以基本上当我运行第一个,然后是第二个时,它会抛出以下异常。

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();
        }

例外:

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)

1 个答案:

答案 0 :(得分:1)

您不需要两个RMI注册表。一个就足够了。