Sie sind auf Seite 1von 29

Presented By:

Sapna Saxena
Lecturer, CSE Department
Chitkara University
Remote Procedure Call
The concept of performing computations over a
network of machines had been around long before
the concept of Java itself.
A popular way to distribute work over a network is
through RPC (or Remote Procedure Call).
MACHINE A MACHINE B
line 1…. begin remote proc()
line 2…. ll R e mote
Ca
call proc()
r o c e dure … remote proc logic
call remote Proc() P
----
line n…. ----
---- Retur return value
---- n value end remote proc()
-line n+n

In the above figure, while local procedures are executed


on machine A, the remote procedure is actually
executed on machine B. the program executing on
machine A will wait until machine B has completed
the operation of the remote procedure, and then
continue with its program logic. The remote
procedure may have a return value that the
continuing program may use immediately.
Marshalling and Unmarshalling
While executing RPC’s, following operations are performed–
 The name of procedure and the arguments to the call are
packaged, and then this package is transmitted over the
network to the remote machine where the RPC server is
running. This act of packaging the data involved in a call,
and transmitting it over the network is called Marshalling.
 The RPC server picks up this transmission, decodes the
name of the procedure and the parameters. This act of un
packaging (decoding) is called Unmarshalling.
 The RPC server makes the actual procedure call on the
server (remote) machine.
 The RPC server packages the returned values and output
parameters (in some programming language) and then
transmits it over the network back to the machine that
made the call.
Mech. of Marshalling/Unmarshalling
---
Call remoteProc(param1,param2) Un marshalling
---
r
it ove
Package nsm k Package
Tr a
e t wor
N
begin remoteProc(a,b)
Marshalling -- logic
end remoteProc

In fact, the calling machine does not even have to know the
procedure is actually performed remotely on another machine.
In the figure, we can see how a remote call is actually placed “on
the network wire” by marshalling code.
On the remote machine, corresponding un marshalling code will
un package the data and make the actual call (logically) on the
remote machine.
Remote
Method
Invocation
Introduction to RMI
Java provide different mechanisms, such as RMI,
Corba, EJB. Etc. that allow one to invoke a method
on an object that exists in another address space.
The other address space could be on the same
machine or a different one.
The primary goal for the network designer is to allow
programmers to develop distributed Java programs
with the same syntax and semantics used for non-
distributed programs. To do this they had to
carefully map how Java classes and objects would
work in a distributed (multiple JVM) computing
environment.
Remote Method Invocation
Java’s RMI feature enables a program in one Java
Virtual Machine (JVM) to make a method calls on a
remote server machine within another JVM.
The Remote Method Invocation features give Java
programmers the ability to distribute computing
across a networked environment.
RMI internally use sockets over TCP/IP.
Object-oriented design requires every task to be
executed by the object most appropriate for that task.
RMI takes this concept one step further by allowing a
task to be performed on the machine most
appropriate for the task.
RMI Communication
NETWORK

request for
Machine A remote service Machine B Machine C

(JVM) (JVM) (JVM)


response

When Java code on Machine A needs a service or a


method from a remote Java object on Machine B, it
starts a remote method invocation. It does this the
same way as invoking a local object's method.
The whole communication behind the remote invocation
is done by the RMI mechanism.
How RMI Works
•RMI Registry
•RMI uses a network-based registry to keep track of
the distributed objects. The server object makes a
method available for remote invocation by binding it
to a name in the registry. The client object, in turn, can
check for availability of an object by looking up its
name in the registry . The registry acts as a limited
central management point for RMI . The registry is
simply a name repository . It does not address the
problem of actually invoking the remote method.
rmic – The Compiler
The two objects may physically reside on different
machines. A mechanism is needed to transmit the
client's request to invoke a method on the server
object and provide a response. RMI uses an
approach similar to RPC in this regard. The code
for the server object must be processed by an RMI
compiler called rmic, which is part of the JDK.
Stub and Skeleton Marshalled
Parameters
Parameters

Stub Skeleton Server


Object Object Object

Marshalled
Return
Return Method Invocation Value Method Invocation
Value on Client on Server

The rmic compiler generates two files: a stub


and a skeleton. The stub resides on the
client machine and the skeleton resides on
the server machine. The stub and skeleton
are comprised of Java code that provides the
necessary link between the two objects. 
Role of Stub and Skeleton
When a client invokes a server method, the JVM looks at
the stub to do type checking (since the class defined
within the stub is an image of the server class). The
request is then routed to the skeleton on the server,
which in turn calls the appropriate method on the server
object. In other words, the stub acts as a proxy to the
skeleton and the skeleton is a proxy to the actual remote
method.
Using RMI
A working RMI system is composed of several
parts –
Interface definitions for the remote services
Implementations of the remote services
Stub and Skeleton files
A server to host the remote services
An RMI naming service that allows clients to find
the remote services
A class file provider (an HTTP or FTP server)
A client program that needs the remote services.
Building RMI Application
Assuming that the RMI system is already designed, we
take the following steps to build a system –
1. Write and compile Java code for interfaces
2. Write and compile Java code for implementation
classes
3. Generate stubs and skeleton class files from the
implementation classes
4. Write Java code for a remote service host program
(server)
5. Develop Java code for RMI client
6. Install and run RMI system
Example - CalculatorServer
The Calculator server accepts tasks from clients, runs
the tasks, and returns any result. The server is
comprised of an interface and a class. The interface
provides the definition for the methods that can be
called from the client. Essentially the interface defines
the client’s view of the Remote object. The class
provides the implementation.
Structure - CalculatorServer
Client Server
CalculatorClient.java CalculatorServer.java
Lookup Lookup
CalculatorInterface.java CalculatorImplement.java

