简体   繁体   中英

Java RMI without RMI Registry

I am rather new to java RMI and am trying to create a Peer 2 Peer bit torrent like application wherein multiple instances of the same peer may be on the same machine. This would mean that I would need to be able to have more than one remote object of the same type registered on the same machine. The RMI registry seems to only allow me to have one implementation of a Remote Object on any machine as the registry would not be able to differentiate between which of the objects it should be returning a reference to. Is there a way to bypass the registry such as by specifying an IP and port where I know the other peer is exposing its remote object? If not do you have any ideas how I would be able to create multiple instances of the same object on the same machine? Any help with this would be greatly appreciated...

You can either start multiple rmi registries on different ports, or better bind the instances of the object under different names multiple times. But the best way is probably to do the logic in your code and return a new remote object every time it is needed. Eg dependent on a parameter:

public MyRemoteObject connect(String name) throws java.rmi.RemoteException {
    if("first".equals(name)){
         return firstinstance;
    }else if("new".equals(name)){
         return new MyRemoteObject();
    }
    ...
}

or something like this...

I would suggest you to forget about RMI - IMHO this technique is not applicable for your use case.

Define yourself a network protocol including a serialization and deserialization logic and use this for sending and receiving data on a raw socket connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM