|
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. |
Lecture /
Java Network Programming - Part 3A 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 InvocationRMI Overview
RMI vs socket programming
Distributed Computing - Remote Objects
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
Stub and skeleton
Writing an RMI application
1) Define the remote interface
Example: RMI remote interface DateServerimport 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)
Example: RMI server class DateServerImplimport 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
Example: RMI client class DateClientimport 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
javac *.java
rmic -v1.1 DateServerImpl
DateServer, DateServerImpl, DateServerImpl_Stub, DateServerImpl_Skel
DateServer, DateClient, DateServerImpl_Stub Running an RMI application
rmiregistry
java -Djava.rmi.server.codebase=file:/c:\serverfiledirectory\DateServerImpl
java DateClient registryhostname RMI class file locations
More on programming Java RMIRMI classes and interfaces
Remote and RemoteException
Class Naming
Remote lookup(String address)
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 LocateRegistry
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
int REGISTRY_PORT // default port of 1099
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
Class RemoteServer
Class UnicastRemoteObject
protected UnicastRemoteObject() throws RemoteException
protected UnicastRemoteObject(int port) throws RemoteException
protected UnicastRemoteObject(int port,
RMIClientSocketFactory clients, RMIServerSocketFactory servers)
throws RemoteException
// 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 exportObjectimport 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
grant {
permission java.net.SocketPermission "*:1024-65535", "connect,accept";
permission java.io.FilePermission "c:\\path\\server\\-", "read";
};
java -Djava.security.policy=java.policy ClientImpl CourseworkYou 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 RMIThe 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 ReadingIf 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 |