Advertisement :
   Log In    OR    Register  
  Topics :  
RMI Example

Home >>> Spring Tutorials >>> Spring Remoting >>> Java RMI Example
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 : ISHTEK
Title :
Spring singleton
Description : Spring Container Example
More...


Written By : ISHTEK
Title :
Spring Features Updates News
Description : Bringing To Page
More...


Written By : ISHTEK
Title :
Spring Service
Description : Web Service Example
More...


Written By : ISHTEK
Title :
Spring WebService
Description : Example Code Discussed
More...


Written By : Amit
Title :
Spring Web MVC
Description : Example code discussed
More...


Written By : ISHTEK
Title :
Spring Remote Stateless SessionBean
Description : Calling SessionBean Example code
More...


Written By : ISHTEK
Title :
Spring Local Stateless SessionBean
Description : Calling Local SessionBean Example code
More...


Written By : guddu
Title :
Struts 2 Spring
Description : Integration Example and Details
More...

Tags/Keywords : Spring Remoting, Spring Remote, Spring, remote, Java RMI,Questions, Example, Code, Tutorial, Article
Author : Amit
Date (Year/Month/Date): 2009-07-11 SpringFramework Remoting with Java RMI Example

Please be informed that NONE of the design/code from this
page is claiming to be some sort of best practices and we DO NOT expect
any of our visitor/reader of this page to assume this as some sort of
best practice for any context and should not be using this 
as it is without appropriate evaluation to their, so to say, 
specific programming context.

This page intends only to provide bit and piece of known ways  for
doing some sort of example and may not be fit for any other purpose.

Tags : Spring RMI, Java RMI with SpringFramework, RMI Spring In this example we are going to see how to use API from SpringFramework along with RMI (Remote Method Invocation). This is an extension of an example that is already hosted on this site.
Advertisement :
Software environment used for this example: 1. Eclipse 3.2 2. JDK 1.5 3. SpringFramework 3.2 We have a Remote interface "ExampleRemote" and an implementation of this interface "ExampleRemoteObject". One difference is that this implementation class "ExampleRemoteObject" is not extending UnicastRemoteObject class from java.rmi package, but still it can be exported to the rmiregistry. Let us look at the basic classes, an Interface and an implementation class, as follows: ExampleRemote.java
package example;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface ExampleRemote extends Remote {
  public String test() throws RemoteException;
}
ExampleRemoteObject.java
package example;

import java.rmi.RemoteException;

public class ExampleRemoteObject implements ExampleRemote {

