Lecture 7 - New - Remote Method Invocation
Lecture 7 - New - Remote Method Invocation
Lecture 7
Remote Method Invocation (RMI)
Objectives
To introduce Remote Procedure Call (RPC) technologies To introduce Remote method invocation (RMI) To describe the basic RMI process To implement RMI using Java To compile and execute the RMI application
RPC Technologies
What is RMI?
RMI is a platform-independent RPC technology which has been a core component of the Java language from its inception Under RMI networking details required by explicit programming of streams and sockets disappear RMI infrastructure handles the use of byte streams to transfer data and method invocations behind the scenes Once a reference to the remote object is obtained the methods of that object can be invoked as if the object was local The fact that an object is located remotely is almost transparent to a Java programmer
Object Client
Host: swin.edu.au
Host: csu.edu.au
1,2
public int sum(int a,int b) { return a + b; }
3
System.out.println(s);
Naming X Y
Remote Object C
Naming
Object Client lookup(Y) X Y Remote Object
Host: csu.edu.au
When received, the stream of bytes need to be decoded back into object. This is called Unmarshalling
Serialization
Marshalling & Unmarshalling parameters and return values is usually achieved by using a process called Serialization, a facility automatically available in Java and similar modern software technologies Normally objects pass a copy of their data contents to/from the server by serialization
Serialization allows one to convert object data into raw bytes and reconstruct the object data from bytes
Objects of a given class sent/received via serialization only have to implement the serialization interface
This is done by importing java.io.Serializable and then stating in the class definition that the class implements the Serializable interface
Three packages are used for an RMI client/server application (only first two need to be used explicitly):
java.rmi java.rmi.server java.rmi.registry
The interface definition for this example must specify the signature for method getGreeting which is to be made available to clients
This method must declare that it throws a Remote Exception
//RMI interface. import java.rmi.*;
//(1)
public interface Hello extends Remote //(2) { public String getGreeting() throws RemoteException; //(3) }
public class HelloImpl extends UnicastRemoteObject implements Hello //(2) { { (imp class) public HelloImpl() throws RemoteException (imp interface) //(3)
It establishes an association between the objects name (the URL) and its reference after which clients are able to retrieve a reference to that object via the registry The URL string as well as specifying a protocol of rmi and a name for the object, specifies the name of the remote objects host machine The default for RMI host is localhost (which is used in our example for simplicity) and the default port for RMI is 1099 (can use others of course) The example has only one method (main) and to cater for various types of exceptions that may be generated it throws Exception
{
private static final String HOST = "localhost"; public static void main(String[] args) throws Exception { //(2)
//(3)
String rmiObjectName = "rmi://" + HOST + "/Hello"; //naming URL //Could omit host name in this example, //since 'localhost' would be assumed by default. Naming.rebind(rmiObjectName, temp); System.out.println("Binding complete...\n"); } } //(4)
{
try { Hello greeting = (Hello)Naming.lookup("rmi://" + HOST + "/Hello"); // Hello type casting. //Simply retrieve and display greeting... System.out.println("Message received: + greeting.getGreeting()); } catch(ConnectException conEx) { System.out.println("Unable to connect to server!"); System.exit(1); } //(3) //(2)
catch(Exception ex)
{ ex.printStackTrace(); System.exit(1); } } }
2.
Compile the implementation class with the rmic compiler to create the stub and perhaps skeleton (older versions of Java) classes
rmic HelloImpl
3. 4. 5.
If you are running client and server on different machines (this is usually the case in real life applications)
1. 2. Compile all files as in previous slide (step 1 and 2 in the preceding slide) Make sure rmiregistry and the server related files are in the same folder, running in the same order (ie. rmiregistry first) but in different DOS command windows on the same machine The server side folder should have the following files:
HelloServer.class Hello.class HelloImpl.class HelloImpl_Stub.class
3.
4.