0% found this document useful (0 votes)
9 views

Remote Method Invocation

Uploaded by

201118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Remote Method Invocation

Uploaded by

201118
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

REMOTE METHOD INVOCATION

Nandan Sharma Roll : 51


Batch : 3 B.E Comps

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.

Defining a remote interface

// Creating a Search interface


import java.rmi.*;
public interface Search extends Remote
{
// Declaring the method prototype
public String query(String search) throws RemoteException;
}

Implementing the remote interface

// Java program to implement the Search interface


import java.rmi.*;
import java.rmi.server.*;
public class SearchQuery extends UnicastRemoteObject
implements Search
{
// Default constructor to throw RemoteException
// from its parent constructor
SearchQuery() throws RemoteException
{
super();
}

// Implementation of the query interface


public String query(String search)
throws RemoteException
{
String result;
if (search.equals("Reflection in Java"))
result = "Found";
else
result = "Not Found";

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.

Need for Remote Method Invocation


Typically, distributed applications in Java need to locate a remote method. They also need to communicate
with remote objects and load the class definitions for these objects. RMI is required to satisfy all these
needs.As an API, RMI enables client and server communications over the internet, allowing for client
programs to send requests to a server program and the server program to respond appropriately to incoming
requests from client programs. With RMI, a Java programmer can create a publicly accessible remote server
object. This object facilitates seamless client-server communications through simple method calls on the

server object. Client programs can communicate directly with the server object and with each other using
a URL and HTTP.

How a Remote Method Invocation application works


Suppose a word processing program is installed on a server for enterprise use. An authorized user, or client,
on the network who wants to use the program double clicks on the icon on their desktop graphical user
interface (GUI). They may also access the program by typing the program name in a command line or
search box.
The client's invocation to the remote object on the server sends a request to the server. The stub receives
this request and passes it on to the client-side RRL. On receiving this request, the RRL invokes a method
and passes the request to the server-side RRL. This RRL then passes the request to the server's skeleton,
which invokes the required object on the server and then passes on the result to the requesting client. At
this point, the user can access and use the software.

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.

Java RMI Example

The is given the 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

You might also like