在Spring整合Rmi中:

服务端使用了org.springframework.remoting.rmi.RmiServiceExporter

RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。

客户端使用了org.springframework.remoting.rmi.RmiProxyFactoryBean

客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。

通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。

 

下面给出简单例子

 

服务端程序:

新建接口:

 

  1. /**  
  2.  * IRmiServer.java  
  3.  * 版权所有(C) 2012   
  4.  * 创建:cuiran 2012-08-08 11:12:40  
  5.  */ 
  6. package com.cayden.rmi;  
  7.  
  8. /**  
  9.  * TODO  
  10.  * @author cuiran  
  11.  * @version TODO  
  12.  */ 
  13. public interface IRmiServer {  
  14.       
  15.     public boolean test();  
  16.  
  17. }  

再实现该接口的方法:

 

  1. /**  
  2.  * RmiServerImpl.java  
  3.  * 版权所有(C) 2012   
  4.  * 创建:cuiran 2012-08-08 11:13:24  
  5.  */ 
  6. package com.cayden.rmi.impl;  
  7.  
  8. import com.cayden.rmi.IRmiServer;  
  9.  
  10. /**  
  11.  * TODO  
  12.  * @author cuiran  
  13.  * @version TODO  
  14.  */ 
  15. public class RmiServerImpl implements IRmiServer {  
  16.  
  17.     /* (non-Javadoc)  
  18.      * @see com.cayden.rmi.IRmiServer#test()  
  19.      */ 
  20.     @Override 
  21.     public boolean test() {  
  22.       
  23.         System.out.println("调用了我--服务端 O(∩_∩)O哈!");  
  24.           
  25.         return true;  
  26.     }  
  27.  
  28. }  

在src下新建applicationContext.xml 配置文件

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd"> 
  3. <beans> 
  4.       
  5.         <!-- rmi --> 
  6.     <bean id="rmiService" class="com.cayden.rmi.impl.RmiServerImpl"> 
  7.     </bean> 
  8.  
  9.     <bean id="remoteRmiService" class="org.springframework.remoting.rmi.RmiServiceExporter"> 
  10.         <property name="serviceName"> 
  11.             <value>remoteService</value> 
  12.         </property> 
  13.         <property name="service" ref="rmiService" /> 
  14.         <property name="serviceInterface"> 
  15.             <value>com.cayden.rmi.IRmiServer</value> 
  16.         </property> 
  17.         <property name="registryPort"> 
  18.             <value>9400</value> 
  19.         </property> 
  20.         <property name="servicePort"> 
  21.             <value>9401</value> 
  22.         </property> 
  23.     </bean> 
  24. </beans> 

启动服务端的类【MainServer.java】

 

  1. package com.cayden.rmi;  
  2.  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. /**  
  6.  * MainServer.java  
  7.  * 版权所有(C) 2012   
  8.  * 创建:cuiran 2012-08-08 11:44:07  
  9.  */ 
  10.  
  11. /**  
  12.  * TODO  
  13.  * @author cuiran  
  14.  * @version TODO  
  15.  */ 
  16. public class MainServer {  
  17.  
  18.     /**  
  19.      * TODO  
  20.      * @param args  
  21.      */ 
  22.     public static void main(String[] args) {  
  23.         // TODO Auto-generated method stub  
  24.         System.out.println("rmi服务端启动");  
  25.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  26.           
  27.         System.out.println("rmi服务端启动完成。。。");  
  28.     }  
  29.  
  30. }  

客户端代码:

在客户端使用服务端的接口文件:

 

  1. /**  
  2.  * IRmiServer.java  
  3.  * 版权所有(C) 2012   
  4.  * 创建:cuiran 2012-08-08 11:12:40  
  5.  */ 
  6. package com.cayden.rmi;  
  7.  
  8. /**  
  9.  * TODO  
  10.  * @author cuiran  
  11.  * @version TODO  
  12.  */ 
  13. public interface IRmiServer {  
  14.       
  15.     public boolean test();  
  16.  
  17. }  

然后在src下新建【applicationContext.xml】

 

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd"> 
  3. <beans> 
  4.     <!-- rmi远程调用 --> 
  5.     <bean id="testRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> 
  6.         <property name="serviceUrl"> 
  7.             <value>rmi://127.0.0.1:9400/remoteService</value> 
  8.         </property> 
  9.         <property name="serviceInterface"> 
  10.             <value>com.cayden.rmi.IRmiServer</value> 
  11.         </property> 
  12.     </bean> 
  13. </beans> 

新建客户端的测试类【TestRmi.java】

 

  1. /**  
  2.  * TestRmi.java  
  3.  * 版权所有(C) 2012   
  4.  * 创建:cuiran 2012-08-08 11:38:06  
  5.  */ 
  6. package com.cayden.rmi.client;  
  7.  
  8. import org.springframework.context.ApplicationContext;  
  9. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  10.  
  11. import com.cayden.rmi.IRmiServer;  
  12.  
  13. /**  
  14.  * TODO  
  15.  * @author cuiran  
  16.  * @version TODO  
  17.  */ 
  18. public class TestRmi {  
  19.     public static void main(String[] arg) {  
  20.         System.out.println("rmi客户端开始调用");  
  21.         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
  22.         IRmiServer rmi=(IRmiServer)ctx.getBean("testRmiService");  
  23.         rmi.test();  
  24.         System.out.println("rmi客户端调用结束");  
  25.     }  
  26.  
  27. }  

最后控制台输出

服务端:

 

  1. rmi服务端启动    
  2. log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).    
  3. log4j:WARN Please initialize the log4j system properly.    
  4. rmi服务端启动完成。。。    
  5. 调用了我--服务端 O(∩_∩)O哈!    

客户端:

 

  1. rmi客户端开始调用    
  2. 2012-8-8 11:46:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh    
  3. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2]; startup date [Wed Aug 08 11:46:51 CST 2012]; root of context hierarchy    
  4. 2012-8-8 11:46:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions    
  5. 信息: Loading XML bean definitions from class path resource [applicationContext.xml]    
  6. 2012-8-8 11:46:51 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory    
  7. 信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1c29ab2]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1e0bc08    
  8. 2012-8-8 11:46:51 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons    
  9. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1e0bc08: defining beans [testRmiService]; root of factory hierarchy    
  10. rmi客户端调用结束