spring+RMI+hibernate发布多个服务(接口)思路,在配置的时候如果是两个服务,则发布两个RmiServiceExporter的bean。
客户端这边也在配置文件中设置两个bean定义RmiProxyFactoryBean。在调用的时候对不同的服务,使用不同的getBean。
例如:服务端的配置文件:
<!-- RMI service 01 -->
<bean id="logPersonService"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<!-- RmiServiceExporter 对服务名没有特殊要求 -->
<property name="serviceName"><value>LogPerson</value></property>
<property name="service"><ref bean="PersonTarget"/></property>
<property name="serviceInterface"><value>com.hibernate.ILogPerson</value></property>
<!-- 避免与默认的RMI注册端口冲突,因此修改为1200 -->
<property name="registryPort"> <value>1200</value> </property>
</bean>
<!-- RMI service 02 -->
<bean id="BayService"
class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName"><value>Bay</value></property>
<property name="service"><ref bean="BayTarget"/></property>
<property name="serviceInterface"><value>com.hibernate.IBay</value></property>
<property name="registryPort"> <value>1200</value> </property>
</bean>
客户端的配置文件:
<!-- RMI Client 01-->
<bean id="LogPerson"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl"><value>rmi://192.168.25.10:1200/LogPerson</value></property>
<property name="serviceInterface"><value>com.hibernate.ILogPerson</value></property>
</bean>
<!-- RMI Client 02-->
<bean id="Bay"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl"><value>rmi://192.168.25.10:1200/Bay</value></property>
<property name="serviceInterface"><value>com.hibernate.IBay</value></property>
</bean>
主程序中调用的方法:
//获得RMI服务
ILogPerson clientLog = (ILogPerson) cfactory.getBean("LogPerson");
IBay clientBay = (IBay) cfactory.getBean("Bay");
在做关联类的时候报错
问题:
Exception in thread "main" org.springframework.remoting.RemoteAccessException:
Could not access remote service [rmi://192.168.25.10:1199/BankService];
nested exception is java.rmi.MarshalException: error marshalling arguments;
nested exception is: java.io.NotSerializableException: springexample.hibernate.Account
at springexample.hibernate.TestClient.main(TestClient.java:40)
Caused by: java.rmi.MarshalException: error marshalling arguments; nested exception is:
Caused by: java.io.NotSerializableException: springexample.hibernate.Account
解决:
这个单词的意思居然没没明白marshalling arguments
就是用到的实体类没有从可以序列化类中继承过来啦!!!
转载于:https://blog.51cto.com/alinazh/1279507