Java Reference
In-Depth Information
1. Create the interface.
2. Defi ne a class that implements this interface.
3. Create the server process.
4. Create the client process.
Simple Example
This fi rst example application simply displays a greeting to any client that uses the
appropriate interface registered with the naming service to invoke the associated
method implementation on the server. In a realistic application, there would almost
certainly be more methods and those methods would belong to some class (as will
be shown in a later example). However, we shall adopt a minimalistic approach until
the basic method has been covered. The required steps will be numbered as aboveā€¦
1. Create the interface.
This interface should import package java.rmi and must extend interface Remote ,
which (like Serializable ) is a 'tagging' interface that contains no methods. The inter-
face defi nition for this example must specify the signature for method getGreeting ,
which is to be made available to clients. This method must declare that it throws a
RemoteException . The contents of this fi le are shown below.
import java.rmi.*;
public interface Hello extends Remote
{
public String getGreeting() throws RemoteException;
}
2. Defi ne a class that implements this interface.
The implementation fi le should import packages java.rmi and java.rmi.server . The
implementation class must extend class RemoteObject or one of RemoteObject 's
subclasses. In practice, most implementations extend subclass UnicastRemoteObject ,
since this class supports point-to-point communication using TCP streams. The
implementation class must also implement our interface Hello , of course, by pro-
viding an executable body for the single interface method getGreeting . In addition,
we must provide a constructor for our implementation object (even if we simply
give this constructor an empty body, as below). Like the method(s) declared in the
interface, this constructor must declare that it throws a RemoteException . Finally,
we shall adopt the common convention of appending Impl onto the name of our
interface to form the name of the implementation class.
import java.rmi.*;
import java.rmi.server.*;
public class HelloImpl extends UnicastRemoteObject
implements Hello
{
public HelloImpl() throws RemoteException
{
//No action needed here.
Search WWH ::




Custom Search