Distributed Computing - Remote Method Invocation (RMI)
Distributed Computing - Remote Method Invocation (RMI)
import java.rmi.*;
Import java.rmi.server.*;
public class SpeechImpl extends UnicastRemoteObject implements
Speech{
public SpeechImpl() throws RemoteException {};
public String sayHello() throws RemoteException{
Return “Hello”;
}
}
Create a Remote Implementation (Impl)
The stubs and skeletons are created by the rmic tool
Supplied with the Java JDK
rmic SpeechImpl
rmic creates a stub for use by the client
SpeechImpl_Stub.class
rmic creates a skeleton for use by the remote object
SpeechImpl_Skel.class
Inheriting from UnicastRemoteObject
Constructor will export the class
Effectively make the stub available
The class redefines several java.lang.Object methods
.hashCode() , .equals() , and .toString()
These are redefined to be used in a distributes environment
Create the Publishing Process
A publishing process exposes the remote object for use
Create an instance of object (remote object)
Publishes the object via RMI Registry
May simply be main method of implementation class
May be a separate Java class to carry out work
The RMI Registry is responsible for remote object
publication
Hold details linking names to instantiated objects
Relates to the static Naming class
Expose bind (_) and rebind (_) methods
Listen on a socket port for incoming request
1099 by default
Create a Publishing Process
Code:
Create an instance of
import java.rmi.*; the implementation
public class Publisher{ class
public static void main(String args[]){
try{
SpeechImpl si = new SpeechImpl();
Naming.rebind(“Talker”, si);
}
catch (Exception e){
e.printStackTrace();
} Bind the remote
} object into the
} RMI Registry
Start RMI Registry and Server
The Registry code is found in java/bin
rmiregistry utility
start rmiregistry
java Publisher
Create the Client Application
A client process needs to interact with the remote object for service
Locate an instance of remote object via the RMI Registry
Only interested in finding any object that supplies services
Only has the name used when object was published
Does not know, or need to know the class used
May also load an RMI Security Manager
Controls what is allowed to be downloaded
Similar to control over applet downloading
The client program gets a reference (not object) to the remote object
Returned from a call to the Naming.lookup(_) method
Reference is of type Remote that can be cast as appropriate
Actually a reference to the downloaded stub
Page for diagrams
Create a Publishing Process
Code:
import java.rmi.*;
public class TheClient{ Create an object
public static void main(String args[]){ variable of the
try{ remote interface
Speech sp;
System.setSecurityManager(new RMISecurityManager());Ask the Registry
sp=(Speech) Naming.lookup(“rmi://loaclhost/Talker”); for an instance of
System.out.println(sp.sayHello()); the remote object
}
catch (Exception e){
e.printStackTrace();
}
}
}