Java Reference
In-Depth Information
JVMs. There are three classes of parameters and return values to consider: primitive types,
Serializable objects, and remote objects.
Note Arrays of primitives and arrays of Serializable objects can also be safely passed as a remote
method parameter.
Primitive Types
RMI makes a copy of the primitive type for both method arguments and return values. When a
copy of the primitive is sent across the wire, we say that the parameter or return value is pass-
by-value .
Serializable Objects
When an object is marshaled across the wire as a method parameter or a return value, RMI
makes a copy of the object and all of the objects it references as one large object graph. Object
parameters are sent across the wire as pass-by-value, just like primitive types.
The object being passed must be Serializable or a NotSerializableException will be
thrown. In addition, all of the class members of the object being marshaled must also be
Serializable , unless the member is declared as a transient member or the Externalizable
interface is implemented instead of Serializable .
Remote Objects
This is the most complex scenario of the three. When the item being returned is a remote
object (i.e., one that implements java.rmi.Remote ), the proxy for that object, or the stub, is
returned in place of the Real-Object . In this way, remote objects are pass-by-reference and
the stub is marshaled back and forth.
Remote objects, by default, implement Serializable . Table 6-3 summarizes how RMI
handles various parameters and return values. RMI uses one of two mechanisms to obtain
remote object references. The RMI registry is one way, and another is simply passing the
proxy or stub, which was discussed previously. The RMI registry uses java.lang.Naming to
store references to remote objects. java.rmi.Naming provides URL-based methods to associate
name-object pairs located on a particular host and port. RMI can also load bytecodes with a
valid URL naming protocol such as FTP or HTTP. When a client interacts with a remote object,
it also interacts with the remote object interface, not with the actual remote implementation.
Table 6-3. RMI Parameter Passing
Object/Type
Parameter/Return Value
Remote objects
Pass-by-reference: A stub is passed instead of a copy.
Nonremote/serialized objects
Pass-by-value: An actual copy of an object is passed as a
serialized object.
Primitive types
Pass-by-value: A copy is made.
Search WWH ::




Custom Search