1.先测试RMI服务的发布,测试代码如下:
//MyService.java: remote interface for RMI
package test.spring.remote.rmi;
public interface MyService extends java.rmi.Remot {
public void doSomething() throws java.rmi.RemoteException;
}
//MyBusinessInterface.java: My own business interface, must has the same methods as MyService
package test.spring.remote.rmi;
public interface MyBusinessInterface {
public void doSomething();
}
//MyServiceImpl.java: the service implement class
package test.spring.remote.rmi;
public class MyServiceImpl implements MyService, MyBusinessInterface {
public void doSomething() {
System.out.println("MyServiceImpl.doSomething()");
}
}
Spring的context配置文件如下:
//spring-remote.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="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyService</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
</beans>
再写一个测试程序,如下:
//TestSpringRmi.java
package test.spring.remote.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringRmi {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");
}
}
运行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
at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:98)
at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:55)
at sun.rmi.server.UnicastServerRef.setSkeleton(UnicastServerRef.java:179)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:142)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:129)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:275)
at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:178)
at org.springframework.remoting.rmi.RmiServiceExporter.afterPropertiesSet(RmiServiceExporter.java:244)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1058)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:363)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:275)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:318)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:81)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:57)
at test.spring.remote.rmi.TestClient.main(TestClient.java:14)
Caused by: java.lang.ClassNotFoundException: test.spring.remote.rmi.MyServiceImpl_Stub
at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at sun.rmi.server.RemoteProxy.loadClassFromClass(RemoteProxy.java:191)
at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:93)
... 17 more
Exception in thread "main"
咦?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配置文件:
//spring-remote.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="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<!-- <value>test.spring.remote.rmi.MyService</value> -->
<value>test.spring.remote.rmi.MyBusinessInterface</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
</beans>
再运行TestSpringRmi,成功了。console打印:
03-02 14:51:56 INFO [RmiServiceExporter.java:236] Binding RMI service 'myService' to registry at port '1199'
2.再继续测试客户端调用,先修改context配置如下:
//spring-remote.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="rmiService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyBusinessInterface</value>
</property>
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
</bean>
<bean id="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<!-- <value>test.spring.remote.rmi.MyService</value> -->
<value>test.spring.remote.rmi.MyBusinessInterface</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
</beans>
再修改测试代码,添加客户端调用:
//TestSpringRmi.java
package test.spring.remote.rmi;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringRmi {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-remote.xml");
MyBusinessInterface service = (MyBusinessInterface) context.getBean("rmiService");
service.doSomething();
}
}
运行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
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:567)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:313)
at sun.rmi.registry.RegistryImpl_Stub.lookup(Unknown Source)
at java.rmi.Naming.lookup(Naming.java:84)
at org.springframework.remoting.rmi.RmiClientInterceptor.lookupStub(RmiClientInterceptor.java:156)
at org.springframework.remoting.rmi.RmiClientInterceptor.prepare(RmiClientInterceptor.java:125)
at org.springframework.remoting.rmi.RmiClientInterceptor.afterPropertiesSet(RmiClientInterceptor.java:110)
at org.springframework.remoting.rmi.RmiProxyFactoryBean.afterPropertiesSet(RmiProxyFactoryBean.java:66)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1058)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:363)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:226)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:269)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:318)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:81)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:66)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:57)
at test.spring.remote.rmi.TestClient.main(TestClient.java:14)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:305)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:171)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:158)
at java.net.Socket.connect(Socket.java:452)
at java.net.Socket.connect(Socket.java:402)
at java.net.Socket.<init>(Socket.java:309)
at java.net.Socket.<init>(Socket.java:124)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:562)
... 19 more
Exception in thread "main"
仔细检查,原来自己把生成rmi客户端的bean映射放到了发布rmi服务的serviceExporter之前了,调换一下顺序:
//spring-remote.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="myService" class="test.spring.remote.rmi.MyServiceImpl" />
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName">
<value>myService</value>
</property>
<property name="service">
<ref bean="myService" />
</property>
<property name="serviceInterface">
<!-- <value>test.spring.remote.rmi.MyService</value> -->
<value>test.spring.remote.rmi.MyBusinessInterface</value>
</property>
<property name="registryPort">
<value>1199</value>
</property>
</bean>
<bean id="rmiService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceInterface">
<value>test.spring.remote.rmi.MyBusinessInterface</value>
</property>
<property name="serviceUrl">
<value>rmi://localhost:1199/myService</value>
</property>
</bean>
</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等注册服务性质的应用,同一应用中的客户端要根据其依赖性调整配置顺序。