Search code examples
javarmi

Exporting remote data and Socket Reuse in RMI


I am new to RMI so please forgive me if my question is silly. I am trying to export a data in concurrent manner. How can I reuse the same port for exporting more than one remote object at once. Can anyone guide me to achieve this with an example?

I tried to do this so first object got exported but got following exception at the time of second export.

java.rmi.server.ExportException: Port already in use: 55580; nested exception is: java.net.BindException: Address already in use: JVM_Bind

I used following code snippet

UnicastRemoteObject.exportObject(remoteObj, 55580);

If I skipped the port number than any port number will be picked by RMI. Like as below

UnicastRemoteObject.exportObject(remoteObj)

In my case port number is fixed for my application.


Solution

  • How can I reuse the same port for exporting more than one remote object at once

    That's what happens by default. All remote objects exported from the same JVM that

    • don't specify a port number, or specify port zero, and
    • don't specify server or client socket factories, or specify the same socket factories as determined by equals()

    will be exported on the same port.

    I conclude that you must be exporting your remote objects from two JVMs. THere's no real need to do that. Put them all into one. If you start the Registry from in there as well, with LocateRegistry.createRegistry(), you can use the Registry port 1099 for everything, and it's already reserved at IANA, so no arguments about the number.