Tech I Enjoy Logo

Custom Search




  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,


1
some name


If anything missed out , please let me know at techienjoy at yahoo . com
RMI and Java Stub and Skeleton :
Example using RMI using Java Technology.
Java Reflection using Comparator :
Comparator using Reflection on Java Platform
and example discussed.
Interview Questions and answer of Java Technology :
Interview Questions and answer of Java Technology.
List of Examples on Java Platform :
List of Examples on Java Platform.
Example of Drag and Drop using Java Technology :
Example of Drag and Drop using Java Technology.
Image creation using Java AWT :
In-Memory Image creation using AWT on Java Platform.
Synchronized Block wait and notify With Example :
Example using wait and notify from within
synchronized block with code and explained
Event Handling using Java Technology :
Example on how to use Event
and handling code using Java Technology.
Example ThreadPoolExecutor using Java :
ThreadPoolExecutor Example using Java Technology.
Thread design scenario using Java :
Thread design scenario using Java Technology.
Thread Deadlock using Java :
Example reproducing a possible Thread 
using Java Technology.
Thread wait and notify With Example :
Example using wait and notify within Thread 
with code and explained


References :
Tags: java comparator reflection
Tags: java example drag n drop
Tags: Java Interview Questions
Tags: java rmi tutorial stub skeleton
Tags: Java Thread Deadlock
Tags: Java Thread Design Scenarios
Tags: Java threadpoolexecutor
Tags: Java



DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com. 
techienjoy.com is not liable for any negative consequences that may result/arise from 
implementing directly/indirectly any information covered in these pages/articles/tutorials.

All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.

In spite of all precautions taken to avoid any typo in these pages, there might be some 
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.



Android Examples || Android Examples

© Copyright 2010-2012, TECHIENJOY, All Rights Reserved.      Privacy Policy     Disclaimer & Terms & Conditions