Spring+RMI

http://blog.csdn.net/arkblue/article/details/6237380

 

Spring整合RMI的原理

客户端的核心是RmiProxyFactoryBean,包含serviceURL属性和serviceInterface属性。

通过JRMP访问服务。JRMP JRMP:java remote method protocol,Java特有的,基于流的协议。

Spring+RMI_第1张图片

 

服务端暴露远程服务

RmiServiceExporter把任何Spring管理的Bean输出成一个RMI服务。通过把Bean包装在一个适配器类中工作。适配器类被绑定到RMI注册表中,并且将请求代理给服务类。

Spring+RMI_第2张图片

 

服务端程序:

1 IHelloWorld.java POJO的接口

[java] view plain copy print ?
  1. public interface IHelloWorld {  
  2.     public String helloWorld();  
  3.   
  4.     public String sayHelloToSomeBody(String someBodyName);  
  5. }  

public interface IHelloWorld { public String helloWorld(); public String sayHelloToSomeBody(String someBodyName); }

 

 

2 HelloWorld.java POJO的实现

[java] view plain copy print ?
  1. public class HelloWorld implements IHelloWorld {  
  2.   
  3.     @Override  
  4.     public String helloWorld() {  
  5.         return "Hello World!";  
  6.     }  
  7.   
  8.     @Override  
  9.     public String sayHelloToSomeBody(String someBodyName) {  
  10.         return "Hello World!" + someBodyName;  
  11.     }  
  12.   
  13. }  

public class HelloWorld implements IHelloWorld { @Override public String helloWorld() { return "Hello World!"; } @Override public String sayHelloToSomeBody(String someBodyName) { return "Hello World!" + someBodyName; } }

 

 

3 spring配置文件rmi_server_context.xml

[xhtml] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6. http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7. http://www.springframework.org/schema/aop  
  8. http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  9. http://www.springframework.org/schema/tx  
  10. http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  11.   
  12.     <bean id="helloWorld" class="springapp.rmi.rmi.HelloWorld" />  
  13.   
  14.     <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">  
  15.         <property name="service" ref="helloWorld" />  
  16.         <!-- 定义服务名 -->  
  17.         <property name="serviceName" value="hello" />  
  18.         <property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />  
  19.         <property name="registryPort" value="8088" />  
  20.     </bean>  
  21.   
  22. </beans>  

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="helloWorld" class="springapp.rmi.rmi.HelloWorld" /> <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"> <property name="service" ref="helloWorld" /> <!-- 定义服务名 --> <property name="serviceName" value="hello" /> <property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" /> <property name="registryPort" value="8088" /> </bean> </beans>

 

 

4  服务端启动RMI的代码HelloHost.java

[java] view plain copy print ?
  1. public class HelloHost {  
  2.     public static void main(String[] args) {  
  3.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  4.                 "rmi_server_context.xml");  
  5.         System.out.println("RMI服务伴随Spring的启动而启动了.....");  
  6.     }  
  7. }  

public class HelloHost { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext( "rmi_server_context.xml"); System.out.println("RMI服务伴随Spring的启动而启动了....."); } }

 

 

 

客户端

1 配置文件rmi_client_context.xml

[java] view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">  
  5.     <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  
  6.         <property name="serviceUrl" value="rmi://10.87.40.141:8088/hello" />  
  7.         <property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" />  
  8.     </bean>  
  9. </beans>  

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"> <property name="serviceUrl" value="rmi://10.87.40.141:8088/hello" /> <property name="serviceInterface" value="springapp.rmi.rmi.IHelloWorld" /> </bean> </beans>

 

 

2 客户端代码 HelloClient.java

[java] view plain copy print ?
  1. public class HelloClient {  
  2.   
  3.     public static void main(String[] args) throws RemoteException {  
  4.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  5.                 "rmi_client_context.xml");  
  6.         IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld");  
  7.         System.out.println(hs.helloWorld());  
  8.         System.out.println(hs.sayHelloToSomeBody("Lavasoft"));  
  9.     }  
  10.   
  11. }  

 

你可能感兴趣的:(Spring+RMI)