[TOC] [Prev] [Next]

Java Distributed Object Model


Topics:

Definition of Terms

In the Java distributed object model, a remote object is one whose methods can be invoked from another Java Virtual Machine, potentially on a different host. An object of this type is described by one or more remote interfaces, which are Java interfaces that declare the methods of the remote object.

Remote method invocation (RMI) is the action of invoking a method of a remote interface on a remote object. Most importantly, a method invocation on a remote object has the same syntax as a method invocation on a local object.

The Distributed and Non-Distributed Models Contrasted

The Java distributed object model is similar to the Java object model in the following ways:

The Java distributed object model differs from the Java object model in these ways:

RMI Interfaces and Classes

The interfaces and classes that are responsible for specifying the remote behavior of the RMI system are defined in the java.rmi and the java.rmi.server packages. The following figure shows the relationship between these interfaces and classes:

The Remote Interface

All remote interfaces extend, either directly or indirectly, the Remote interface. The Remote interface defines no methods, as shown here:

	interface Remote {}
For example, the following code fragment defines a remote interface for a bank account that contains methods that deposit to the account, get the account balance, and withdraw from the account:

public interface BankAccount
       extends Remote
{
	public void deposit (float amount)
		throws RemoteException;
	public void withdraw (float amount)
		throws OverdrawnException, RemoteException;
	public float balance()
		throws RemoteException;
}

The RemoteException Class

The RemoteException class is the superclass of all exceptions that can be thrown by the RMI runtime. To ensure the robustness of the RMI system, each method declared in a remote interface must specify RemoteException in its throws clause.

RemoteException is thrown when a remote method invocation fails (for example when the network fails or the server for the call cannot be found). This allows the application making the remote invocation to determine how best to deal with the remote exception.

The RemoteObject Class and its Subclasses

The RMI server functions are provided by RemoteObject and its subclasses, RemoteServer and UnicastRemoteServer:

Implementing a Remote Interface

The general rules for a class that implements a remote interface are as follows:

For example, the following code fragment defines the BankAcctImpl class, which implements the BankAccount remote interface and which extends the UnicastRemoteServer class:

package my_package;

import java.rmi.*;
import java.rmi.server.UnicastRemoteServer;

public class BankAccountImpl
	extends UnicastRemoteServer
	implements BankAccount
{
	public void deposit (float amount) throws RemoteException {
		...
	}
	public void withdraw (float amount) throws OverdrawnException,
		RemoteException {
		...
	}
	public float balance() throws RemoteException {
		...
	}
}
Note that if necessary, a class which implements a remote interface can extend some other class besides UnicastRemoteServer. However, the implementation class must then assume the responsibility for the correct remote semantics of the hashCode, equals, and toString methods inherited from the Object class.

Type Equivalency of Remote Objects with Local Stub

In the distributed object model, clients interact with stub (surrogate) objects that have exactly the same set of remote interfaces defined by the remote object's class; the stub class does not include the non-remote portions of the class hierarchy that constitutes the object's type graph. This is because the stub class is generated from the most refined implementation class that implements one or more remote interfaces. For example, if C extends B and B extends A, but only B implements a remote interface, then a stub is generated from B, not C.

Because the stub implements the same set of remote interfaces as the remote object's class, the stub has, from the point of view of the Java system, the same type as the remote portions of the server object's type graph. A client, therefore, can make use of the built-in Java operations to check a remote object's type and to cast from one remote interface to another.

Parameter Passing in Remote Method Invocation

An argument to or a return value from a remote object can be any Java type. This includes Java primitive types, remote Java objects, and non-remote Java objects. If the class of an argument or return value is not available locally, it is loaded dynamically.

Some classes may disallow their being passed, perhaps for security reasons. In this case the remote method invocation will fail with an exception.

Passing non-remote objects

Non-remote objects contained in a parameter of a remote method invocation or returned as a result of a remote method invocation are passed by copy.

