Remote Method Invocation
Introduction:
RMI stands for remote method invocation .
It facilitates communiction between two remotely placed java objects and thus makes association of the inter-object.
The communication in RMI allows directly between server and client if and only if both are in java programming language,thus ,avoiding a middle layer of translation.
RMI applications are often comprised of two separate programs.One is server and another one is client.A typical server application creates some remote objects ,makes reference to them accessible ,and waits for clients to invoke methods on these remote objects.A typical client application gets a remote reference to one or more remote objects in the server and then invokes methods on them.
Between java Virtual Machines ,the RMI facilitates object function calls.Although on separate Computers JVM can invoke methods belonging to an object stored in JVM.One of the powerful feature is that methods can even pass objects that the foreign virtual machine has never encountered before,allowing dynamic loading of new classes as required.
Now,consider the following scenario......
Example
1..Developer A that performs some useful function writes a service.By adding new features and improving existing ones,he regularly updates this service.
2..Developer B wishes to use the service provided by Developer A.However ,it's inconvenient for A to supply B with update every time.
Developer B can let RMI handle updates automatically for him,because RMI can dynamically load new classes.Developer A places the new classes in a web directory ,where RMI can fetch new updates ,as they are required.
fig a
In fig,the connections made by the client while using RMI.Firstly,the client must contact an RMI registry,and then he has to request the name of the service.Developer B won't know the exact location of the RMI service,but he knows enough to contact Developer A's registry.This will point him in direction of the service he wants to call.
The service of Developer A's change regularly,therefore Developer B doesn't have copy of the class.Because the client automatically fetches the new subclass from a web server where the two developers share classes .Into the memory,the new class is loaded,and now the client is ready to use the new class.This happens transparently for De veloper B.To fetch the class,no extra code is needed to be written.
Implementing RMI
steps....
- The first step is to create an interface.
- The second step is to create a class that implements the interface.
- The third is to create a server that creates an instance of this class.
- The fourth step to create a client that connects to the server object using Naming.lookup().
- The fifth step is to compile these classes.
- The sixth step is to run the RMI interface compiler on the .class file of the implemention class.
- The seventh step is to start the RMIRegistry.
- The eigthth step is to start the server class
- The last one is to run the client program.
Note:
Here the RMI example is described by using Eclipse.
Client...
Calculator is the package name(Client side)
In that,
Calculator is interface.
CalculatorClient is client class .
Server...
CalculatorSer is the package name (Server Side)
In that,
Calculator is interface.
CalculatorImpl is the class which implements the Calculator interface.
CalculatorServer is the server class.
Step 1: Create an Interface
It defines the methods that will be available in class implements it. Their are two important things......
a.It extends java.rmi.Remote interface It marks itself as one methods that can be called from any virtual machine.Any object becomes a remote object that implements interface.
b.The method throws java.rmi.RemoteException During the remote method call,this exception is thrown by RMI system to indicate that either a communication failure or a protocol error has occured.A RemoteException is a checked Exception,therefore any code making a call to a remote method needs to handle this exception by either catching it or declaring it in its throws clause.
Step 2:Create A Class That Implements The Interface
The implementation class of a remote interface should at least perform following functions....
1.To declare the remote interfaces being implemented.
2.To define the constructor for remote object.
3.To provide an implementation for each remote method in the remote interfaces.
eg.
public CalculatorImpl() throws RemoteException
{
super();
}
This constructor simply calls the superclass constructor,which is no arguement constructor of the UnicastRemoteObject class.During construction,A UnicastRemoteObject is exported,which means that it is available to accept incoming requests by listening from clients on an anonymous port for incoming calls.The no-arguement constructor for the superclass known as UnicastRemoteObject declares the Exception RemoteException in its throws clause,the CalculatorImpl() constructor must throws RemoteException.If the attempt to export the object fails due to,then RemoteException can occur during construction.
Step 3.Create the Server That Creates an Instance Of "impl" Class
their are three steps::
1.Creating and Installing Security Manager
In the main() method creating and installing is the first step.Because of this,it protects acccess to system resources from entrusted downloaded code running within the virtual machine.Weather downloaded code has acccess to the local file system or can perform any other priviledged operations is determined by the security manager.All programs that use RMI must install the security manager,otherwise RMI will not download classes (other than from the local class path) for objects recieved as parameters,return values,or exceptions in remote method calls.The operations are performed by downloaded code go through a set of security checks is ensured by this restriction.
following is the code that create and install security manager...
if(System.getSecurityManager()==null)
{
System.setSecurityManager(new SecurityManager());
}
System.setSecurityManager(new SecurityManager());
}
Using RMI, applications can create remote objects that accept method calls from clients in other JVMs. In order, for a client to call methods on a remote object, the client must have a way to communicate with the remote object. Rather than having to program the client to speak the remote object's protocol, RMI uses special classes called stubs that can be downloaded to the client that are used to communicate with (make method calls on) the remote object. The
2.Making Remote Objects Available To Clients
java.rmi.server.codebase
property value represents one or more URL locations from which these stubs (and any classes needed by the stubs) can be downloaded.
eg. Calculator c = new CalculatorImpl();
In this,It exports newly created objects to the runtime.Once the export step is complete ,the addImpl remote object is ready to accept incoming calls from the client on the port.
3.The call to Naming.rebind(name,this);
java.rmi.Naming interface is used as a front-end API that performs various function such as binding,registering,and looking up remote objects in the registry.Once the remote object is registered with the RMI registry on the localhost,caller on any host can lookup the remote object by name,obtain its reference ,and then invoke remote methods on the objects.Its advantage is,the registry may be sharedby by all servers running on the host,or an indivisual server process may create and use its own registry.
eg.Naming.rebind(name,engine);
Naming.rebind("rmi://localhost:1099/RMIServer", c);
The first parameter:: is URL-formatted java.lang.String that represent the location and the name of the remote object.The requirement is to change the value of the remote object.The requirement is to change the value of host to be name,or IPaddress,of your server machine.If the host is ommited from the URL,then the host defaults to the localhost.Their is no need to specify a protocol in the URL.The default protocol is good to use because it provides a well known place to look for the remote objects that offer service on a particular host.
The second parameter::for the remote object reference specified by the arguement,the RMI runtime substitutes a reference to the stub.Remote implementation objects,never leave the VM where they are created.Therefore when a client performs a lookup in a server's remote object registry,a reference to stub is returned.Remote objects are passed by reference rather than pass by value.
Step 4.Create the Client That Connects To Server Object Using Naming.lookup()
1.The client first installs the RMISecurityManager then to get reference to the remote object uses static method Naming.lookup().The point to note is that the client is using the interface to hold the reference and method calls.The client uses the Naming.lookup() method to lookup the remote objects by name in the remote host's registry.At the time of doing the name lookup,the code creates URL that specifies the host where the server is running.The name passed in the
Naming.lookup call has the same URL syntax as the name passed in the Naming.rebind call.
Step 5.Start the Rmiregistry
1.we cant directly start rmiregistry.
2. First set the path on command prompt and then use command rmiregistry.
another method for adding Environmental method in Windows 7
a .From Desktop,Right click on Computer icon.
b .Choose Properties from the context menu.
c .Click Advanced System Setting Links.
d .Click Environmental Variables.In the section System Variables,find the path,Java_home ,class path environment variable and select it.Click Edit.If the path environment variable does not exist,click New.
e.In the Edit System Variable(or New System Variable)window,specify the value of the path environment varible.Click OK.Close all remaining windows by clicking OK.
Step 6.Start RMI Server