Defining the RMI Contract

Problem

You want to define the communications exchange between client and server.

Solution

Define a Java interface.

Discussion

RMI procedures are defined using an existing Java mechanism: interfaces. An interface is similar to an abstract class, but a class can implement more than one interface. RMI remote interfaces must be subclassed from java.rmi.Remote,[53] and both the client and server must be in the same Java package. All parameters and return values must be either primitives (int, double, etc.), or implement Serializable (as do most of the standard types like String). Or, as we’ll see in Section 22.6, they can also be Remote.

Figure 22-1 shows the relationships between the important classes involved in an RMI implementation. The developer need only write the interface and two classes, the client application and the server object implementation. The RMI stub or proxy and the RMI skeleton or adapter are generated for you by the rmic program (see Section 22.4), while the RMI Registry and other RMI classes at the bottom of the figure are provided as part of RMI itself.

Example 22-1 is a simple RemoteDate getter interface, which lets us find out the date and time on a remote machine.

Example 22-1. RemoteDate.java

package darwinsys.distdate; import java.rmi.*; import java.util.Date; /** A statement of what the client & server must agree upon. */ public interface RemoteDate extends java.rmi.Remote { /** The method used to get the current date on the remote ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.