Remote Method Invocation
Remote Method Invocation
INTRODUCTION
Remote Method Invocation (RMI) is an API that allows an object to invoke a method on an object that
exists in another address space, which could be on the same machine or on a remote machine. Through
RMI, an object running in a JVM present on a computer (Client-side) can invoke methods on an object
present in another JVM (Server-side). RMI creates a public remote server object that enables client and
server-side communications through simple method calls on the server object.An RMI request in Java is
a request to "invoke" the method of a remote object. It has the same syntax as a request to invoke an object
method in the same (local) computer. Objects can include information that will change the service
performed in the remote computer. Sun Microsystems, the inventors of Java, calls this behavior "moving
behavior" and the object parameter-passing mechanism "object serialization."
For example, when a user at a remote computer, A, fills out an expense account, the
Java program interacting with the user could communicate, using RMI, with a Java program in another
computer, B. Computer B always has the latest policy about expense reporting. In reply, the program in
Computer B would send back an object and associated method information that would enable Computer
A's program to screen the user's expense account data in a way consistent with the latest policy. If the policy
changes, a change would be required to a program in only one computer (Computer B).
Stub Object: The stub object on the client machine builds an information block and sends this
information to the server . The stub is an object, 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 .
The block consists of
An identifier of the remote object to be used
Method name which is to be invoked
Parameters to the remote JVM
Skeleton Object: The skeleton object passes the request from the stub object to the remote
object. It performs the following tasks
It calls the desired method on the real object present on the server.
It forwards the parameters received from the stub object to the method.
Architecture of an RMI Application.
In an RMI application, we write two programs, a server program (resides on the server) and
a client program (resides on the client).
A remote object is an object whose method can be invoked from another JVM.
Java RMI application contains two types of programs such as server program and client program.
In the server-side program, a remote object is created, and a reference of that remote object is made
available for the client-side using registry.
Working of RMI
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 media as follows:
These are the steps to be followed sequentially to implement Interface as defined below as follows:
1. Defining a remote interface
2. Implementing the remote interface
3. Creating Stub and Skeleton objects from the implementation class using rmic (RMI compiler)
4. Start the rmiregistry
5. Create and execute the server application program
6. Create and execute the client application program.
return result;
}
Marshalling and Unmarshalling
Whenever a client invokes a method that accepts parameters on a remote object, the parameters are bundled
into a message before being sent over the network. These parameters may be of primitive type or objects.
In case of primitive type, the parameters are put together and a header is attached to it. In case the parameters
are objects, then they are serialized. This process is known as marshalling.
At the server side, the packed parameters are unbundled and then the required method is invoked. This
process is known as unmarshalling.
server object. Client programs can communicate directly with the server object and with each other using
a URL and HTTP.
Minimize the difference between working with local and remote objects.