Spring学习 RMI

来源:互联网 发布:雅马哈授权网络经销商 编辑:程序博客网 时间:2024/06/12 00:42

使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。

下面通过一个例子,说明如何通过Spring集成RMI。

服务端发布服务

我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务


package springapp.rmi.rmi;public interface IHelloWorld {public String helloWorld();public String sayHelloToSomeBody(String someBodyName);}

package springapp.rmi.rmi;public class HelloWorld implements IHelloWorld {@Overridepublic String helloWorld() {return "Hello World!";}@Overridepublic String sayHelloToSomeBody(String someBodyName) {return "Hello World!" + someBodyName;}}

<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-3.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>

public class RmiServer {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:rmi_server_context.xml");System.out.println("RMI服务伴随Spring的启动而启动了.....");  Object lock = new Object();          synchronized (lock) {              try {lock.wait();} catch (InterruptedException e) {e.printStackTrace();}          }      }}

Client

<?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:p="http://www.springframework.org/schema/p"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">        <bean id="helloWorld" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">          <property name="serviceUrl" value="rmi://192.168.1.34:8088/hello" />          <property name="serviceInterface"              value="springapp.rmi.rmi.IHelloWorld" />      </bean>    </beans>

package springapp.rmi.rmi;public interface IHelloWorld {public String helloWorld();public String sayHelloToSomeBody(String someBodyName);}

public class RMIClientTest {public static void main(String[] args){ApplicationContext ctx = new ClassPathXmlApplicationContext(                  "classpath:client.xml");          IHelloWorld hs = (IHelloWorld) ctx.getBean("helloWorld");        System.out.println(hs);        System.out.println(hs.helloWorld());          System.out.println(hs.sayHelloToSomeBody("Lavasoft"));  }}


0 0
原创粉丝点击