Unit 3
Unit 3
1. RMI – OVERVIEW
Remote Method Invocation (RMI) is a Java technology that allows an object in one JVM to invoke methods on an
object located in another JVM, even across different computers.
It follows a Client-Server Model, enabling distributed computing where different applications communicate over a
network.
The communication between client and server is handled by using two intermediate objects: Stub object (on client
side) and Skeleton object (on server-side) as also can be depicted from below diagram as follows,
At the server side, the packed parameters are unbundled and then the required method is invoked. This process is
known as unmarshalling.
1.4 RMI Registry
RMI registry is a namespace on which all server objects are placed. Each time the server creates an object, it registers
this object with the RMIregistry (using bind () or 1ebind() methods). These are registered using a unique name known
as bind name.
To invoke a remote object, the client needs a reference of that object. At that time, the client fetches the object from
the registry using its bind name (using lookup() method).
1.5 Goals of RMI
Following are the goals of RMI –
To minimize the complexity of the application.
To preserve type safety.
Distributed garbage collection.
Minimize the difference between working with local and remote objects.
Output
Calculator Server is Running...
Sum: 8
Difference: 5
RMI uses STUB and SKELETONS object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM.
STUB
The Stub is an object, acts as a gateway for the client’s side. The entire 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 objects, it does the
following task.
i) It initiates a connection with remote Virtual Machine (JVM).
ii) It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM.)
iii) It waits for the result.
iv) It reads (unmarshals) the return value or exception
v) It finally, returns the value to the caller.
SKELETONS
The Skeletons is an object, acts as a gateway for the server side object. All the incoming requests are routed through it.
When the Skeleton receives the incoming requests, it does the following tasks:
i) It reads the parameter for the remote method.
ii) It invokes the method on the actual remote objects.
iii) It writes and transmits (marshals) the result to the caller.
The original implementation depends on Java Virtual Machine, class representation mechanism and it thus only Supports
Making calls from one JVM to another. The protocol underlying this Java only implementation is known as JAVA
Remote Method Protocol (JRMP).
In order to support code running in a non-JVM context, programmers later developed a COBRA version. Usage
of the term RMI may denote solely the programming interface or may signify both the API and JRMP, IIOP or another
implementation, whereas the term RMI-IIOP specifically denotes the RMI interface delegating most of the functionality
to the supporting COBRA implementation.
The basic idea of the JAVA RMI, the distributed garbage-collection protocol, and much of the architecture underlying
the original sun implementation, come from the “network objects”.
For example, the reference can be obtained as a parameter to, or a return value from, another remote method call.
Once a remote object is registered in the rmiregistry, clients can:
obtain a remote object reference (e.g., by looking it up in the rmiregistry)
Remotely invoke methods on the object.
For example, the following code binds the name "HelloServer" to a reference for the remote object:
Naming.rebind("HelloServer", obj);
try
{
Hello obj = (Hello) Naming.lookup( "//" +
"lysander.cs.ucsb.edu" +
"/HelloServer"); //objectname in registry
System.out.println(obj.sayHello());
}
catch (Exception e)
{
System.out.println("HelloClient exception: " + e.getMessage());
e.printStackTrace();
}
}
}
set the security manager, so that the client can download the stub code.
Get a reference to the remote object implementation (advertised as "HelloServer") from the server host's rmiregistry.
Invoke the remote sayHello method on the server's remote object
The constructed URL-string that is passed as a parameter to the Naming.lookup method must include the server's
hostname.
@Override
public void sendUpdate(String message) throws RemoteException {
for (DataPushClient client : clients) {
client.receiveUpdate(message); // Push data to clients
}
}
}
@Override
public void init() throws ServletException {
try {
dataService = (DataPushService) Naming.lookup("//localhost/DataPushService");
} catch (Exception e) {
throw new ServletException("RMI lookup failed", e);
}
}
@Override
public void receiveUpdate(String message) throws RemoteException {
System.out.println("Received update: " + message);
}
}
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
ctx.rebind("CalculatorService", calcObj);
System.out.println("RMI-IIOP Server Started...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Running RMI-IIOP
1. Start the CORBA Naming Service:
orbd -ORBInitialPort 1050