Free WebSchools.com

Google




Blog ForumsLive chat WebhostingLink TO UsLink TO UsSEO Tools


HOME

J2ee tutorials

Java introduction
Java basic
Java installation
Java Packages
Learn Applets
Java Threads
Java Gui component
pixels


Java Event Handaling
Java Streams and Files
pixels
Java Swings
Java JDBC
pixels
Java Network Programming
Java RMI
pixels
Java Servlets
Java javabeans
pixels
EJB
Struts
pixels


java RMI introduction



Remote method invocation (RMI) allows a java object that executes on one machine to invoke a method of a java object that execute on another machine. In this way, RMI supports distributed applications. In distribution applications, an application is executed across multiple host systems. Object executing on one host system can invoke the methods on remote hosts. The remotely invoked methods can return values to the local objects.



The transfer of data is one of the most important processes in the distributed applications. The default implementation of the message passing method in java transfer data from the calling object to the called object within a single java virtual machine (JVM) One way to transfer the data from one host to another is using sockets. Another way to achieve the same is using the remote method invocation (RMI) which allows object in different hosts to send and receive message. Both the methods achieve the same goal. However the method call approach used by RMI is much easier to use.

What is RMI?



RMI stands for Remote Method Invocation, away for inter-process communication between java virtual machines. RMI is a pure java answer to remote procedure calls (RPC) , and in some sense to the distributed components object model(DCOM) and the common request broker architecture(CORBA) . It is built upon the specification of how local and remote objects interoperate. Local objects are objects that execute on a local machine whereas remote objects are objects that execute on all the other machines. Objects on remote machine are exported so that they can be invoked remotely. An object exports itself with a remote registry server.

Working of an RMI applications

The working of RMI applications is based on accessing the remote object. A remote object can be defined as an object associated with methods hat can be called from another java virtual machine. In this mechanism the application that access to the remote object is treated as client application and the application that implements that object is treated as a server application. Thus an RMI application can be satirized into two parts.

Server side application
Client side application




Server side application

: on server side 3 files are created

The first file is an interface that declares the methods, which are accessed remotely. The purpose of making an interface is to enable the client to access the methods of an interface with the help of its reference. The second file creates a class and the binds that object in RMI registry. An RMI registry is the naming server, which allows the remote request to be redirected to an object, which is bind inside it.

Client side application

On a client side, a file is created that creates a class access that remote object through RMI registry and invokes the remote methods.

Creating RMI server

The first side to create an RMI application is to create server side application. For server side application, the first thing to create is an interface that defines the methods, which are accessed by the remote clients. The code for defining an interface is given below which is the first step towards creating server application.

Step 1 defining a remote interface

To call the methods of an object remotely, you have to create a remote interface that should extend the java.rmi.Remote interface. This is the first steps in creating an RMI application, which extends the Remote interface. All the method declares in this interface can be called remotely. Each and every methods declares in the remote interface must have the java.rmi.RemoteException in the throws clause as they throw this exception whenever there is an error in sending or receiving information. The following listings is a sample

remote interface
import java.rmi.*;
import java.rmi.server.*;
Public interface int extends remote
{
public int add(int a,int b)throws RemoteException;
public int sub(int a1,int b1) throws RemoteException;
}

Implementation of an interface

In this step the interface created in step 1 is implemented. The calss, which implements the interface, must extend java.rmi.server.unicastRemoteObject The purpose of an extending the UnicasRemoteObject class is to provide the functionality to import an object from a remote machines. The code for implementation of an interface is given below
import java.rmi.*;
import java.rmi.server.*;
Public class serverIMPL extends UnicastRemoteObject implements intf
{
public serverIMPL() throws RemoteException
{
}
public int ass(int a,int b) throws RemoteException
{
return(a+b);
}
public int sub(int a1,int b1) throws RemoteException
{
return(a1-b1);
}
}

Step 3 creating server class

This step contains the main program for the server machine by registering the object of the class, which implements the interface. This is done through bind() and rebind() method found in java.rmi.Naming calss Bind() and rebind() are static methods found in the naming class In the code given code given below the method rebind() has been used
import java.rmi.*;
import java.rmi.server.*;
public class server
{
public static void main (String s[]) THROWS Exception
{
serverimpl impl=new serverIMPL()
System.out.printl(“initializing server…………………………….”);
Naming.rebind(“ram”, impl);
System.out.println(“registered………”);
}
}
Creating client

The next step is creating an RMI application is to create a client that will actually invoke the remoter methods. In this program, you will request a reference of that object which is present in the registry. This is done through the method lookup() which is present inside the calss java.rmi.Naming. The method lookup() is a static method, which takes the RMI URL that points to the registry as a parameter and ,if it finds the object, will return a reference to it. The code for creating the clients is given below
Import java.rmi.*;
Import java.rmi.server.*;
Public class client
{
public static void main (String s[]) throws Exception
{
String url=”rmi://127.0.0.1/ram”;
Int f  intf1=(intf)Naming.llokup(url);
Int a =intf1.add(10,20);
Int b=intf1.sub(30,10);
System.out.println(a);
System.out.println(b);
}
}
Configuring and executing client and server

To execute the codes written while creating server and client, you have to follow certain steps Note before following the steps given below you have to set the classpath to crrent working directory

compile all the four source file i.e intf.java, serversimple.java, sever.java and client.java

step2 the next step is to create the stub and skeleton of the serveIMPL for the creation of the RMOI server. For this purpose you have to use the rmic compiler rmic serverIMPL

The rmic command will create the following 2 file

ServerIMPL_Skel.class
severIMPL_stup.class


step3: start the rmiregistry by typing command rmiregistry in the command prompt of the server machine


c:\rmiexample>rmiregistry

Inform Friend About This Site

Your Name:

Friend's Name:

Friend's Email: