JAVA Spring RMI(1)

在Spring整合Rmi中: 
服务端使用了org.springframework.remoting.rmi.RmiServiceExporter 
RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。 
客户端使用了org.springframework.remoting.rmi.RmiProxyFactoryBean 
客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。 
通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。 ===============================================================================================================================================================================================================================================================================================================================================================================

下面给出简单例子 
1、服务端程序: 
1)新建接口:

public interface IRmiServer {        
    public boolean test();    
} 
  • 1
  • 2
  • 3

2)实现该接口的方法:

public class RmiServerImpl implements IRmiServer {  
    @Override  
    public boolean test() {        
        System.out.println("调用了我--服务端 O(∩_∩)O哈!");            
        return true;  
    }    
}  

3)在src下新建applicationContext.xml 配置文件

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.5//EN" "file:/usr/local/tomcat_report/lib/spring-beans-2.0.dtd">  
<beans>        
        <!-- rmi -->  
    <bean id="rmiService" class="com.lovoo.rmi.impl.RmiServerImpl">  
    </bean>    
    <bean id="remoteRmiService" class="org.springframework.remoting.rmi.RmiServiceExporter">  
        <property name="serviceName">  
            <value>remoteService</value>  
        </property>  
        <property name="service" ref="rmiService" />  
        <property name="serviceInterface">  
            <value>com.cayden.rmi.IRmiServer</value>  
        </property>  
        <property name="registryPort">  
            <value>9400</value>  
        </property>  
        <property name="servicePort">  
            <value>9401</value>  
        </property>  
    </bean>  
</beans>

4)启动服务端的类【MainServer.java】

public class MainServer {    
    public static void main(String[] args) {  
        // TODO Auto-generated method stub  
        System.out.println("rmi服务端启动");  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");            
        System.out.println("rmi服务端启动完成。。。");  
    }  

}  

2、客户端代码: 
1)在客户端使用服务端的接口文件:

public interface IRmiServer {        
    public boolean test();    
}  
  • 1
  • 2
  • 3

2)然后在src下新建【applicationContext.xml】

<beans>  
    <!-- rmi远程调用 -->  
    <bean id="testRmiService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
        <property name="serviceUrl">  
            <value>rmi://127.0.0.1:9400/remoteService</value>  
        </property>  
        <property name="serviceInterface">  
            <value>com.lovoo.rmi.IRmiServer</value>  
        </property>  
    </bean>  
</beans>

3)最新新建客户端的测试类【TestRmi.java】

public class TestRmi {  
    public static void main(String[] arg) {  
        System.out.println("rmi客户端开始调用");  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");  
        IRmiServer rmi=(IRmiServer)ctx.getBean("testRmiService");  
        rmi.test();  
        System.out.println("rmi客户端调用结束");  
    }    
}

评论

暂无评论