rmi of spring

基于Spring实现远程服务编程:
[urlhttp://www.51cto.com/art/200611/34262.htm][/url]


[size=medium]用Spring动态调用RMI远程对象 [/size]

// 不需要通过BeanFactory直接动态调用远程对象

DistributeCenterBO distributeCenterBO = null;
RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean();
rmiProxyFactoryBean.setServiceInterface(DistributeCenterBO.class);
rmiProxyFactoryBean.setServiceUrl(
"rmi://localhost:1199/DistributeCenterBO");
try {
rmiProxyFactoryBean.afterPropertiesSet(); //更改ServiceInterface或ServiceUrl之后必须调用该方法,来获取远程调用桩
} catch (Exception ex) {
}

if (rmiProxyFactoryBean.getObject() instanceof DistributeCenterBO) {
distributeCenterBO = (DistributeCenterBO)
rmiProxyFactoryBean.getObject();
distributeCenterBO.register(SubscriberImpl.subscriberId);
}


[size=medium]rmi和httpInvoker [/size]

对于富客户端来说,和服务器端的通讯有很多种方式,不过我一般用的就是rmi或者httpInvoker。
spring为多种远程调用都提供了包装:
一。对于RMI来说
1、服务器端: <bean class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName"><value>ExampleService</value></property>
<property name="service"><ref bean="exampleManager"/></property>
<property name="serviceInterface"><value>com.example.server.service.manager.base.IExampleManager</value></property>
<property name="registryPort"><value>777</value></property>
</bean>


Spring中合理的配置RMI:

因为RMI stub被连接到特定的端点,不仅仅是为每个调用打开一个给定的目标地址的连接,所以如果重新启动RMI端点主机的服务器,那么就需要重新注册这些stub,并且客户端需要再次查询它们。
虽然目标服务的重新注册在重新启动时通常会自动发生,不过此时客户端保持的stub将会变的陈旧,且客户端不会注意这些,除非他们再次尝试调用stub上的方法,而这也将throw一个连接失败的异常。
为了避免这种情形,Spring的RmiProxyFactoryBean提供了一个refreshStubOnConnectFailure的bean属性,如果调用失败,并且连接异常的话,将它设定为true来强制重新自动查询stub。
<bean id="reportService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>${showcasewiz.report.serviceurl}</value>
</property>
<property name="serviceInterface">
<value>com.meetexpo.showcase.backend.service.ReportService</value>
</property>
[color=blue] <property name="refreshStubOnConnectFailure">
<value>true</value>
</property>[/color]
</bean>
stub查询的另一个问题是,目标RMI服务器和RMI注册项在查询时要为可用的。如果客户端在服务器启动之前,尝试查询和缓存该服务stub,那么客户端的启动将会失败(即使还不需要该服务)。
为了能够惰性查询服务stub,设定RmiProxyFactoryBean的lookupStubOnStarup标志为false。然后在第一次访问时查询该stub,也就是说,当代理上的第一个方法被调用的时候去主动查询stub,同时被缓存。这也有一个缺点,就是直到第一次调用,否则无法确认目标服务是否实际存在。
<bean id="reportService"
class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>${showcasewiz.report.serviceurl}</value>
</property>
<property name="serviceInterface">
<value>com.meetexpo.showcase.backend.service.ReportService</value>
</property>
[color=blue] <property name="lookupStubOnStartup">
<value>false</value>
</property>
<property name="refreshStubOnConnectFailure">
<value>true</value>
</property>[/color]
</bean>

还有一个属性就是cacheStub,当它设置为false的时候,就完全避免了stub的缓存,但影响了性能。需要的时候还是可以试试。


这段spring的配置文件就定义了服务器端的一个bean,可以暴露给客户端通过RMI方式来访问了。
examleMaanger这个bean在实现时,完全不需要知道它自己有一天还会被通过rmi方式被远程访问。
2、客户端:
<bean id="cityService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl"><value>rmi://localhost:777/CityService</value></property>
<property name="serviceInterface"><value>com.example.server.service.manager.base.IExampleManager</value></property>
<property name="lookupStubOnStartup"><value>true</value></property>
<property name="cacheStub"><value>true</value></property>
</bean>这段spring的配置文件定义了客户端的一个bean,这样就可在客户端使用exampleManager了,就如同在本地使用一样,完全没有什么不同。


二。对于httpInvoker来说,其配置比rmi方式要麻烦一些,而且据说其效率也要比rmi方式差,不过这一点我到没有亲身证实过,只是听说而已。但是httpInvoker有一个优点却足以抵消其所有的缺点,那就是它是通过web的端口来访问的。这样,只要能够浏览页面,就能够进行远程调用,避免了rmi方式有时无法通过防火墙的问题。
1、服务器端:
httpInvoker需要web容器的支持,因此需要将服务器端程序部署到web容器内。
在web.xml文件中
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>remote</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remote</servlet-name>
<url-pattern>/remote/*</url-pattern>
</servlet-mapping>
注意第一行定义的listener一定要有,否则下面提到的remote-servlet.xml中要引用的bean就会无法找到。
我们定义了一个servlet,名字叫remote,因此在WEB-INF目录下我们建一个名字为remote-servlet.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 name="/exampleService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service"><ref bean="exampleManager"/></property>
<property name="serviceInterface">
<value>com.example.server.service.manager.IExampleManager</value>
</property>
</bean>
</beans>这样服务器端的配置就完成了。exampleManager这个bean被暴露给了客户端
2、客户端:
<bean id="exampleService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl">
<value>http://localhost:80/remote/exampleService</value>
</property>
<property name="serviceInterface">
<value>com.example.server.service.manager.IExampleManager</value>
</property>
</bean> OK,这样客户端的配置就完成了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值