Remote Method Invocation in Java
An Introduction |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
rmiregistry
and
rmid
) that provide registry servicesServer
)
Impl
suffix (e.g., ServerImpl
)
rmic
tool
normally included the _Stub
suffix
(e.g., Server_Stub
)
rmic
tool
normally included the _Skel
suffix
(e.g., Server_Skel
).
Remote
Interface:
Remote
interfaceRemote
interface has no methods or
fields and serves only to identify the semantics of
being remotely availableRemoteException
Class:
Remote
interface must list
RemoteException
in its throws clause
Course
Class is Unchangedimport 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; } }
CourseDatabase
Interfaceimport 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; }
CourseDatabase
Implementationimport 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); } }
grant { permission java.security.AllPermission; };
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(); } } }
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(); } } }
javac
command.
rmiregistry
command
(which uses port 1099 by default).
java
command).
History:
rmic
tool was
used to create stubs and skeletons.
rmic
tool was used
to create stubs but explicit skeletons were not needed.
rmic
tool is no longer needed.
(The rmic
tool can
be used for IIOP and IDL.)
Activatable
Class:
rmid
Tool: