`
zcjl
  • 浏览: 41539 次
  • 性别: Icon_minigender_1
  • 来自: 70码之城
社区版块
存档分类
最新评论

Spring-remoting使用心得1-RMI

阅读更多

看了《J2EE without EJB》的remote章节,忍不住写点代码试试,看看Spring的实现到底多巧妙。

1.先测试RMI服务的发布,测试代码如下:

java 代码
  1. //MyService.java: remote interface for RMI   
  2. package test.spring.remote.rmi;   
  3.   
  4. public interface MyService extends java.rmi.Remot {   
  5.     public void doSomething() throws java.rmi.RemoteException;   
  6. }   
  7.   
  8.   
  9. //MyBusinessInterface.java: My own business interface, must has the same methods as MyService   
  10. package test.spring.remote.rmi;   
  11.   
  12. public interface MyBusinessInterface {   
  13.     public void doSomething();   
  14. }   
  15.   
  16.   
  17. //MyServiceImpl.java: the service implement class   
  18. package test.spring.remote.rmi;   
  19.   
  20. public class MyServiceImpl implements MyService, MyBusinessInterface {   
  21.     public void doSomething() {   
  22.         System.out.println("MyServiceImpl.doSomething()");   
  23.     }   
  24. }  


Spring的context配置文件如下:

xml 代码
  1. //spring-remote.xml   
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans>  
  7.     <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />  
  8.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  9.         <property name="serviceName">  
  10.             <value>myService</value>  
  11.         </property>  
  12.         <property name="service">  
  13.             <ref bean="myService" />  
  14.         </property>  
  15.         <property name="serviceInterface">  
  16.             <value>test.spring.remote.rmi.MyService</value>  
  17.         </property>  
  18.         <property name="registryPort">  
  19.             <value>1199</value>  
  20.         </property>  
  21.     </bean>  
  22. </beans>  


再写一个测试程序,如下:

java 代码
  1. //TestSpringRmi.java   
  2. package test.spring.remote.rmi;   
  3.   
  4. import org.springframework.context.ApplicationContext;   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  6.   
  7. public class TestSpringRmi {   
  8.     public static void main(String[] args) {   
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");   
  10.     }   
  11. }  


运行TestSpringRmi,报错如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serviceExporter' defined in class path resource [spring-remote.xml]: Initialization of bean failed; nested exception is java.rmi.StubNotFoundException: Stub class not found: test.spring.remote.rmi.MyServiceImpl_Stub; nested exception is:
 java.lang.ClassNotFoundException: test.spring.remote.rmi.MyServiceImpl_Stub
java.rmi.StubNotFoundException: Stub class not found: test.spring.remote.rmi.MyServiceImpl_Stub; nested exception is:
 java.lang.ClassNotFoundException: test.spring.remote.rmi.MyServiceImpl_Stub

咦?Spring不是号称不需要自己生成stub么?怎么会出现“Stub class not found”呢?
祭出google,从spring官方论坛搜到一个帖子:http://forum.springframework.org/showthread.php?t=19185,里面有条回复是:

I found the answer:
The class org.springframework.remoting.rmi.RmiInvocationWrap per_Stub is present in spring.jar, but not in the source tree as a Java file. Since I was running against the compiled Spring Java files, rather than the jar, it did not find it.

晕倒,Spring不会这么弱智吧,难道我以后使用的时候还得把jar包解压到class目录下?
不甘心,再搜,找到这个帖子:http://forum.springframework.org/showthread.php?t=12685

在Juergen Hoeller的回复提示下,我再去看了jpetstore的配置文件,原来用以发布rmi的接口应该是pojo形式的MyBusinessInterface,而不是那个继承自Remote的MyService,修改自己的context配置文件:

xml 代码
  1. //spring-remote.xml   
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans>  
  7.     <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />  
  8.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  9.         <property name="serviceName">  
  10.             <value>myService</value>  
  11.         </property>  
  12.         <property name="service">  
  13.             <ref bean="myService" />  
  14.         </property>  
  15.         <property name="serviceInterface">  
  16.             <!-- <value>test.spring.remote.rmi.MyService</value> -->  
  17.             <value>test.spring.remote.rmi.MyBusinessInterface</value>  
  18.         </property>  
  19.         <property name="registryPort">  
  20.             <value>1199</value>  
  21.         </property>  
  22.     </bean>  
  23. </beans>  

再运行TestSpringRmi,成功了。console打印:
03-02 14:51:56 INFO  [RmiServiceExporter.java:236] Binding RMI service 'myService' to registry at port '1199'


2.再继续测试客户端调用,先修改context配置如下:

xml 代码
  1. //spring-remote.xml   
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans>  
  7.     <bean id="rmiService"  
  8.         class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  9.         <property name="serviceInterface">  
  10.             <value>test.spring.remote.rmi.MyBusinessInterface</value>  
  11.         </property>  
  12.         <property name="serviceUrl">  
  13.             <value>rmi://localhost:1199/myService</value>  
  14.         </property>  
  15.     </bean>  
  16.     <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />  
  17.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  18.         <property name="serviceName">  
  19.             <value>myService</value>  
  20.         </property>  
  21.         <property name="service">  
  22.             <ref bean="myService" />  
  23.         </property>  
  24.         <property name="serviceInterface">  
  25.             <!-- <value>test.spring.remote.rmi.MyService</value> -->  
  26.             <value>test.spring.remote.rmi.MyBusinessInterface</value>  
  27.         </property>  
  28.         <property name="registryPort">  
  29.             <value>1199</value>  
  30.         </property>  
  31.     </bean>  
  32. </beans>  

再修改测试代码,添加客户端调用:

java 代码
  1. //TestSpringRmi.java   
  2. package test.spring.remote.rmi;   
  3.   
  4. import org.springframework.context.ApplicationContext;   
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  6.   
  7. public class TestSpringRmi {   
  8.     public static void main(String[] args) {   
  9.         ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");   
  10.         MyBusinessInterface service = (MyBusinessInterface) context.getBean("rmiService");   
  11.         service.doSomething();   
  12.     }   
  13. }  

运行TestSpringRmi,报错如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rmiService' defined in class path resource [spring-remote.xml]: Initialization of bean failed; nested exception is java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
 java.net.ConnectException: Connection refused: connect
java.rmi.ConnectException: Connection refused to host: localhost; nested exception is:
 java.net.ConnectException: Connection refused: connect

仔细检查,原来自己把生成rmi客户端的bean映射放到了发布rmi服务的serviceExporter之前了,调换一下顺序:

    xml 代码

  1. //spring-remote.xml   
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  4.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  5.   
  6. <beans>  
  7.     <bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />  
  8.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  9.         <property name="serviceName">  
  10.             <value>myService</value>  
  11.         </property>  
  12.         <property name="service">  
  13.             <ref bean="myService" />  
  14.         </property>  
  15.         <property name="serviceInterface">  
  16.             <!-- <value>test.spring.remote.rmi.MyService</value> -->  
  17.             <value>test.spring.remote.rmi.MyBusinessInterface</value>  
  18.         </property>  
  19.         <property name="registryPort">  
  20.             <value>1199</value>  
  21.         </property>  
  22.     </bean>  
  23.     <bean id="rmiService"  
  24.         class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  25.         <property name="serviceInterface">  
  26.             <value>test.spring.remote.rmi.MyBusinessInterface</value>  
  27.         </property>  
  28.         <property name="serviceUrl">  
  29.             <value>rmi://localhost:1199/myService</value>  
  30.         </property>  
  31.     </bean>  
  32. </beans>  

运行TestSpringRmi,结果如下:
03-02 15:01:24 INFO  [RmiServiceExporter.java:236] Binding RMI service 'myService' to registry at port '1199'
03-02 15:01:24 INFO  [RmiClientInterceptor.java:128] RMI stub [rmi://localhost:1199/myService] is an RMI invoker
MyServiceImpl.doSomething()


经过一番浅尝辄止,初步得出几个结论:
1.Spring对RMI的支持果然很不错,在Cglib等工具的支持下,使用RMI终于可以同Naming、rmic和stub告别了。
2.用以发布RMI的接口不能从java.rmi.Remote继承而来,否则就会出现“Stub class not found”的错误,原因有待深究。
3.Spring的BeanFactory创建bean实例是有序的,向RMI、JNDI、WebService等注册服务性质的应用,同一应用中的客户端要根据其依赖性调整配置顺序。

 

分享到:
评论
3 楼 q6823221 2014-09-24  
谢谢,帮忙找到了问题
2 楼 至尊宝_唯一 2014-04-01  
对于结论的第三点,Spring创建bean不是放在map中的吗?为什么说是有序的呢
1 楼 wyq_tomorrow 2008-12-25  
谢谢,学习了

相关推荐

    spring-remoting.jar

    spring-remoting.jar spring-remoting.jar

    flex-messaging-remoting.jar

    flex-messaging-remoting.jarflex-messaging-remoting.jarflex-messaging-remoting.jarflex-messaging-remoting.jar

    spring remoting的rmi服务2.5版本和3.0版本不兼容解决办法

    由于spring2和spring3的rmi方式调用方式不同引起的,通过查阅相关文档后发现,spring3不在需要生成skeleton和stub了,所以把这个类从spring-context中删除了,解决办法就是想办法将它再加进来

    shale-remoting jar

    shale-remoting 1.0.4

    spring-security Spring Security-3.0.1 中文官方文档

    spring-security-remoting-3.1.4.RELEASE spring-security-taglibs-3.1.4.RELEASE spring-security-web-3.1.4.RELEASE 这些jar包都是通过Maven下载下来的。 还有Spring Security-3.0.1 中文官方文档

    alibaba_dubbox_2.8.4.zip[jar、pom]

    dubbo-remoting-grizzly dubbo-remoting-http dubbo-remoting-mina dubbo-remoting-netty dubbo-remoting-p2p dubbo-remoting-zookeeper dubbo-rpc dubbo-rpc-api dubbo-rpc-default dubbo-rpc-hessian dubbo-rpc-...

    spring remoting

    NULL 博文链接:https://zcmerjade.iteye.com/blog/1476218

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    11) spring -remoting.jar需spring-core.jar,spring-beans.jar,spring-aop.jar,spring- dao.jar,spring-context.jar,spring-web.jar,spring-webmvc.jar 12) spring-support.jar需spring-core.jar,spring-...

    spring jar 包详解

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    spring-framework完整源代码(spring框架源码)

    spring-framework完整源代码(spring框架源码) 完整的Spring工程源码,工程内内包括spring各模块源码 以下为spring工程源码包结构: spring-src org.springframework aop,beans,cache,context,core,dao,ejb,...

    spring-web-2.5.jar

    org.springframework.remoting.caucho.Hessian1SkeletonInvoker.class org.springframework.remoting.caucho.Hessian2SkeletonInvoker.class org.springframework.remoting.caucho.HessianClientInterceptor.class ...

    spring jar资源包

    包括:spring-aop.jar,spring-beans.jar,spring-context.jar,spring-core.jar,spring-dao-2.0-m1.jar,spring-hibernate.jar,spring-jdbc.jar,spring-mock.jar,spring-orm.jar,spring-remoting.jar,...

    最新最全的spring开发包

    (1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工具类。 (2) spring-beans.jar 这个...

    JSF与Shale开发用包

    aop.jar spring-beans.jar spring-context.jar spring-core.jar spring-dao.jar spring-hibernate.jar spring-jdbc.jar spring-mock.jar spring-orm.jar spring-remoting.jar spring-...

    javaweb项目常用jar包

    spring-security-remoting-3.1.6.RELEASE.jar spring-security-taglibs-3.1.6.RELEASE.jar spring-security-web-3.1.6.RELEASE.jar spring-test-4.1.6.RELEASE.jar spring-tx-4.1.6.RELEASE.jar spring-web-...

    org.apache.cxf.spring.remoting.Jsr181HandlerMapping.jar

    org.apache.cxf.spring.remoting.Jsr181HandlerMapping.jar

    VB.NET---How-To-TCP-Remoting.rar_Remoting_VB.NET TCP_tcp vb.net_

    vb.net tcp 协议的使用(适用于初学者参考)

    自己弄的三层框架Spring.net,Remoting

    使用Spring.Net IoC注入数据访问层, 发布为Remoting服务, 供Web层调用. 3, Web 实现UI, 组织数据提交WebService处理. 层与层之间是独立的. 声明: 本框架仅做为个人研究使用, 若有任何问题或因使用产生的任何损失...

    dubbo-remoting-zookeeper-2.8.4.jar

    java运行依赖jar包

Global site tag (gtag.js) - Google Analytics