Home >> Java
RMI (Remote Method Invocation) - Java Core RMI concept.
This is an illustration of how to use RMI in form of a sample
example. RMI is useful in multi JVM or clustered environment, where
a single object is referenced from one or more JVMs.
Object of type Remote, are Marshalled and unmarshalled
and moved from JVM to JVM, in multiple server instances.
Let us examine a sample rmi example for understanding
how rmi functions. We require three Java files, such as
TestRemote.java, a remote interface, acts like an interface
to the client.
A TestRemoteObj.java, a Remote Object, that
provides implementation to the remote interface and is
serialized and used for storing into the registry.
We require a client I2WRemoteServer.java file as client
for this example.
Now one can rewrite or copy and create two java files,
as shown below:
TestRemote.java
/**
* This code is provided "AS IS", without warranty of any kind.
* This is a sample code, might be not completely error free.
* This site or author of this code doesn't take any responsibility
* what so ever resulting out of usage of this code.
*/
import java.rmi.*;
public interface TestRemote extends Remote
{
public void setName(String argName) throws RemoteException;
public String getName() throws RemoteException;
}
This is the interface that is of type Remote (extends java.rmi.Remote)
and has all the business methods implemented.
Now a Remote object TestRemoteObj.java, is implementing this
remote interface (TestRemote) and extends
java.rmi.server.UnicastRemoteObject
TestRemoteObj.java
/**
* This code is provided "AS IS", without warranty of any kind.
* This is a sample code, might be not completely error free.
* This site or author of this code doesn't take any responsibility
* what so ever resulting out of usage of this code.
*/
import java.rmi.*;
import java.rmi.server.*;
public class TestRemoteObj extends UnicastRemoteObject
implements TestRemote {
String name;
public TestRemoteObj() throws RemoteException
{
super();
}
public void setName(String argName)
{
name=argName;
}
public String getName()
{
return name;
}
}
This class implements all the business methods and is used
for binding to rmi registry purposes.
Now we require a client java file, namely I2WRemoteServer.java.
I2WRemoteServer.java
/**
* This code is provided "AS IS", without warranty of any kind.
* This is a sample code, might be not completely error free.
* This site or author of this code doesn't take any responsibility
* what so ever resulting out of usage of this code.
*/
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;
public class I2WRemoteServer
{
public I2WRemoteServer()
{
try{
Registry reg = LocateRegistry.getRegistry(9999);
TestRemoteObj testRemote = new TestRemoteObj();
testRemote.setName("some name");
reg.bind("testremote",testRemote);
System.out.println((reg.list()).length);
TestRemote testRemote1 = (TestRemote)reg
.lookup("testremote");
System.out.println(testRemote1.getName());
System.exit(0);
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String[] args)
{
new I2WRemoteServer();
}
}
This class is self explanatory, there is LocateRegistry class,
that find the registry at 9999 port.
By creating an object of TestRemoteObj, setting some initial
values for name and binding this to the registry and then,
looking up for the same object and printing out the value of
the name variable.
Before really executing the client, one has to compile all
these Java files, thereby creating STUB (represents server) and
skeleton (represents client), by using rmic.exe, as below:
rmic.exe TestRemoteObject
After successfully running rmic, one shall see two additional
class files are created,
namely "TestRemoteObj_Stub.class","TestRemoteObj_Skel.class".
If wanted to see the implementation of these two files,
use -keep option while running rmic , like as shown below:
rmic -keep TestRemoteObj
Now starting the rmiregistry with a port number 9999, by a command
rmiregistry 9999
Now opening up another command prompt and going to the directory
And setting appropriate classpath, (set classpath=.;%classpath%),
if not set already and then running the client (I2WRemoteServer.java)
one can see the following output,
If anything missed out , please let me know at
techienjoy at yahoo . com