That is, when a non-remote object appears in a remote method invocation, the content of the non-remote object is copied before invoking the call on the remote object. By default, only the non-static and non-transient fields are copied.

Similarly, when a non-remote object is returned from a remote method invocation, a new object is created in the calling virtual machine.

Passing remote objects

When passing a remote object as a parameter, the stub for the remote object is passed. This means that on receipt, only the remote interfaces are available, not any local interfaces that the remote object also happens to implement. Attempting to pass local interfaces to a remote method will be accepted by the compiler, but will result in a runtime exception being generated.

Exception Handling in Remote Method Invocation

Since remote methods include RemoteException in their signature, the caller must be prepared to handle those exceptions in addition to other application specific exceptions. When a RemoteException is thrown during a remote method invocation, the client may have little or no information on the outcome of the call-whether a failure happened before, during, or after the call completed. Therefore, remote interfaces and the calling methods declared in those interfaces should be designed with these failure semantics in mind.

Object Methods Overridden by the RemoteObject Class

The default implementations in the Object class for the equals, hashCode, toString, clone, and finalize methods are not appropriate for remote objects. Therefore, the RemoteObject class provides implementations for these methods that have semantics more appropriate for remote objects. In this way, all objects that need to be available remotely can extend RemoteObject (typically indirectly via UnicastRemoteServer).

equals and hashCode

In order for a remote object to be used as a key in a hash table, the methods equals and hashCode are overridden by the RemoteObject class:

toString

The toString method is defined to return a string which represents the reference of the object. The contents of the string is specific to the reference type. The current implementation for non-replicated objects includes transport specific information about the object (e.g., host name and port number) and an object identifier; references to replicated objects would contain more information

clone

Objects are only cloneable using the Java language's default mechanism if they support the java.lang.Cloneable interface. Remote objects do not implement this interface, but do implement the clone method so that if subclasses need to implement Cloneable, the remote classes will function correctly.

Client stubs are declared final and do not implement clone. Cloning a stub is therefore a local operation and cannot be used by clients to create a new remote object.

For RemoteServer objects, clone is implemented to make a new remote object distinct from the original. Cloning a remote object is only available in the server process where the remote object exists. If a remote object does not extend RemoteServer, it must implement its own version of clone and be able to export a cloned object.

finalize

The finalize method is used as necessary by the implementation. Remote object implementations may use finalize to perform their own cleanup as necessary.

The Semantics of Object Methods Declared final

The following methods are declared final by the Object class and cannot be overridden:

The default implementation for getClass is appropriate for all Java objects, local or remote; the method needs no special implementation for remote objects. When used on a remote object, the getClass method reports the exact type of the generated stub object. Note that this type reflects only the remote interfaces implemented by the object, not its local interfaces.

The wait and notify methods of Object deal with waiting and notification in the context of the Java language's threading model. While use of these methods for remote objects does not break the Java threading model, these methods do not have the same semantics as they do for local Java objects. Specifically, using these methods operates on the client's local reference to the remote object, not the actual object at the remote site.

Locating Remote Objects

A simple bootstrap name server is provided for storing named references to remote objects. A remote object reference can be stored using the URL-based interface Naming.

For a client to invoke a method on a remote object, that client must first obtain a reference to the object. A reference to a remote object is usually obtained as a return value in a method call. The RMI system provides a simple bootstrap name server from which to obtain remote objects on given hosts. The java.rmi.Naming interface provides Uniform Resource Locator (URL) based methods to lookup, bind, rebind, unbind, and list the name and object pairings maintained on a particular host and port.

Here's an example, (without exception handling) of how to bind and lookup remote objects:

BankAccount acct = new BankAcctImpl();
String url = "rmi://java.Sun.COM/account";
// bind url to remote object
java.rmi.Naming.bind(url, acct);
	...
// lookup account
acct = (BankAccount)java.rmi.Naming.lookup(url);


[TOC] [Prev] [Next]

rmi-comments@jse.East.Sun.COM
Copyright © 1996 Sun Microsystems, Inc. All rights reserved.