Recent Changes - Search:

Distributed Computing

This website demonstrates using wikis as teaching and learning tool.

The course instructor is also happy to share the teaching materials here with those who find it readable.

Java Network Programming - Part 3

A Distributed Computing Lecture by Steven Choy

Lecture Overview: RMI principles - RMI registry, stub and skeleton - RMI classes and interfaces - RMI in practice


Java Remote Method Invocation

RMI Overview

  • Access to Remote Objects
  • Java-to-Java only
  • Client-Server Protocol
  • High-level API
  • Transparent

RMI vs socket programming

  • RMI
    • Higher level interface to networking, no need to cater for communication details
    • Costly overhead of call setup and marshalling
    • Good for distributed applications where the ability to create arbitrary interconnections will be useful
  • Socket
    • Good for interacting with existing socket-based applications

Distributed Computing - Remote Objects

  • Remote objects live on server
  • Remote objects are accessed as if they were local
The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. RMI provides for remote communication between programs written in the Java programming language.

RMI Layers

RMI Principles

  • A set of protocols/facilities for Java objects to communicate via methods regardless of locations, hiding networking details from API
  • It forms the basis for many other API such as EJB
  • Remote object - its methods are invoked from another Java object on a different host
  • Remote interface - declare methods of remote objects that can be remotely accessed
  • Naming registry - server registers remote object's name for client to lookup (rmiregistry)
  • Typically has server and client
    • Server creates remote object, makes references to them accessible to clients, waits for clients' invocation
    • Client gets remote reference to remote objects and invokes methods on them
  • RMI handles all marshaling, delivery, and unmarshaling of objects
  • Object passing is always by value, and remote object is "by value of stub"

Stub and skeleton

  • Stub - a class automatically translating remote method calls into network communication setup and parameter passing
  • Skeleton - a class residing on remote machine to accept these network connections & translate them into actual method calls on actual objects
  • Generated by the tool rmic
    • E.g. rmic -v1.1 -d . MyRemoteImpl
  • JDK 1.2 (JRMP 1.2) does not need skeleton file
    • E.g. rmic -v1.2 -d . MyRemoteImpl
  • JDK 1.5 does not need skeleton and stub files!!

Writing an RMI application

1) Define the remote interface

  • Extends the marker/tag interface Remote
  • Every (remote) method throws RemoteException

Example: RMI remote interface DateServer

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;

public interface DateServer extends Remote {
   public Date getDate () throws RemoteException;
}

2) Write the server (remote object)

  • Extends UnicastRemoteObject
  • Implements the newly defined remote interface
  • Write a constructor that throws RemoteException
  • In main, create the remote server object, bind/register it to the naming registry (may setSecurityManager)

Example: RMI server class DateServerImpl

import java.rmi.*;
import java.rmi.server.*;
import java.util.Date;

public class DateServerImpl extends UnicastRemoteObject
 implements DateServer {

   public DateServerImpl () throws RemoteException { }
   public Date getDate () {
      return new Date ();
   }

   public static void main (String[] args) throws Exception {
      DateServerImpl dateServer = new DateServerImpl ();
      Naming.bind ("DateServer", dateServer); // No space in JDK 1.4/1.5
   }
}

3) Write the client

  • Lookups the remote object
  • Invoke the remote object's methods like local object

Example: RMI client class DateClient

import java.rmi.Naming;
import java.util.Date;

public class DateClient {
   public static void main (String[] args) throws Exception {
      if (args.length != 1) throw
         new IllegalArgumentException("Syntax: DateClient <hostname>");
      DateServer dateServer = (DateServer) Naming.lookup
         ("rmi://" + args[0] + "/DateServer"); // No space in JDK 1.4/1.5
      Date when = dateServer.getDate ();
      System.out.println (when);
   }
}

Compiling an RMI application

  • Compile all Java source file
   javac *.java
  • Generate the the server's stub & skeleton files
   rmic -v1.1 DateServerImpl
  • Server needs class files of
   DateServer, DateServerImpl, DateServerImpl_Stub, DateServerImpl_Skel
  • Client needs class files of
   DateServer, DateClient, DateServerImpl_Stub

Running an RMI application

  • Run the RMI registry in server
   rmiregistry
  • Run the server
   java -Djava.rmi.server.codebase=file:/c:\serverfiledirectory\DateServerImpl
  • Run the client
   java DateClient registryhostname

RMI class file locations

  • RMI client needs
    • Client implementation classes
    • Remote interfaces classes
    • Stub classes (in execution, maybe by automatic distribution)
  • RMI server needs
    • Server implementation classes
    • Remote interfaces classes
    • Stub classes
    • Skeleton classes

More on programming Java RMI

RMI classes and interfaces

  • Client side
    • Interface java.rmi.Remote
    • Class java.rmi.Naming
  • Registry
    • Class java.rmi.registry.LocateRegistry
    • Interface java.rmi.registry.Registry
  • Server side
    • Class java.rmi.server.RemoteObject
    • Class java.rmi.server.RemoteServer
    • Class java.rmi.server.UnicastRemoteObject

Remote and RemoteException

  • Interface Remote
    • Serves as an identification for remote interfaces
    • A marker interface, declaring no methods
    • A custom-defined remote interface must extends this Remote interface
  • Class RemoteException
    • All methods in a remote interface must declare throwing RemoteException
    • Common superclass of possible communicationrelated exceptions

Class Naming

  • Provides addressing services for server to register and for client to lookup
    • rmi://hostname:port/service
    • Default - localhost (hostname), 1099 (port), service listing (service)
    • service must contain no space in JDK 1.4
  • For security, binding is only allowed in same host as the naming registry
  • Static methods
 Remote lookup(String address)
