Search code examples
javainterfacejvmrmi

RMI: Must a parameter to a remote method implement Serializable if there is only one JVM?


For Java RMI, must an object passed as a parameter to a Remote object implement Serializable even if both the client and server are running in the same JVM?


Solution

  • Essentially yes. Even if the client and server are in the same JVM, making a remote call through a stub still marshals the arguments, sends the request using the RMI protocol (JRMP), unmarshals them on the server side, and vice-versa for the return value. There is no short-circuiting if the client and server happen to be on the same machine.

    In a sense this is good in that it helps validate your design. If there were short-circuiting, you might run into serializability issues or accidental object sharing when you moved the client to a different JVM.

    You can short-circuit RMI calls yourself, if you have the client make "remote" calls directly on the server implementation object instead of the stub. These calls of course are regular method calls. To avoid this problem, you have to make sure that only stubs are available to the client, not the actual implementation objects.