Remote Method Invocation in Java
Remote Method Invocation in Java
Application Layer
Client
Server
Proxy Layer
Proxy Layer
Stub
Skeleton
TCP
JVM
JVM
It works with Java Virtual Machine & rmi system on client machine to
serialize any arguments to a remote method called & sends this
information to server machine.
- The stub receives any result from result method & returns to client.
b. Skeleton class- it is a server side proxy of remote object. The
responsibilities are- It receives the remote method called by any associated argument. It
works with JVM & rmi system on server machine to serialize any
arguments for this remote method call.
- It invokes the appropriate method in class using these argumentsRmis <classname>
-
3. Remote Reference Layer- It is efficient between stub & skeleton classes &
the transport layer, which is to handle the actual communication protocol.
- When transmitting the parameter or object through network it should be
in the form of string. JVM works with java byte code & gets stringoriented data from transport layer & give it to proxy layer & vice versa.
- This layer is use to handle replicated object & responsible for
establishing persistence & strategies for recovery of lost connection.
4. Transport Layer- responsible for handling actual machine-to-machine
communication (default is TCP/IP), it creates a stream that is accessed by
remote reference layer to send & receive data to & from the machine.
- It sets up, manages, & monitors connection on machine. The transport
layer can be modified to handle encrypted stream compression
algorithm & number of another security enhancement; hence this layer
is independent of another three layers.
RMI flow
When client needs to communicate with remote object or method the RMI or
system follows certain steps1. Server creates remote object
2. Server registers the object with RMI registry.
3. Client gives a request for object to the RMI registry. RMI registry
will return the remote interface to the stub.
4. Client invokes the stub method, which is proxy for the server.
Stub passes the request to skeleton.
5. Skeleton invokes the remote object method.
Methods1.
public static void bind(String URL, Remote Object) throws
AllreadyBoundException, RemoteException, UnknownHostException
- used to register an object with a specified registry. URL String contains
URL to registry server & object contains reference to an object which
should get registered.
2.
3.
4.
5.
-
2. java.rmi.server.*;
Class- UnicastRemoteObject
Public class UnicastRemoteObject
RemoteException
-
implements
RemoteServer
throws
Interface - RemoteServer
- Extends RemoteObject class, it provides framework to support wide
range of remote references
RMI Class loaderExtends object class, which provides static methods for loading classes
from a network location & obtain location from which an existing class can be
loaded.
RMI registryAllows remote clients to get reference to remote object.
Start rmiregistry- is an simple server to enable application to lookup object that
are exported for remote method invocation.
The registry keeps tracks or address of remote object which has unique name
& are be exported by their application.
Garbage collection
In a distributed system, just as in the local system, it is desirable to
automatically delete those remote objects that are no longer referenced by
any client. This frees the programmer from needing to keep track of the
remote objects' clients so that it can terminate appropriately. RMI uses a
reference-counting garbage collection algorithm similar to Modula-3's Network
Objects.
RMI uses a standard mechanism (employed in RPC systems) for
communicating with remote objects: stubs and skeletons. A stub for a remote
object acts as a client's local representative or proxy for the remote object.
The caller invokes a method on the local stub which is responsible for carrying
out the method call on the remote object. In RMI, a stub for a remote object
implements the same set of remote interfaces that a remote object
implements.
When a stub's method is invoked, it does the following:
1. initiates a connection with the remote JVM containing the remote object,
2. marshals (writes and transmits) the parameters to the remote JVM,
3. waits for the result of the method invocation,
4. unmarshals (reads) the return value or exception returned, and
5. returns the value to the caller.
The stub hides the serialization of parameters and the network-level
communication in order to present a simple invocation mechanism to the
caller.
In the remote JVM, each remote object may have a corresponding skeleton (in
Java 2 platform-only environments, skeletons are not required). The skeleton
is responsible for dispatching the call to the actual remote object
implementation.
When a skeleton receives an incoming method invocation it does the
following:
1. unmarshals (reads) the parameters for the remote method,
2. invokes the method on the actual remote object implementation, and
3. marshals (writes and transmits) the result (return value or exception) to
the caller.
In the Java 2 SDK, Standard Edition, v1.2 an additional stub protocol was
introduced that eliminates the need for skeletons in Java 2 platform-only
environments. Instead, generic code is used to carry out the duties performed
by skeletons in JDK1.1. Stubs and skeletons are generated by the rmic
compiler.
Example:
Step 1. All Remote interfaces must extends the Remote, which is a part
of java.rmi.Remote. All remote methods can throw a remote exception.
import java.rmi.*;
public interface helloinfo extends Remote
{
public void hello() throws RemoteException;
}
Step 2. Implements remote interface. All remote objects must extend
UnicastRemoteObject, which provides functionality that is needed to
make objects available from remote machines.
import java.rmi.*;
import java.rmi.server.*;
public class helloimpl extends UnicastRemoteObject implements helloinfo
{
public helloimpl() throws RemoteException
{ }
public void hello() throws RemoteException
{ System.out.println(hello & welcome); }
}
Step 3. Contains main program for server m/c. The main function is to
update RMI registry on that m/c & done by rebind() method of naming
class.
import java.rmi.*;
public class helloserver
{
public static void main(String args[])
{
try
{
helloimpl h=new helloimpl();
Naming.rebind(helloserver,h);
// or rmi://localhost/helloserver
}
catch(Exception e) { }
}
}
Step 4. Client implementation
import java.rmi.*;
public class helloclient
{
public static void main(String args[])
{
try
{
helloinfo h1=(helloinfo) Naming.lookup(helloserver);
h1.hello();
}
catch(Exception e) { }
}
}
To run the above example follow the steps:
Example: Take 2 float numbers from client, call a remote method add(),
display addition of numbers.
Step 1.
import java.rmi.*;
public interface addserverintf extends Remote
{
public double add(double d1, double2) throws RemoteException;
}
Step 2.
import java.rmi.*; import java.rmi.server.*;
public class addserverimpl extends UnicastRemoteObject implements
addserverintf
{
public addserverimpl() throws RemoteException
{ }
public double add(double d1, double d2) throws RemoteException
{ return d1+d2; }
}
Step 3.
import java.rmi.*; import java.net.*;
public class addserver
{
public static void main(String args[])
{
try
{
addserverimpl as1=new addserverimpl();
Naming.rebind(addserver,as1);
// or rmi://localhost/helloserver
}
catch(Exception e) { }
}
}
Step 4. Client implementation
import java.rmi.*;
public class addclient
{
public static void main(String args[])
{
try
{
addserverintf as2=(addserverintf) Naming.lookup (addserver);
System.out.println(first number:+args[0]);
double d1=Double.valueOf(args[0]).doubleValue();
System.out.println(Second number:+args[1]);
double d2=Double.valueOf(args[1]).doubleValue();
System.out.println(sum is:+as2.add(d1,d2));
}
catch(Exception e) { }
}
}