top button
Flag Notify
Site Registration

why does the Java RMI implementation not reuse server side ports when I use a custom server socket factory?

+4 votes
438 views

In other words,Why does Java RMI implementation create so many sockets when my application uses custom socket factories ?

posted Sep 27, 2013 by Manish Negi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+3 votes

The Java RMI implementation attempts to reuse open sockets where possible for remote invocations. When a remote method is invoked on a stub that uses a custom socket factory, the Java RMI
implementation 'll reuse an open connection if any as long as that socket was created by an equivalent socket factory. Since client socket factories are serialized to clients a single client may have several distinct copies of the same logical socket factory. To ensure that the Java RMI implementation will reuse sockets created by custom socket factories, make sure your custom client socket factory classes implement the hashCode and equals methods appropriately. If the client socket factory does not implement these methods correctly, another ramification is that stubs (using the client socket factory) that refer to the same remote object will not be equal.
The Java RMI implementation attempts to reuse server-side ports as well. It will only do so if there is an existing server socket for the port created by an equivalent socket factory. Make sure the server socket factory class implements the hashCode and equals methods too.

If your socket factory has no instance state a trivial implementation of the hashCode and equals methods are the following:-

public int hashCode() { return 57; }
public boolean equals(Object o) { return this.getClass() == o.getClass() }
answer Sep 28, 2013 by Arvind Singh
...