| | |
|
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,
| | |
| Are you interested in solving a very interesting Technology Stack while Playing this Game 
|
|
| Home >>> Coding Tips on Java >>> RMI Stub and Skeleton >>> Using Java Technology |
|
|
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
|
|
|
|
|
<- requires login | Log in or Register | |
Copyright © 2008-2009, Interview-Questions-Tips-Forum, All Rights Reserved. | CONTACT PRIVACY POLICY DISCLAIMER |
 |
Terms of Use and Disclaimer :
This web site provides some of the information about various technologies, example
code, tips, tutorials etc. Like any printed materials, content of these pages may
become out of date over a period of time. Therefore all visitor/users of this web
site are requested/advised to refer to the originating parties/sources for the
latest changes and happenings for detailed information. This information is not
intended to be a substitute for the original reference provided by the originating
parties/sources.
By accessing and using this website in any ways, including, without
limitation, browsing the website pages, using any information, using any content and/or
downloading any materials, you agree to and are bound by the terms of use
described in this page and Usage Terms and Conditions.
If you do not agree to all of
the terms and conditions contained in the terms of use described in this
page and Usage Terms and Conditions, do not use this
website in any manner. If you are using the website on behalf of your
employer, you represent that you are authorized to accept these Terms of Use
on your employer's behalf.
All Trademarks are property of their respective owner. Appropriate measure is being
taken for providing accurate and up-to-date information but like any printed materials,
these blog(s)/contents may eventually be outdated one day, so if you are using any
of these information, please refer original content/documentation from respective sources.
And under no circumstances shall the Author of these contents and/or this web site
be liable for any loss, damage, expense incurred or suffered which is claimed to have
occurred because of usage of the contents of this web site.
If you have any questions/queries/feedback/suggestions then please write to this web
site owner at contact.
|  |
|
|
|
|
|