Previous lesson 3/6 |home| Next lesson 5/6

Remote Method Invocation (Level I)

Design an RMI Server

When you have a remote object, you need a carrier to hold it for a possible remote invocation. That carrier is often called a server.

What is an RMI server?

A typical RMI server is an application that creates a number of remote objects, makes references to those remote objects and waits for clients to invoke methods on those remote objects.

In this context, a server is a class. In this class, we need a main method that creates an instance of the remote object, exports the remote object, and then binds that instance to a name in a Java RMI registry. So we list the server jobs as follows:

  1. Create an instance of the remote object
  2. Export the remote object
  3. Bind the instance to a name in RMI registry

How to create an instance of the remote object

We already create a remote object class -- PaymentImpl. To instantiate it, just do it like any other class instantiation.


PaymentImpl robj = new PaymentImpl();

Here robj is an instance of the remote object PaymentImpl.

How to export the remote object?

We will use this method,

java.rmi.server.UnicastRemoteObject.exportObject(Remote, int); 
to export the remote object. When you export a remote object, you make that object available to accept incoming calls from clients. The exportObject() method takes 2 parameters, one is an instance of the remote object, the other is TCP port number. Note that the same port number can accept incoming calls for more than one remote objects. The default number for RMI is 1099. If you pass a zero to the method, the default number 1099 is used. Of course, you can use a different number to listen to different remote client calls.

 
Payment stub = (Payment) UnicastRemoteObject.exportObject(robj, 0);

Note that the exportObject() method will return a stub which is a term used to describe a proxy class. The stub class is a key to make remote object available for remote invocation. We need to cast it to the remote interface for safe. The second parameter we use "0" that means we are going to use the default TCP port number, which is 1099.

An alternative way to export remote object

You can make the server class extend java.rmi.server.UnicastRemoteObject class and use a constructor to export the object and define a port number. For example,

public class Server extends java.rmi.server.UnicastRemoteObject implements aRemoteInterface{
    public Server(int port) { 
         super(port);
    }
    ....
    Naming.bind(uniqueName, this);
    ....
}

In this case, you combine the server with remote interface implementation together. Such design is often used in a simple RMI application.

What is Java RMI registry

The Java RMI registry is a remote object that maps names to remote objects.

The methods of LocateRegistry are used to get a registry operating on a particular host or host and port. The method bind() or rebind() binds a unique name to the reference of the remote object.

java.rmi.registry.Registry registry = java.rmi.registry.LocateRegistry.getRegistry();
registry.bind("Mortgage", stub);

or use:
registry.rebind("Mortgage", stub);

Note that we give the remote object name "Mortgage" which is bound to the stub that is returned from the exportObject() method.

So, let's put all these together. The server class is listed below:

 
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
	
public class Server {
	
    public Server() {}

    public static void main(String args[]) {
	
	try {
	    PaymentImpl robj = new PaymentImpl();
	    Payment stub = (Payment) UnicastRemoteObject.exportObject(robj, 0);

	    Registry registry = LocateRegistry.getRegistry();
	    registry.bind("Mortgage", stub);
	    System.out.println("Mortgage Server is ready to listen...");

	} catch (Exception e) {
	    System.err.println("Server exception thrown: " + e.toString());
	    e.printStackTrace();
	}
    }
}

This is a simple RMI Server. And now you are ready to run it. But you need to create a client to test its functionality.

Ask yourself why we need to create a server?

Our next job is to create a client class.

Check your skill

Design a server for the remote object class which you did in the previous section.

A possible solution: HelloServer.java

page 1  page 2  page 3  page 4  page 5  page 6  Previous lesson 3/6 |home| Next lesson 5/6