简体   繁体   中英

RMI Callbacks using a single port

How can I reduce the number of overall ports used by RMI from N (where N = number of RMI UnicastRemoteObjects) to 2 + N?

Allow me to explain further. I have a system which is using RMI to perform client-server communication. There are two interfaces; we shall call them Client and Server to keep things simple. The RMI registry runs in the same JVM as Server, and is bound to some port (say 4000). Server is exported as a UnicastRemoteObject with no port number, and is bound to an anonymous port as per the no-args constructor.

When a Client starts up, it calls a void hello(Client client) method on the Server implementation which it gets from a lookup on the registry. The Client implementation passed in is also a UnicastRemoteObject; giving us bi-directional communication between Client and Server over the two interfaces. The problem is that the UnicastRemoteObject in the Client is also listening on its own anonymous port.

That means that in order to have N clients, I need to have 1 + 1 + 3 * N ports availble; 2 + N of which are listening, and 2 * N of which are ephemeral. (Registry + Server Instance + One listening port per client, Ephemeral port per client, Ephemeral port per server). We usually have around 30 clients, giving 92 ports. I believe I can get this down to 32 ports (1 + 1 + the ephemeral port for each client), but I can't see how to do it with pure RMI.

I am willing to accept other solutions which do not require RMI, but also do not require that we re-write our interfaces.

I'm not clear what the problem is here. Exported RMI objects will all share the same port number by default, unless you have written non-compliant server socket factories. You can specify the port number to be used when constructing or exporting them as described in the Javadoc for UnicastRemoteObject . You can use the same port for your objects and the Registry when starting the latter in your JVM. Your 92 ports don't make much sense as a resource count even if accurate, as they are distributed across all the server and client hosts.

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