JMU
Remote Method Invocation in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Introduction
Class Name Conventions in Java RMI
Designing for RMI in Java
Passing Objects:
Comparing Objects
A Simple Example

The Course Class is Unchanged

javaexamples/remoteobjects/Course.java
import java.io.Serializable;


/**
 * A Course 
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Course implements Serializable
{
    private int    number;
    private String department, title;

    /**
     * Explicit Value Constructor
     *
     * @param department  The department code
     * @param number      The course number
     * @param title       The course title
     */
    public Course(String department, int number, String title)
    {
        this.department = department;
        this.number     = number;
        this.title      = title;
    }

    /**
     * Get the course designation
     *
     * @return  The course designation
     */
    public String getDesignation()
    {
        return department+number;
    }

    /**
     * Get the course title
     *
     * @return  The course title
     */
    public String getTitle()
    {
        return title;
    }
}
        
A Simple Example (cont.)

The CourseDatabase Interface

javaexamples/remoteobjects/rmi/CourseDatabase.java
import java.rmi.*;


/**
 * Methods that must be implemented by a database of Course objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0rmi
 */
public interface CourseDatabase extends Remote
{
    /**
     * Add a Course
     *
     * @param course   The Course to add
     */
    public void add(Course course)  throws RemoteException;


    /**
     * Get a Course
     *
     * @param course   The Course to add
     */
    public Course get(String department, int number)  throws RemoteException;


    /**
     * Remove a Course
     *
     *
     * @param department   The department code
     * @param number       The course number
     */
    public void remove(String department, int number)  throws RemoteException;

}
        
A Simple Example (cont.)

The CourseDatabase Implementation

javaexamples/remoteobjects/rmi/CourseDatabaseImpl.java
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

/**
 * The implementation of a database of Course objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0rmi
 */
public class CourseDatabaseImpl extends    UnicastRemoteObject
                                implements CourseDatabase
{
    private Hashtable      db;

    /**
     * Constructor
     */
    public CourseDatabaseImpl() throws RemoteException
    {
        db = new Hashtable();
    }

    /**
     * Add a Course
     *
     * @param course   The Course to add
     */
    public void add(Course course)  throws RemoteException
    {
        db.put(course.getDesignation(), course);
    }

    /**
     * Get a Course
     *
     * @param course   The Course to get
     */
    public Course get(String department, int number)  throws RemoteException
    {
        return (Course)db.get(department+number);
    }

    /**
     * Remove a Course
     *
     * @param department   The department code
     * @param number       The course number
     */
    public void remove(String department, int number)  throws RemoteException
    {
        db.remove(department+number);
    }
}
        
Security

You Need a Policy File

grant
{
	permission java.security.AllPermission;
};
  
Interacting with the Registry
A Simple Example (cont.)

The Client Driver

javaexamples/remoteobjects/rmi/ClientDriver.java
import java.rmi.*;

/**
 * The client in an example of Java's remote method invocation
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0rmi
 */
public class ClientDriver
{

    /**
     * The entry point
     *
     * @param args  The command line arguments
     */
    public static void main(String[] args)
    {
       Course           course;       
       CourseDatabase   db;
       String           host, username;
        
       System.setProperty("java.security.policy", "database.policy");        

       try 
       {
          db = (CourseDatabase)Naming.lookup("rmi://localhost:22801/CourseDatabase");
          db.add(new Course("CS",139,"Algorithm Development"));
          db.add(new Course("CS",240,"Data Structures and Algorithms"));
          db.add(new Course("CS",349,"Developing Multimedia Content"));
          db.add(new Course("CS",462,"Network Applications Development"));
          
          course = db.get("CS",462);
          System.out.println(course.getDesignation()+"\t"+
                             course.getTitle());          

       }
       catch (Exception e) 
       {
          e.printStackTrace();
       }
    }

}
        
A Simple Example (cont.)

The Server Driver

javaexamples/remoteobjects/rmi/ServerDriver.java
import java.rmi.*;

/**
 * A server Java's remote method invocation
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0rmi
 */
public class ServerDriver
{
    private static final String SERVERNAME = "//localhost:22801/CourseDatabase";


    /**
     * The entry point
     *
     * @param args  The command line arguments
     */
    public static void main(String[] args)
    {
       CourseDatabaseImpl    db;


       System.setProperty("java.security.policy", "database.policy");        
       System.setSecurityManager(new RMISecurityManager());


       try 
       {
          db = new CourseDatabaseImpl();
          Naming.rebind(SERVERNAME, db);
       }
       catch (Exception e) 
       {
          e.printStackTrace();
       }
    }
}
        
Running Java's RMI Tools
  1. Compile the source files using the javac command.
  2. Start the registry using the rmiregistry command (which uses port 1099 by default).
  3. Run the application that adds the necessary remote objects to the registry (using the java command).
  4. Run the applications/applets that use the remote objects.

History:

RMI and Persistence in Java