You are on page 1of 35

Remote Method Invocation

Chapter 7
• Consider the following program organization:

method call

SomeClass AnotherClass
returned object

computer 1 computer 2

• Is it possible to put the two classes on different


computers and make method call between the two
classes?
– RMI is one technology that makes this possible
2
In the Good Old Days...
Only local objects existed

My Machine

My Object
Today’s World...
Network and Distributed Objects

My Machine Remote
Machine
Local
Remote
Objects
Objects

A remote object in RMI is an object whose


method can be invoked from another JVM.
Understanding requirements for
the distributed applications
• If any application performs these tasks, it can
be distributed application.
1. The application need to locate the
remote method
2. It need to provide the communication with
the remote objects, and
3. The application need to load the
class definitions for the objects.
• The RMI application have all these features, so
it is called the distributed application.
Remote Method Invocation (RMI)
– Remote Method Invocation (RMI) allows a Java object
that executes on one machine to invoke a method of a
Java object that executes on another machine.
– This is an important feature, because it allows you to
build distributed applications.
– The Remote Method Invocation (RMI) is an API that
provides a mechanism to create distributed application
in java.
• The RMI allows an object to invoke methods on an object
running in another JVM.
• The RMI provides remote communication between the
applications.
The General Idea

•Instantiate an object on another machine


•Invoke methods on the remote object
Remote Method Invocation (RMI)
• RMI is used to connect together a client and
a server.
• RMI is used for remote communication between
Java application and components, both of which
must be written in Java Programming Language.
• RMI handles transmission of requests and
provides the facility to load the object’s bytecode,
which is referred to as dynamic code binding.
RMI Architecture
Stub
 The stub is an object, that acts as a gateway for the
client side. All the outgoing requests are routed
through it. It resides at the client side and represents
the remote object. When the caller invokes method on
the stub object, it does the following tasks:
It initiates a connection with remote
VirtualMachine (JVM),
It writes and transmits (marshals) the
parametersto the remote Virtual Machine
(JVM),
It waits for the result
It collects and reads (unmarshals) the return
valueor exception, and
It finally, returns the value to the caller.
Skeleton
• The skeleton is an object, that acts as a
gateway for the server side object. All the
incoming requests are routed through it. When
the skeleton receives the incoming request, it
does the following tasks:
1. It reads the parameter for the remote method.
2. It invokes the method on the actual
remote object, and
3. It writes and transmits (marshals) the result
to the caller.
• In the Java 2 SDK, a stub protocol was introduced
that eliminates the need for skeletons.
Components of Architecture
• Client - user interface
• Server - data source
• Stubs
– marshals argument data (serialization)
– unmarshals results data (deserialization)
• Skeletons (not required with Java 2)
– unmarshals argument data
– marshals results data
Marshalling Parameters

13
The Stub and Skeleton
call

skeleton
Stub
RMI Client RMI Server

return

– A client invokes a remote method, the call is first forwarded


to stub.
– The stub is responsible for sending the remote call over to
the server-side skeleton
– The stub opens a socket to the remote server, marshaling the
object parameters and forwarding the data stream to the
skeleton.
– A skeleton contains a method that receives the remote calls,
unmarshals the parameters, and invokes the actual remote
object implementation.
14
Components of RMI Architecture..cont
• Remote Reference Layer
– provides a RemoteRef object that represents the link to
the remote service implementation object.
– implements the remote object protocols
• Transport layer
– The Transport Layer makes the connection between JVMs.
All connections are stream-based network connections that
use TCP/IP.
– Take care of the underlying socket handling required
for communications
• sets up and maintains connections
• communications related error handling
Java Remote Method Protocol (JRMP)

• Proprietary, stream-based protocol that is


only partially specified is now in two versions
– JDK 1.1 version of RMI and required the use
of Skeleton classes on the server
– Java 2 SDK. It has been optimized for performance
and does not require skeleton classes
• some implementations, such as BEA
Weblogic don’t use JRMP instead use their
own wire level protocol
Steps to write the RMI program
6 steps to write the RMI program.
1. Create the remote interface
2. Provide the implementation of the
remote interface
3. Compile the implementation class and create the
stub and skeleton objects using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application
RMI Registry
• The RMI Registry is a naming service
provided with the JDK as a teaching tool or
for a small number of Remote Objects.
• The rmiregistry is a special server that looks
up objects by name
• Uses port 1099 as its default port
• Can be considered to be a
reference implementation
• runs on same machine as the remote object.
• rmic is a special compiler for creating stub
(client) and skeleton (server) classes
Example 1
Step 1: Create the Remote Interface
• In order to use a remote object, the client
must know its behavior (interface), but does
not need to know its implementation (class).
• In order to provide an object, the server must
know both its interface (behavior) and its
class (implementation).
• In short,
– The interface must be available to both client
and server
– The class should only be on the server
Step 1: Create the remote interface
For creating the remote interface,
extend the Remote interface and
declare the RemoteException with
//TestInterface.java all the methods of the remote
import java.rmi.Remote; interface.
import java.rmi.RemoteException;
public interface TestInterface extends
Remote {
public boolean isLoginValid(String username)
throws RemoteException;
}
Step 1: Create the remote interface
– By extending the java.rmi.Remote interface
• An object of a class that implements interface Remote
directly or indirectly is a remote object and can be
accesses from any JVM.