Stub Skeleton
CalculatorImplement_Stub.java CalculatorImplement_Skel.java

Remote Reference Layer (RRL)

Transport Network Transport


Designing Remote Interface
At the heart of the Calculator server, as a protocol that
allows jobs to be submitted to the Calculator server, the
Calculator server to run these jobs, and the results of the
job to be returned to the client. This protocol is expressed
in the interface CalculatorInterface supported by the
CalculatorServer . Mecahnism of CalculatorServer is
shown in figure –

Submit Tasks
Client CalculatorServer
Return Results

Interface, CalculatorInterface contains different methods


to allow jobs to be submitted to the server. The interface
defines the remotely accessible part.
Code of CalculatorInterface
//CalculatorInterface.java
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface CalculatorInterface extends Remote {
public long addition(long a, long b)
throws RemoteException;
public long subtraction(long a, long b)
throws RemoteException;
public long multiplcation(long a, long b)
throws RemoteException;
public long division(long a, long b)
throws RemoteException;
}
Implementing Remote Interface
Next, we write the implementing class for the
CalculatorServer . In general the implementation class
of a remote interface should at least –
Declare the remote interfaces being implemented.
Define the constructor for the remote object.
Provide an implementation for each remote method in
the remote interface.
The complete implementation of the Calculator server
follows the CalculatorImplement class which
implements the remote interface CalculatorInterface.
Code of CalculatorImplement
//CalculatorImplement.java
import java.rmi.*;
import java.rmi.server.*;
public class CalculatorImplement extends UnicastRemoteObject
implements CalculatorInterface {
public CalculatorImplement() throws RemoteException {
super();
}
public long addition(long a , long b) throws RemoteException {
return a + b;
}
public long subtraction(long a , long b) throws RemoteException {
return a - b;
}
public long multiplication(long a , long b) throws RemoteException {
return a * b;
}
public long division(long a , long b) throws RemoteException {
return a / b;
}
}
Developing Remote Server
//CalculatorServer.java
import java.rmi.*;
import java.net.*;
public class CalculatorServer {
public CalculatorServer() {
try {
CalculatorInterface calc = new
CalculatorImplement();
Naming.rebind("rmi://localhost:1099/CalculatorService",calc);
} catch(Exception e) {
System.out.println(“Trouble: “ + e.getMessage())
}
}
public static void main(String [] args) {
new CalculatorServer();
}
}
Developing Remote Client
//CalculatorClient.java
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
String calcServiceURL = “rmi://args[0]+”CalculatorService”’
CalculatorInterface ci = (CalculatorInterface)
Naming.lookup(calcServiceURL);
System.out.println(ci.addition(4,3));
System.out.println(ci.subtraction(4,3));
System.out.println(ci.division(4,3));
System.out.println(ci.multiplication(4,3));
} catch(MalformedException me) {
System.out.println(“Trouble: “ + me.getMessage())
}
(Contd.)
CalculatorClient (contd.)
  catch(RemoteException re) {
System.out.println(“Trouble: “ + re.getMessage())
}
catch(NotBoundException ne) {
System.out.println(“Trouble: “ + ne.getMessage())
}
catch(java.lang.ArithmaticException ae) {
System.out.println(“Trouble: “ + ae.getMessage())
}
}
}
Compilation of Programs
In order to get executable codes, first we need to compile all
the example programs. For this javac compiler can be
used. The files can be compiled like follows –
 Compilation of Implementation class,
CalculatorImplement.java
javac CalculatorImplement.java
 Generation of stub and skeleton class files from
implementation classes – we next use the RMI compiler,
rmic, to generate the stub and skeleton files. The compiler
runs on the remote service implementation class file –
rmic CalculatorImplement
After we run rmic we should find the
CalculatorInterface_Stub.class and if we are running the
Java 2 SDK, CalculatorInterface_Skel.class.
Compilation of Programs
Compilation of server program, CalculatorServer.java
Javac CalculatorServer.java
Compilation of Client program, CalculatorClient.java
javac CalculatorClient.java
After compilation of all the .java files one can find
following class files in the appropriate directory
 CalculatorInterface.class
 CalculatorImplement.class
 CalculatorInterface_Stub.class
 CalculatorInterface_Skel.class
 CalculatorServer.class
 CalculatorClient.class
Running the RMI System
1 – Starting RMI Registry
The RMI registry is a simple server-side bootstrap naming
facility that allows remote clients to get a reference to a
remote object. To start the registry on the server, we have
to execute the rmiregistry command. This command
produces no output and is typically run in the
background. We should run this command as follows –
start rmiregistry
If all goes well, the registry will start running and we can
switch to the next console.
By default, the registry run on port 1099. to start registry
on a different port, we have to specify it on the command
line, like –
start rmiregistry 2001
Running the RMI System
2 – Starting the Server
Once the registry is started, we can start the server.
First we need to make sure that both the server files,
that is, CalculatorServer.class and the remote object
implementation class, that is,
CalculatorImplement.class are in the same path. In
the second console we can start the CalculatorServer
java CalculatorServer
It will start, load the stub class dynamically into the
memory and wait for a client connection.
Running the RMI System
3 – Starting the Client
Once the registry and the server are running, we can start
client, java CalculatorClient 127.0.0.1
Here, 127.0.0.1 is the host name of the server, so that client
knows, where to locate the Calculator remote object. If all
goes well, we will see the following output –
1
7
12
1.333
That’s it, we have created a working RMI system. Even
though we ran the three consoles on the same computer,
RMI uses our network stack and TCP/IP to communicate
between the three separate JVMs. This is a full-fledged
RMI system.

Das könnte Ihnen auch gefallen