Click here for other java examples. Click here for the top page.


A Simple Java RMI example

 

Takashi Yamanoue , yamanoue@cc.kagoshima-u.ac.jp

 

-          The client program(RmiClient.class) sends a message to the server program(RmiServer.class). The server program print out the message on the console.

 

 

-          This example consists of the following files

Ø         MessageReceiverInterface.java

²        This part defines the RMI interface. The method (receiveMessage) of the server class, which implements this interface, is called from the remote client. In the remote client program, the type of the server class (which is the remote class in this client class) is this interface.

Ø         RmiServer.java

²        This is the server program(class). In this class, the method greceiveMessageh, which is called from the remote client, is defined. This class is the implementation of the RMI interface.

Ø         RmiClient.java

²        This is the client program(class). The remote method is called from this class.

 

-          Execution outline

1.      RmiServer creates the gregistryh. This is a kind of dictionary. Its key is a name (which is the ID of a remote object) and its content is an object. This object is looked up from a remote program by the name. This registry is accessed from a remote object by the IP address (or host name) and the port number.

2.      RmiServer binds the name grmiServerh and it-self(RmiServer.class) in the registry.

3.      RmiClient looks up the remote object (RmiServer) by the name grmiServerh.

 

4.      RmiClient calls the method greceiveMessageh of the RmiServer class.

5.      The method greceiveMessageh of the RmiServer class prints out the message.

-          Compile

1.      javac RmiServer.java

2.      rmic RmiServer

3.      javac RmiClient.java

 

-          Execution

1.      (at one host,) java RmiServer

2.      (at another host) java RmiClient <serverfs address> 3232 <message text>

-          The source codes

ReceiveMessageInterface.java

 

    import java.rmi.*;

public interface ReceiveMessageInterface extends Remote

{

    @@@@@@@void receiveMessage(String x) throws RemoteException;

@@@@@@}

 

RmiServer.java

 

import java.rmi.*;

import java.rmi.registry.*;

import java.rmi.server.*;

import java.net.*;

 

public class RmiServer extends java.rmi.server.UnicastRemoteObject

implements ReceiveMessageInterface

{

    int      thisPort;

    String   thisAddress;

    Registry registry;    // rmi registry for lookup the remote objects.

 

    // This method is called from the remote client by the RMI.

    // This is the implementation of the gReceiveMessageInterfaceh.

    public void receiveMessage(String x) throws RemoteException

    {

        System.out.println(x);

    }

 

    public RmiServer() throws RemoteException

    {

        try{

            // get the address of this host.

            thisAddress= (InetAddress.getLocalHost()).toString();

        }

        catch(Exception e){

            throw new RemoteException("can't get inet address.");

        }

thisPort=3232;  // this port(registryfs port)

        System.out.println("this address="+thisAddress+",port="+thisPort);

        try{

        // create the registry and bind the name and object.

        registry = LocateRegistry.createRegistry( thisPort );

            registry.rebind("rmiServer", this);

        }

        catch(RemoteException e){

        throw e;

        }

    }

   

    static public void main(String args[])

    {

        try{

        RmiServer s=new RmiServer();

    }

    catch (Exception e) {

           e.printStackTrace();

           System.exit(1);

    }

     }

}

 

RmiClient.java

 

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class RmiClient
{
    static public void main(String args[])
    {
       ReceiveMessageInterface rmiServer;
       Registry registry;
       String serverAddress=args[0];
       String serverPort=args[1];
       String text=args[2];
       System.out.println("sending "+text+" to "+serverAddress+":"+serverPort);
       try{
           // get the gregistryh
           registry=LocateRegistry.getRegistry(
               serverAddress,
               (new Integer(serverPort)).intValue()
           );
           // look up the remote object
           rmiServer=
              (ReceiveMessageInterface)(registry.lookup("rmiServer"));
           // call the remote method
           rmiServer.receiveMessage(text);
       }
       catch(RemoteException e){
           e.printStackTrace();
       }
       catch(NotBoundException e){
           e.printStackTrace();
       }
    }
}

 


-          References

1.       Trail: RMI ,  in The JavaTM Tutorial, http://java.sun.com/docs/books/tutorial/rmi/

A brief overview of the RMI system and then walks through a complete client/server example that uses RMI's unique capabilities to load and to execute user-defined tasks at runtime.

2.      Java Remote Method Invocation (Java RMI), http://java.sun.com/products/jdk/rmi/

 


Takashi Yamanoue , yamanoue@cc.kagoshima-u.ac.jp