– Each method in Remote interface must throw


RemoteException
• Potential network errors
Step 1: contd........
The other information that is required by
server and client is remote object name or
reference and RMI port for communication.
– port number of registry on host (1099 default)
– Name of server object
• Used by clients to connect
public class Const {
public static final String RMI_ID="MyRMI";
public static final int RMI_PORT=1278;
}
Step 2: Provide the implementation of the remote
interface – Define the remote class –Known only to server

– An object of a class that implements interface


Remote directly or indirectly is a remote object
and can be accesses from any JVM.
– Must implement your remote interface
– Should extend java.rmi.server.UnicastRemoteObject
• Provides functionality for remote objects
• Constructor exports object so it can receive remote calls
– Wait for client on anonymous port number
• Subclass constructors must throw RemoteExceptions
Allows objects to be
import java.rmi.RemoteException; exported.
import java.rmi.server.UnicastRemoteObject;
public class TestImpl extends UnicastRemoteObject implements
TestInterface
{ protected TestImpl() throws RemoteException {
super();
Superclass constructor
} exports objects, and this
constructor must be able to
public boolean isLoginValid(String username) { throw
if(username.equals("test")) RemoteException.
return true;
else
return false;
}
}
Step 3: Create the stub using the rmic tool.

• C:>/rmic TestImpl

• The above command generates


TestImpl_Stub.class.
• From Java 6 onwards skeleton class is
not required.
Step 4: Start the registry service by the rmiregistry tool

• C:>/start rmiregistry
Step 6: Create the Remote Server &
Start the server
– Create the object of remote class.
– static method bind/rebind (class Naming)
• Binds object to rmiregistry
• Object named to be used by client
• rebind replaces any previous objects with
same name
– Method bind does not
Step 6: Create the Remote Server &
Start the server
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RMIServer1


{
public static void main(String[] args) throws RemoteException,
AlreadyBoundException
{
TestImpl impl=new TestImpl();
Registry registry=LocateRegistry.createRegistry(Const.RMI_PORT);
registry.bind(Const.RMI_ID, impl); System.out.println("server is
started");
}
}
Step 8: Write the remote client & Start the client

import java.rmi.NotBoundException;
Lookup remote object in
import java.rmi.RemoteException; registry. Returns
import java.rmi.registry.LocateRegistry; Remote reference, cast
to proper type.
import java.rmi.registry.Registry;
public class RemoteClient1 {
public static void main(String[] args) throws RemoteException,
NotBoundException {
Registry registry=LocateRegistry.getRegistry("localhost", Const.RMI_PORT);
TestInterface remote=(TestInterface)registry.lookup(Const.RMI_ID);
System.out.println(remote.isLoginValid("test"));
System.out.println(remote.isLoginValid(“dick"));
}
} Call like regular
method.
For running this RMI example,
1) compile all the java files

javac *.java
2)create stub object by rmic tool

rmic TestImpl
3)start rmi registry in one command prompt

start rmiregistry
4)start the server in another command prompt

java RMIServer1
5)start the client application in another command prompt

java RemoteClient1
Processes
– For RMI, you need to be running three processes
• The Client
• The Server
• The Object Registry, rmiregistry, which is like a DNS
service for objects
– You also need TCP/IP active

31
The General RMI Architecture
Remote Machine
– The server must first bind its
bind
name to the registry RMI Server

Registry
– The client lookup the server skeleton

name in the registry to


establish remote references. return call lookup

– The Stub serializing the stub

parameters to skeleton, the


skeleton invoking the remote RMI Client

method and serializing the Local Machine


result back to the stub.

32
What is needed for RMI
– To send a message to a remote “server object,”
• The “client object” has to find the object
– Do this by looking it up in a registry
• The client object then has to marshal the parameters (prepare
them for transmission)
– The server object has to unmarshal its parameters, do its
computation, and marshal its response
• The client object has to unmarshal the response
– Much of this is done for you by special software

33
Terminology
– A remote object is an object on another computer
– The client object is the object making the request (sending
a message to the other object)
– The server object is the object receiving the request
– As usual, “client” and “server” can easily trade roles (each
can make requests of the other)
– The rmiregistry is a special server that looks up objects by
name
– rmic is a special compiler for creating stub (client) and
skeleton (server) classes
34

You might also like