RMI

 

1. 发布RMI服务

在Spring配置文件中

 <!-- RMI配置参数-->
<bean id="rmiServerHost" factory-method="setProperty" class="java.lang.System" lazy-init="false">
<constructor-arg value="java.rmi.server.hostname"/>
<constructor-arg value="${ipAddress}"/>
</bean>

<!--
<bean factory-method="setProperty" class="java.lang.System" lazy-init="false">
<constructor-arg value="java.rmi.dgc.leaseValue"/>
<constructor-arg value="300000"/>
</bean>

<bean factory-method="setProperty" class="java.lang.System" lazy-init="false">
<constructor-arg value="java.rmi.dgc.gcInterval"/>
<constructor-arg value="900000"/>
</bean>
-->

<!--
接口IService
类Service实现IService  
-->

<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="Service" />      //服务接口的实现类 <property name="serviceName" value="MyService" />     //自定义服务名称 <property name="serviceInterface" value="IService" />   //服务接口 <property name="registryPort" value="9000" />      //端口    </bean>

 

 

2. 客户端调用


a.

public
static IService getService(String url){ try { RmiProxyFactoryBean rmiProxyFactoryBean = new RmiProxyFactoryBean(); rmiProxyFactoryBean.setServiceUrl(url); rmiProxyFactoryBean.setServiceInterface(IService.class); rmiProxyFactoryBean.setRefreshStubOnConnectFailure(true); rmiProxyFactoryBean.afterPropertiesSet(); IService service = (IService)rmiProxyFactoryBean.getObject();
return service;
}
catch (Exception e){ throw new RuntimeException("获取程序服务[" + url + "]出错", e); } }

//url的格式为 rmi://ip:port/ServiceName 如:rmi://127.0.0.1:9000/MyService

b.

在spring配置文件中
<bean id="service" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<!-- 接收的rmi协议 -->
<property name="serviceUrl" value="rmi://127.0.0.1:9000/MyService"/>
<!-- 接收的rmi协议的接口 -->
<property name="serviceInterface" value="IService"/>
</bean>

//注入到Test类中
<bean id="test" class="com.qinh.Test">
  <property name="service" ref="service"/>
</bean>





 

posted @ 2015-08-12 16:19  QinH  阅读(185)  评论(0编辑  收藏  举报