Support Me ��


SUBSCRIBE NOW

RMI JAVA


RMI JAVA 

RMI

Remote method invocation(RMI) allow a java object to invoke method on an object running on another machine. RMI provide remote communication between java program. RMI is used for building distributed application.

Concept of RMI application

A RMI application can be divided into two part,Client program and Server program. A Server program creates some remote object, make their references available for the client to invoke method on it. A Client program make request for remote objects on server and invoke method on them. Stub and Skeleton are two important object used for communication with remote object.

Stub and Skeleton

Stub act as a gateway for Client program. It resides on Client side and communicate with Skeleton object. It establish the connection between remote object and transmit request to it.

1. It initiates a connection with remote Virtual Machine (JVM),

2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),

3. It waits for the result

4. It reads (unmarshals) the return value or exception, and

5. It finally, returns the value to the caller.

Skeleton object resides on server program. It is responsible for passing request from Stub to remote object

1. It reads the parameter for the remote method.

2. It invokes the method on the actual remote object, and.

3. It writes and transmits (marshals) the result to the caller.

Creating a Simple RMI application involves following steps

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


Define a remote interface

A remote interface specifies the methods that can be invoked remotely by a client. Clients program communicate to remote interfaces, not to classes implementing it. To be a remote interface, a interface must extend the Remote interface of java.rmi package. There is only one method named add() and it declares RemoteException.

import java.rmi.*;
public interface AddServerInterface extends Remote
{
public int sum(int a,int b) throws RemoteException;
}

Implementation of remote interface

For implementation of remote interface, a class must either extend UnicastRemoteObject or use exportObject() method of UnicastRemoteObject class.

import java.rmi.*;
import java.rmi.server.*;
public class Adder extends UnicastRemoteObject implements AddServerInterface
{
Adder()throws RemoteException
{
super();
}
public int sum(int a,int b)
{
return a+b;
}
}


Create AddServer and host rmi service

You need to create a server application and host rmi service Adder in it. This is done using rebind() method of java.rmi.Naming class. rebind() method take two arguments, first represent the name of the object reference and second argument is reference to instance of Adder. 

import java.rmi.*;
import java.rmi.registry.*;
public class AddServer
{
public static void main(String args[])
{
try
{
AddServerInterface addService=new Adder();
Naming.rebind("AddService",addService);
//addService object is hosted with name AddService.
}
catch(Exception e)
{
System.out.println(e);
}
}
}


Create client application

Client application contains a java program that invokes the lookup() method of the Naming class. This method accepts one argument, the rmi URL and returns a reference to an object of type AddServerInterface. All remote method invocation is done on this object.

import java.rmi.*;
public class Client
{
public static void main(String args[])
{
try
{
AddServerInterface st=(AddServerInterface)Naming.lookup("rmi://"+args[0]+"/AddService");
System.out.println(st.sum(25,8));
}
catch(Exception e)
{
System.out.println(e);
}
}
}


Create client application

Save all the above java file into a directory and name it as "rmi".

1. compile all the java files
    javac *.java

2. Start RMI registry
    start rmiregistry

3. Run Server file
    java AddServer

4. Run Client file in another command prompt abd pass local host port number at run time
    java Client 127.0.0.1

Dinesh Kumar S is a 23-year-old System Administrator who enjoys playing games, listening to music and learning new technology. He is friendly and generous, but can also be very lazy and crazy.

Share this

Backlinks

Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website