Write a program for remote method invocation in java with output
Answers
Answered by
0
Remote Method Invocation in Java
4.5
Remote Method Invocation (RMI) is an API which 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, 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.
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).
Stub Object
The stub object on the client machine builds an information block and sends this information to the server. The block consists of
An identifier of the remote object to be usedMethod name which is to be invokedParameters to the remote JVM
Skeleton Object
The skeleton object passes the request from the stub object to the remote object. It performs 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.
Steps to implement Interface
Defining a remote interfaceImplementing the remote interfaceCreating Stub and Skeleton objects from the implementation class using rmic (rmi complier)Start the rmiregistryCreate and execute the server application programCreate and execute the client application program.
Step 1: Defining the remote interface
The first thing to do is to create an interface which will provide the description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.
// Creating a Search interface
import java.rmi.*;
public interface Search extends Remote
// Declaring the method prototype
public String query(String search) throws RemoteException;
4.5
Remote Method Invocation (RMI) is an API which 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, 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.
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).
Stub Object
The stub object on the client machine builds an information block and sends this information to the server. The block consists of
An identifier of the remote object to be usedMethod name which is to be invokedParameters to the remote JVM
Skeleton Object
The skeleton object passes the request from the stub object to the remote object. It performs 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.
Steps to implement Interface
Defining a remote interfaceImplementing the remote interfaceCreating Stub and Skeleton objects from the implementation class using rmic (rmi complier)Start the rmiregistryCreate and execute the server application programCreate and execute the client application program.
Step 1: Defining the remote interface
The first thing to do is to create an interface which will provide the description of the methods that can be invoked by remote clients. This interface should extend the Remote interface and the method prototype within the interface should throw the RemoteException.
// Creating a Search interface
import java.rmi.*;
public interface Search extends Remote
// Declaring the method prototype
public String query(String search) throws RemoteException;
Similar questions
Science,
8 months ago
Business Studies,
8 months ago
Math,
8 months ago
Physics,
1 year ago
Science,
1 year ago