问题
I have a test program T which:
- Acquires a stub for a
Remote
object O from an RMI registry on server S - In hundreds of parallel threads, invokes methods on this object O.
I can see that server S has many "RMI TCP Connection" threads. I had expected there to be only one, since there is only one stub of O on T. How does this work?
回答1:
RMI needs a connection per end-point per thread. It pools them at the client end, which in turn causes pooling at the server end as well, so it isn't actually as bad as that, but if you have 1000 threads performing RMI calls at the same instant there will certainly be 1000 connections. They are not multiplexed.
回答2:
RMI is supposed to try-to reuse server sockets and client sockets if possible.
Your case happens when the server socket factories are not equivalent (when equals is performed, the result is false and the connection is established again). Perhaps the stub haven't implemented hashCode and equals, so there's no certain way to know if the socket is available to be used.
来源:https://stackoverflow.com/questions/11416710/when-does-rmi-make-tcp-connections