Remote Method Invocation



General Information

Introduction

RMI (remote method invocation) is a mechanism for writing distributed programs that communicate by calling methods that may actually reside remotely, i.e, are executing in a JVM on some remote computer.

This advantage is that the details of socket communication and marshalling data is hidden from the programmer.

rmi

RMI Registry

rmi1 rmi2 rmi3

RMI is supported by three packages: java.rmi, java.rmi.server, java.rmi.activation (new in Java 1.2), and java.rmi.registry. Use of RMI requires a program to generated Java programs, rmic, and a server to broker communication, registry. Both programs are part of the standard JDK1.2 distribution.

A simple example.

  1. Write a service that gives identifying information about the remote host.

    Begin by defining an interface, say, Identifiable.java which extends extends Java class java.rmi.Remote (Java API documentation) (local copy). This interface accounces the procedure calls to be provided to remote clients.

    Implement the interface, by writing, say, IdentityRemoteObject.java which extends Java class java.rmi.server.UnicastRemoteObject (Java API documentation) (local copy)

  2. Compile the Java source
          javac Identifiable.java IdentityRemoteObject.java
          

  3. Create _Stub and _Skel classes for remote object
    rmic IdentityRemoteObject
          
    yielding
    IdentityRemoteObject_Stub.class
    IdentityRemoteObject_Skel.class
           

    (The source, _Stub.java and _Skel.java is created. Take a look at it for informational purposes.)

    IdentityRemoteObject_Stub.java
    IdentityRemoteObject_Skel.java
           
  4. Write the server IdentityServer.java, really just registering the service using the static Java method Naming.rebind() (Sun source) (local copy)

  5. Compile the server.
           javac IndentifyServer.java
           

  6. Start the registry.

           # start registry
           > rmiregistry [9901] &
           [N] pid1
           

    rmiregistry (sun copy) (local copy)

  7. Start and register service called "identity".
           # start java IdentityServer localhost 9901
           > java IdentityServer localhost 9901 &
           [M] pid2
          

  8. Now write the client IdentityClient.java. the makes use of the remote interface Identifiable.java created for the service. Ask for the "identity" service on the host using a special URL-like string:
           Naming.lookup ("//host:9901/identity")
           
    This returns (a handle to) the remote object
           Identifiable o = (Identifiable) Naming.lookup ("//host:9901/identity")
           

  9. Compile the client
  10. Run the client
    > java IdentityClient localhost
    ryan
    maelstrom.cs.fit.edu
    Thu Apr 29 12:40:04 EDT 1999
    
  11. Remove all the running processes
    > kill %N %M
           

Another example -- a feeble authenticating server.

Other examples from Java by Example.

It is possible for a remote object to be notified when all clients disconnect by implementing the java.rmi.server.Unreferenced interface (in addition of the others)

Remote Object Activation


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Thu Apr 22 15:12:17 EDT 2004