通过spring实现RMI的步骤

1. 编写服务器端接口INetCoverInfoRemoteService;(省级、集团级)
2. 实现INetCoverInfoRemoteService接口(省级)
3. 编写客户端接口NetCoverInfoRemoteService(集团级)
4. 实现NetCoverInfoRemoteService接口(集团级)
5. 在服务器端(省级服务器)配置spring(application-service.mxl)
<!--Rmi服务器端实现类-->
     <bean id="netCoverInfoRemoteService"class="cn.com.cncsi.product.ttms.common.rmi.server.impl.NetCoverInfoRemoteService">
<property name="opncis">
<ref bean="outPutNetCoverInfoService" />
</property>
</bean>


<!--Rmi服务端口-->
<bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
       <property name="port" value="8888"/>
    </bean>
     <!--Rmi服务设置-->
     <bean id = "serviceExporter" class = "org.springframework.remoting.rmi.RmiServiceExporter" >
        <property name = "serviceName" value = "RemoteService" />
        <property name = "service" ref = "netCoverInfoRemoteService" />
        <property name = "serviceInterface" value ="cn.com.cncsi.product.ttms.common.rmi.server.inter.INetCoverInfoRemoteService" />
        <property name="registry" ref="registry"/>
</bean >
6. 在服务器端(省级服务器)配置spring.xml
<!--Rmi客户端实现类-->
<bean id="netCoverInfoRemoteClient"
class="cn.com.cncsi.product.ttms.common.rmi.client.impl.NetCoverInfoRemoteClient">
<property name="opncis">
<ref bean="outPutNetCoverInfoService" />
</property>
</bean>
8.客户端远程调用
  (1)方法1
/**
* 根据各省服务器端访问地址ip,返回接口所实现的类对象
* @param serviceUrl 省服务器端访问地址ip
* @return service
*/
private INetCoverInfoRemoteService getNetCoverInfoRemoteService(
String serviceUrl) {
//创建代理工厂bean
   RmiProxyFactoryBean factory= new RmiProxyFactoryBean();
   //给代理工厂bean设置服务器接口
   factory.setServiceInterface(INetCoverInfoRemoteService.class);
   //设置工厂所要访问的地址Ip
   factory.setServiceUrl(serviceUrl);
   // XXX vincan: 解决重启 rmi 的服务器后会出现拒绝连接或找不到服务对象的错误
   factory.setLookupStubOnStartup(false );
   factory.setRefreshStubOnConnectFailure(true );
   //访问新的服务器接口地址Ip
   factory.afterPropertiesSet();
   INetCoverInfoRemoteService service=(INetCoverInfoRemoteService)factory.getObject();
return service;
}
  (2)方法2
把下面的bean配置在spring配置文件中并注入在一个bean中
  <!--客户端-->
    <bean id="pf" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
        <property name="serviceUrl" value="rmi://192.168.1.8:1099/perfect_time"/>
        <property name="serviceInterface" value="com.open.rmi.ex2.PerfectTimeI"/>
    </bean>
    <bean id="test" class="com.open.rmi.ex2.DisplayPerfectTime">
        <property name="pf" ref="pf"/>
    </bean>
TestServer.java
package com.open.rmi.ex2;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestServer {
    public static void main(String[] args) {
        BeanFactory bf=new ClassPathXmlApplicationContext("bean_server.xml");
        bf.getBean("serviceExporter");
    }
}

你可能感兴趣的:(spring,bean,xml)