外出旅行、冬季保暖得常备户外袜、速干袜、加厚袜子哦。

猛戳乐途驿站http://zhoupa1188.taobao.com抢购品牌男女式加厚户外袜子,coolmax、全棉、保暖、吸汗、速干、登山、徒步袜子。满10包邮


谢炜的cnblogs

CSDN上比较完整:http://hi.csdn.net/xiefeifeihu

导航

RMI发布多个服务(接口)

在使用Spring RMI时可能会遇到发布多个服务的需求,假设一个服务要操作文件,另一个服务要往屏幕上打印“hello world”。难道要把这两个风马牛不相及的服务写在一个接口里吗!如果真这样那结构也太差了。

网上有人问这个问题:“http://topic.csdn.net/u/20090702/11/9bcf60e5-f3f8-4401-a41d-2f98535988b9.html”,看样子这个问题难住了一些人,因为他们想在一个serviceName里面发布多个serviceInterface。其实转换个思维也就不会被难倒了。倘若在一个serviceName里面发布多个serviceInterface,那么service又该怎么配呢,难道用个list将serviceInterface和service对应起来?显然是行不通的。

解决办法很简单:配置多个RmiServiceExporter的bean,使不同的服务(属性:service)用不同的服务名(属性:serviceName)和接口(属性:serviceInterface)。给个demo就更清晰了:

 1:  <bean id="oper1" class="***.Oper1">bean>
 2:  <bean id="rmiService_oper1" class="org.springframework.remoting.rmi.RmiServiceExporter">
 3:      <property name="serviceName">
 4:          <value>oper1value>        
 5:      property>
 6:      <property name="service">
 7:          <ref local="oper1" />
 8:      property>
 9:      <property name="serviceInterface">
10:          <value>***.IOper1value>
11:      property>
12:      <property name="registryPort">
13:          <value>9990value>        
14:      property>
15:  bean>
16:  
17:  <bean id="oper2" class="***.Oper2">bean>
18:  <bean id="rmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">
19:      <property name="serviceName">
20:          <value>oper2value>        
21:      property>
22:      <property name="service">
23:          <ref local="oper2" />
24:      property>
25:      <property name="serviceInterface">
26:          <value>***.IOper2value>
27:      property>
28:      <property name="registryPort">
29:          <value>9990value>        
30:      property>
31:  bean>

service、serviceName和serviceInterface都不一样,端口可以相同。

客户端的配置也很简单了:

 1:  <bean id="rmiClient_oper1" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
 2:      <property name="serviceUrl">
 3:          <value>rmi://192.168.0.***:9990/oper1value>
 4:      property>
 5:      <property name="serviceInterface">
 6:          <value>***.IOper1value>
 7:      property>
 8:  bean>
 9:  <bean id="rmiClient_oper2" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
10:      <property name="serviceUrl">
11:          <value>rmi://192.168.0.***:9990/oper2value>
12:      property>
13:      <property name="serviceInterface">
14:          <value>***.IOper2value>
15:      property>
16:  bean>

main里运行:

1:  public static void main(String[] args) {
2:      ApplicationContext ctx = new ClassPathXmlApplicationContext(
3:              "applicationContext-client.xml");
4:      IOper1 o1 = (IOper1) ctx.getBean("rmiClient_oper1");
5:      o1.exeuteFile();
6:   
7:      IOper2 o2 = (IOper2) ctx.getBean("rmiClient_oper2");
8:      o2.printHelloWorld();
9:  }

这主要是个思路问题,没什么巧的。

posted on 2009-11-25 16:30  飞飞狐  阅读(739)  评论(0编辑  收藏  举报

外出旅行、冬季保暖得常备户外袜、速干袜、加厚袜子哦。

猛戳乐途驿站http://zhoupa1188.taobao.com抢购品牌男女式加厚户外袜子,coolmax、全棉、保暖、吸汗、速干、登山、徒步袜子。满10包邮