throws MalformedURLException, RemoteException, NotBoundException
 void bind(String address, Remote object)
  throws MalformedURLException, RemoteException, AlreadyBoundException

 void rebind(String address, Remote object)
  throws MalformedURLException, RemoteException

 void unbind(String address)
  throws MalformedURLException, RemoteException, NotBoundException

 String[] list (String address)
  throws MalformedURLException, RemoteException

Registry and LocateRegistry

  • Class Naming relies on
    • Class LocateRegistry to make initial connection to the registry
      • Obtain remote references to naming registries
      • Directly start a naming registry
    • Interface Registry to help binding & lookup of RMI server objects
  • Methods of class Naming call to LocateRegistry followed by remote method calls through interface Registry

Class LocateRegistry

  • Static methods
Registry getRegistry()
   throws RemoteException

Registry getRegistry(int port)
   throws RemoteException

Registry getRegistry(String host)
   throws RemoteException

Registry getRegistry(String host, int port)
   throws RemoteException

Registry getRegistry(String host, int port, RMIClientSocketFactory clients)
   throws RemoteException

Registry createRegistry(int port)
   throws RemoteException

Registry createRegistry(int port,
   RMIServerSocketFactory servers, RMIClientSocketFactory clients)
   throws RemoteException

Interface Registry

  • Static variable
   int REGISTRY_PORT // default port of 1099
  • Methods
   Remote lookup(String name) throws RemoteException, NotBoundException

   void bind(String name, Remote object) 
    throws RemoteException, AlreadyBoundException

   void rebind(String name, Remote object) 
    throws RemoteException

   void unbind(String name)
    throws RemoteException, NotBoundException

   String[] list () throws RemoteException

Class RemoteObject

  • Ultimate superclass of all remote object implementations and remote stubs
  • Overrides hashCode(), equals(), toString(), and implements Serializable
  • Remote method can have parameters of nonremote objects or remote interfaces, but not actual remote object implementation
    • Remote objects as parameters are actually sent as remote reference (stub class) to remote object implementation, not by copy

Class RemoteServer

  • Abstract subclass of RemoteObject
  • Provide server functions
  • Static methods
    • String getClientHost() throws ServerNotActiveException
    • void setLog(OutputStream out)
      • Examples:
      • RemoteServer.setLog( new FileOutputStream("rmi.log") );
      • RemoteServer.setLog(null); // logging off
    • PrintStream getLog()

Class UnicastRemoteObject

  • Concrete subclass of RemoteServer
  • Superclass for most normal remote objects
  • Constructors
   protected UnicastRemoteObject() throws RemoteException

   protected UnicastRemoteObject(int port) throws RemoteException

   protected UnicastRemoteObject(int port, 
     RMIClientSocketFactory clients, RMIServerSocketFactory servers)
     throws RemoteException
  • Static methods
   // implementing a remote object without subclassing
   // UnicastRemoteObject directly

   RemoteStub exportObject(Remote object) throws RemoteException

   RemoteStub exportObject(Remote object, int port) throws RemoteException

   RemoteStub exportObject(Remote object, int port, 
      RMIClientSocketFactory clients,
      RMIServerSocketFactory servers) throws RemoteException

   boolean unexportObject(Remote object, boolean force) 
      throws NoSuchObjectException

Using method exportObject

import java.rmi.*;
import java.rmi.server.*;

// this class does not subclass UnicastRemoteObject
public class MyClass extends SomeClass implements MyRemote {

   // methods
   public static void main(String[] args) throws Exception {
      MyClass obj = new MyClass();
      MyRemote ref = (MyRemote) UnicastRemoteObject.exportObject(obj);
      Naming.rebind(?MyClass/MyInstance?, ref);
   }
}

Automatic class file distribution

  • RMI allows download class files automatically
  • In the program that delivers class file (server)
    • Specify codebase when invoking program
      java -Djava.rmi.server.codebase=http://abc.com/good/classes/ ServerImpl
  • In the program that downloads class file (client)
    • Install SecurityManager in program
      System.setSecurityManager( new java.rmi.RMISecurityManager() );
    • Create a security policy file
 grant {
    permission java.net.SocketPermission "*:1024-65535", "connect,accept";
    permission java.io.FilePermission "c:\\path\\server\\-", "read";
 };
  • Specify security policy when invoking program
   java -Djava.security.policy=java.policy ClientImpl

Coursework

You need to do this tutorial after this lecture.

Java RMI provides a simple and direct model for distributed computing with Java objects. The objective of this tutorial is to allow you to practise programming RMI for distributed computing applications. You will apply the Java RMI system to implement a distributed computation of pi (π).

You need to demonstrate your work to your instructor during the tutorial class. Your instructor may ask you some questions to test your knowledge.


Extra materials for studying Java RMI

The Java Remote Method Invocation (RMI) system allows an object running in one Java virtual machine to invoke methods on an object running in another Java virtual machine. RMI provides for remote communication between programs written in the Java programming language.
Remote method invocation allows applications to call object methods located remotely, sharing resources and processing load across systems. Unlike other systems for remote execution which require that only simple data types or defined structures be passed to and from methods, RMI allows any Java object type to be used - even if the client or server has never encountered it before. RMI allows both client and server to dynamically load new object types as required. In this article, you'll learn more about RMI.
We are looking at different methods to implement two-way communication for sending Java objects from one computer to another and back.
This is a chapter from Neal Harman's Internet Computing, Distributed Object-Oriented Programming.

Thanks for Reading

If you would rather like to have this lecture note in printed format, please click the print action link in the top right corner.

If you find any problem in this lecture note, please feel free to reach Steven by steven@findaway.hk

Edit - History - Print - Recent Changes - Search
Page last modified on June 17, 2009, at 09:46 PM