Upload
hai-nguyen
View
225
Download
0
Embed Size (px)
Citation preview
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
1/25
Lecture 7
Remote Method Invocation (RMI)
HET715 Network Computing
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
2/25
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
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
3/25
Remote Procedure Calls (RPC)
All our methods so far have been invoked onlocal objects
In a real-life distributed environment it is
often desirable to invoke methods on objectslocated on other (remote) systems
RPC Technologies allow a client to find andthen invoke (call) methods on a remote object
(server) in such a way that it looks like theserver is a local object
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
4/25
RPC Technologies
Several RPC technologies are available which
operate in a similar fashion:
Remote Method Invocation (RMI) is the main Java oriented
technology (covered in this lecture) Common Object Request Broker Architecture (CORBA) is an
older technology found in industry but rarely used in new
projects (covered in another lecture)
Web Services is a more recent technology based on Extensible
Markup Language (XML) (covered in another lecture)
Distributed Component Object Model (DCOM) is an even older,
less common Windows-based technology (not covered in this
unit)
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
5/25
What is RMI?
RMI is a platform-independent RPC technology whichhas been a core component of the Java language fromits 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 thescenes
Once a reference to the remote object is obtained themethods of that object can be invoked as if the objectwas local
The fact that an object is located remotely is almosttransparent to a Java programmer
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
6/25
Remote Method Invocation
Host: swin.edu.au Host: csu.edu.au
networkObject
Client
Remote
Object
Servermethod invocation
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
7/25
Remote Method Invocation
Local Machine (Client)
SampleServer remoteObject;
int s;
s = remoteObject.sum(1,2);
System.out.println(s);
Remote Machine (Server)
public int sum(int a,int b)
{
return a + b;}
1,2
3
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
8/25
The basic RMI process
The serverprogram that has control on the remoteobject registers an interface with a naming service
The interface contains the signatures of thosemethods of the object that the server wishes to make
publicly available A client program then uses the same naming service
to obtain a reference to this interface in the form ofwhat is called a stub which acts as a local proxy for theremote object
On the remote server there will be another proxy calleda skeleton
When the client program invokes a method of theremote object, it appears (to the client) that the methodis being invoked directly on the object
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
9/25
Naming Service (rmiregistry)
Directory that associates names to remote
objects (bind/rebind)
Host: csu.edu.au
Remote
Object
C
RemoteObject
A
RemoteObject
B
Naming
X
Y
Z
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
10/25
Naming Service (cont.)
Client uses Naming Service to find aparticular remote object (lookup)
swin.edu.au Host: csu.edu.au
Object
Client Remote
Object
Naming
X
Y
Z
lookup(Y)
Reference to
Remote object
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
11/25
What actually happens
An equivalent method is being called in the stub (proxyon the local machine)
The stub then forwards the call and any parameters(via marshalling) to the skeleton (proxy on the remote
machine) Upon receipt of the byte stream the skeleton converts
this stream into the original method call andassociated parameters (via unmarshalling)
The skeleton then calls the implementation of themethod on the server
If the method has a return value then the aboveprocess is reversed with the return value beingserialized on the server(by the skeleton) anddeserialized on the client (by the stub)
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
12/25
The basic RMI process
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
13/25
Sending parameters and return values
Recall that we can pass zero or more parameters to amethod which returns one or no value (void) to thecalling code. These can be simple values like anumber (int) or text (String) (these types are readily
serializable objects), or a complex object (eg.Customer)
When sent over a network, such values need to beencoded into a stream of bytes that can be transmittedon a stream through the socket. This is called
Marshal l ing When received, the stream of bytes need to be
decoded back into object. This is calledUnmarshal l ing
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
14/25
Serialization
Marshalling & Unmarshalling parameters and returnvalues is usually achieved by using a process calledSerialization, a facility automatically available in Javaand 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 intoraw 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 importingjava.io.Serializable and then
stating in the class definition that the classimplements the Serializable interface
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
15/25
The packages and steps of
implementation
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
Four steps are involved as follows:
Create the interface to be registered
Define a class that implements this interface
Create the server process
Create the client process
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
16/25
An example for RMI
The example application that follows is simply used
to illustrate the fundamental principles of RMI
It just displays a greeting to any client that uses the
appropriate interface registered with the namingservice to invoke the associated method
implementation on the server
In a more realistic application there would (probably)
be more methods and (possibly) more objectsinvolved
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
17/25
Step-1: Create the interface
This interface must import packagejava.rmi and extend interfaceRemote which contains no methods
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)
}
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
18/25
Step-2: Define a class that implements
the interface
The implementation class (HelloImpl) should import packages
java.rmi andjava.rmi.serverand extend RemoteObject class or
one of its subclasses
In practice most implementations extend the
UnicastRemoteObject subclass since it supports point-to-point
communication using TCP
A constructormust be provided (even if it has an empty body as
in the example) for the implementation object which (like the
methods declared in the interface) must declare that it throws a
Remote Exception
The implementation class must also implement the interface
created previously (ie. the Hello interface) by providing an
executable body for the single interface method (getGreeting)
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
19/25
The implementation of the interface
//Implementation of RMI interface.
import java.rmi.*; //(1)
import java.rmi.server.*; //(1)
public class HelloImpl extends UnicastRemoteObject implements Hello //(2)
{ (imp class) (imp interface)
public HelloImpl() throws RemoteException //(3)
{
//No action needed here (but must provide a constructor).
}
public String getGreeting() throws RemoteException //(4)
{
return ("Hello there!");
}
}
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
20/25
Step-3: Create the server process
The server creates an object of the implementation class (HelloImpl)and registers it with a naming service called rmiregistry
It does this by using the static method rebind of class Naming frompackagejava.rmi (ie. public static void Naming.rebind(String name,Remote object )
This method takes two arguments: A String that holds the name of the remote object as a URL with protocol rmi
A reference to the remote object
It establishes an association between the objects name (the URL) andits reference after which clients are able to retrieve a reference to thatobject via the registry
The URL string as well as specifying a protocol ofrmi and a name forthe object, specifies the name of the remote objects host machine
The default for RMI host is localhost (which is used in our example forsimplicity) and the default port for RMI is 1099 (can use others ofcourse)
The example has only one method (main) and to cater for various typesof exceptions that may be generated it throws Exception
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
21/25
Creating the server process
//Server.
import java.rmi.*; //(1)
public class HelloServer
{
private static final String HOST = "localhost";
public static void main(String[] args) throws Exception //(2)
{
HelloImpl temp = new HelloImpl(); //(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); //(4)
System.out.println("Binding complete...\n");
}
}
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
22/25
Step-4: Create the client process
The client obtains a reference to the remote object from theregistry by using the static method lookup of the Naming classfrom packagejava.rmi (ie. public static RemoteNaming.lookup(String name)
It supplies the URL to the method as the argument (same URL
as the one that the server used when binding the objectreference to the object name in the registry)
Since lookup returns a Remote reference this reference must betype cast into a Hello reference (note: not a HelloImpl reference)
Once the Hello reference has been obtained it can be used tocall the solitary method that was made available in the interface
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
23/25
Creating the client process
//Client.
import java.rmi.*; //(1)
public class HelloClient{private static final String HOST = "localhost";
public static void main(String[] args) throws Exception
{
try
{
Hello greeting = (Hello)Naming.lookup("rmi://" + HOST + "/Hello"); //(2)
// Hello type casting.
//Simply retrieve and display greeting...
System.out.println("Message received: + greeting.getGreeting()); //(3)
}
catch(ConnectException conEx)
{
System.out.println("Unable to connect to server!");
System.exit(1);
}
catch(Exception ex)
{
ex.printStackTrace();
System.exit(1);
}
}
}
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
24/25
Compilation and execution
1. Compile all files withjavacjavac Hello.javajavac HelloImpl.java
javac HelloServer.java
javac HelloClient.java
OR
javac *.java2. Compile the implementation class with the rmic compiler to
create the stub and perhaps skeleton (older versions of Java)classes
rmic HelloImpl
3. Start the RMI registryrmiregistry
4. Open a new window and run the serverjava HelloServer
5. Open a third window and run the client
java HelloClient
If i li t d
7/27/2019 Lecture 7 _New- Remote Method Invocation(1)
25/25
If you are running client and server on
different machines (this is usually the
case in real life applications)
1. Compile all files as in previous slide (step 1 and 2 in thepreceding slide)
2. Make sure rmiregistry and the server related files are in thesame folder, running in the same order (ie. rmiregistry first)but in different DOS command windows on the same machine
3. The server side folder should have the following files:HelloServer.class
Hello.class
HelloImpl.class
HelloImpl_Stub.class
4. The client side folder should have the following files:HelloClient.class
Hello.class
HelloImpl_Stub.class