The java.rmi Package

The java.rmi package contains the classes that are seen by clients (objects that invoke remote methods). Both clients and servers should import java.rmi. While servers need a lot more infrastructure than what’s present in this package, java.rmi is all that’s needed by clients. This package contains one interface, three classes, and a handful of exceptions.

The Remote Interface

The Remote interface tags objects as being remote objects. It doesn’t declare any methods; remote objects usually implement a subclass of Remote that does declare some methods. The methods that are declared in the interface are the methods that can be invoked remotely.

Example 18.9 is a database interface that declares a single method, SQLQuery( ) , which accepts a String and returns a String array. A class that implements this interface would include the code to send an SQL query to a database and return the result as a String array.

Example 18-9. A Database Interface

import java.rmi.*;

public interface SQL extends Remote {

  public String[] SQLQuery(String query) throws RemoteException;

}

An SQLImpl class that implemented the SQL interface would probably have more methods, some of which might be public. However, only the SQLQuery( ) method can be invoked by a client. Because the Remote interface is not a class, a single object can implement multiple Remote subinterfaces. In this case, any method declared in any Remote interface can be invoked by a client.

The Naming Class

The java.rmi.Naming ...

Get Java Network Programming, Second Edition 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.