Struts Tutorials:
Struts2 Tag CheckBoxList , Checkbox, Iterator, IF
Struts2 Tag Library Example
Struts2 Tiles Example
Struts2 Tiles I18N Example
Struts2 Questions
Struts Tiles I18N Example
Struts Eclipse MVC
Struts2 Tags
Struts2 Example and Tutorial
Struts MVC
Struts2 Validation
Hibernate Tutorials:
Hibernate Case Study
Class Hierarchy Persist Example
Using Hibernate Interceptor
Hibernate Questions with Answer
Hibernate Many-to-Many Mapping Example
Hibernate one-to-many Mapping Example
Hibernate and ORM tools
Spring Hibernate Example
Hibernate SessionFactory Example
Hibernate Mapping Class Hierarchy
Hibernate Questions
Hibernate SessionFactory Questions
Spring Hibernate Example:
Spring Hibernate Case Study
Written By : Amit
Title :
Composite keys in Hashtable
Description :
key and value pair More...
|
Written By : Amit
Title :
Customizing JTree using Swing
Description :
AWT using Java Technology More...
|
Written By : Amit
Title :
Event handling and Java Technology
Description :
Event handling and Java Technology More...
|
Written By : admin
Title :
Using Final Keyword
Description :
Example on using Final Keyword in Java code More...
|
Written By : Amit
Title :
In memory AWT Image Creation
Description :
Java Code More...
|
Written By : Amit
Title :
Java Integral Types
Description :
Java Types with Bit size More...
|
| Java RMI, RMI Example, RMI Code, Java RMI Example, Java Platform Author : Amit Date (Year/Month/Date): 2009-07-10
Java RMI Example with code explained | |
Tags: RMI example, RMI Code, Java RMI example
If you have started learning Java technology recently, then this example
could be very basic and preliminary help in understanding RMI using an
example code. If you already know RMI, then this example could serve you
with a refresh of this concept with very basic example code.
My Software environment for this example:
1. Eclipse 3.2
2. JDK 1.5
Idea here is to show way to call a remote method from a different JVM
from a command line based application.
So we have two command line based program, one program locates an already
running RMIRegistry with a port number 1099, and binds the remote object
(an object from a class that extends java.rmi.server.UnicastRemoteObject
and implements an interface that extends java.rmi.Remote).
And the other program locates an already running RMIRegistry and lookup
the already bound remote object.
So we have to define an interface that extends java.rmi.Remote
and a class that extends UnicastRemoteObject and implements this interface.
ExampleRemote.java
//This code is provided "AS IS"
package example;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ExampleRemote extends Remote {
public String test() throws RemoteException;
}
|
ExampleRemoteObject.java
//This code is provided "AS IS"
package example;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ExampleRemoteObject extends UnicastRemoteObject
implements ExampleRemote {
public ExampleRemoteObject() throws RemoteException {
super();
}
public String test() throws RemoteException {
System.out.println("inside test method in ExampleRemoteObject");
return "from test method";
}
}
|
Those two commandline Java programs as follows:
TestClient.java
//This code is provided "AS IS"
package example;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
public class TestClient {
public TestClient() {
try {
ExampleRemoteObject remoteObject = new ExampleRemoteObject();
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
registry.bind("exampleremote", remoteObject );
} catch (RemoteException e) {
e.printStackTrace();
} catch (AlreadyBoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TestClient();
}
}
|
The other program that should be called from another command prompt, this
way we can simulate environment with two different JVM and ClassLoader.
TestRMIClient.java
//This code is provided "AS IS"
package example;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
public class TestRMIClient {
public TestRMIClient() {
try {
Registry registry = LocateRegistry.getRegistry("localhost",1099);
ExampleRemote exampleR = (ExampleRemote) registry.lookup("exampleremote");
System.out.println(exampleR.test());
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TestRMIClient();
}
}
|
One question may you be thinking is what is this RMI registry that is located
in the client programs with a HOST address as localhost and port as 1099?
This is nothing but the rmiregistry.exe program found under %JAVA_HOME%\bin
folder.
One thing to remember is that, before running this rmiregistry.exe program,
classpath environment should be having the folder to the
example.ExampleRemote class, or else you might get exception such as
Caused by: java.lang.ClassNotFoundException: example.ExampleRemote
If everything setup properly, then one may run TestClient to have this program
stays running till the other client program, that is TestRMIClient is run,
in another command prompt, to receive system out as follows:
java TestClient
inside test method in ExampleRemoteObject
java TestRMIClient
from test method
This shows that TestRMIClient is able to invoke test method from the
other TestClient running program and able to receive "from test method"
returned value from the test method.
In my another example on RMI, I shall explore way to use Spring
Remoting feature with RMI. Please keeep reading my examples
and comment on this to improve my examples, and please let me know
if this really helped you with your understanding of the topic
on hand.
 | Author of this article/writeup has expressed his/her willingness |
to help or guide users with any technical difficulties he/she faces
while working with the example code environment setting up, running
and resolving any such exception raised during compile or at runtime.
You may ask for any technical doubt or seek technical help related to
this article by using following form to reach for technical help
from the Author for FREE.
This article's Author shall be reading your request and responding
within reasonable time (no resolution timeframe defined as such).
|
|