  public String test() throws RemoteException {
    System.out.println("inside remote method test...");
    return "from test method";
 }
}
The main program that uses applicationContext.xml file and SpringFramework API to load beans, as follows: TestClient.java
package example;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestClient {

  public TestClient() {
    try {
      ClassPathXmlApplicationContext ctx = 
	     new ClassPathXmlApplicationContext(
		                new String[] {"applicationContext.xml"});
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  /**
   * @param args
   */
  public static void main(String[] args) {
      new TestClient();
  }
}
Spring configuration file "applicationContext.xml" file, as under:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="rmiservice" class="example.ExampleRemoteObject">
  </bean>
  <bean id="rmiserviceexporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
    <property name="service" ref="rmiservice"/>
    <property name="registryHost" value="localhost"/>
    <property name="registryPort" value="1099"/>		
    <property name="serviceName" value="exampleremote"/>		
  </bean>
</beans>
Before running this TestClient, %JAVA_HOME%\bin\rmiregistry.exe file has to be running, and before running rmiregistry.exe, classpath should be set to include the class files example.ExampleRemote. As you might have noticed that the rmiserviceexporter bean is using RmiServiceExporter class from Spring Framework API, so this bean requires host, port and the service name along with the service implementing class/bean reference, as shown above. So basically we need three different command prompts, one for the rmiregistry the second one for the TestClient to bind this service to the RMI registry and the third one is another client (command line main program), that could use Spring API to call this service "exampleremote". This main program is as shown below: Client.java
package example;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;

import org.springframework.beans.BeansException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.remoting.rmi.RmiClientInterceptor;
import org.springframework.remoting.rmi.RmiProxyFactoryBean;

public class Client {
	
    public Client() {
      try {
        ClassPathXmlApplicationContext ctx = 
		                   new ClassPathXmlApplicationContext(
						   new String[] {"applicationContextClient.xml"});
        ExampleRemote exampleR = (ExampleRemote) (ctx.getBean("rmiClient"));
        System.out.println(exampleR.test());
      } catch (BeansException e) {
        e.printStackTrace();
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
    
    public static void main(String[] args) {
      new Client();
    }
}
The configuration XML file, the above program requires as follows: applicationContextClient.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="rmiClient" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
    <property name="serviceInterface" value="example.ExampleRemote"/>
    <property name="serviceUrl" value="rmi://localhost:1099/exampleremote"/>
  </bean>
</beans>
Advertisement :
This example uses Spring Remoting feature and API for using RMI from Java Technology. If you have any comments/questions/suggestions , please write those here in this Page. Thanks.
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).


	

Commented By ->
allen
I met a problem when run the client.
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
 Error creating bean with name 'object' defined in URL 
[file:D:/AllWorkspace/javaworkspace/RMIClient/src/applicationContext.xml]:
 Cannot resolve reference to bean 'accountService' while setting 
bean property 'accountService'; nested exception is org.springframework.beans.factory.BeanCreationExc
eption: Error creating bean with name 'accountService' defined in URL 
[file:D:/AllWorkspace/javaworkspace/RMIClient/src
/applicationContext.xml]: Invocation of init method failed; nested exception is 
org.springframework.remoting.RemoteLookupFailureException: 
Lookup of RMI stub failed; nested exception is 
java.rmi.UnmarshalException: error unmarshalling return; nested exception is: 
	java.lang.ClassNotFoundException: 
server.AccountService (no security manager: RMI 
class loader disabled)


So,could you send me a E-mail tell why ?
I aprrociate for your help.

Commented By ->
Amit
Hi Allen,

After reading your comment , I tried once again in my local
development environment with a newly created workspace from scratch.
I created all these files includig Java and XML files from this
example, but once thing I did is followed each an every steps
including followings:
1. before running rmiregistry, i checked which version JRE
is installed in my system,(as I am having JDK and JRE different
version in my environment), and the set appropriate classpath
to the folder having example.* class files.
And the started RMIRegistry.exe file from the correct JRE
version.

2. Setting up appropriate versions of required JAR files, such
as spring.jar, spring-remoting.jar, commons-logging.jar etc.

3. in another command prompt, appropriate class path to include
example folder and required JAR files are set, and then I executed example.TestClient and saw console output
showing
as successfully bind exampleremote.

4. Finally from Eclipse 3.2 workspace with appropriate JAR files 
set in the Java BUild Path->Libraries TAB, executed example.Client
to get appropriate message from server..

Now looking at your exception, I think, you may have to try
setting up classpath to the example.* class before running code. And see whether you are running rmiregistry
from the
JRE bin folder that is presently installed in Java environment in your system.

Sorry for typo mistakes here and there if any.
Please let me know if this works.
Are you interested in solving a very interesting Technology Stack while Playing this Game          

Please write your Comment on this Matter
(This will be visible if found suitable):
Name: *
Email (will not be displayed): *
Matter: *
12,36
Enter bigger number from above :*
Home >>> Spring Tutorials >>> Spring Remoting >>> Java RMI Example
Visitor/User referred related external URL:
(Visible upon review and approved by this site Administrator)
Referred By Name *:
Resource URL *: (e.g, URL should be starting with http://www.-----.---)
 
Resource Short Description *:
13,11
Enter bigger number from above : *

Please log in to add or reply to any matter<- 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.