spring RMI

    技术2022-05-20  38

    Rmiremote method invocation,即远程方法调用),使用rmi,使用远程的方法就像使用本地的方法一样方便。

    基于spring3.0

    1.首先在src目录下建立两个xml文件

     applicationContext_rmi_client.xml

     内容如下:

     <?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-3.0.xsd"> <bean id="helloService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">  <property name="serviceUrl" value="rmi://localhost:8088/hello" />  <property name="serviceInterface" value="lavasoft.sturmi.HelloService" /> </bean> <bean id="helloServiceClient" class="lavasoft.sturmi.HelloClient">  <property name="helloService" ref="helloService" /> </bean></beans>

     

     applicationContext_rmi_server.xml

     

    <?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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                        http://www.springframework.org/schema/tx                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd                        http://www.springframework.org/schema/aop                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><bean id="helloService" class="lavasoft.sturmi.HelloServiceImpl"/><bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter"><property name="service" ref="helloService"/><!-- 定义服务名 --><property name="serviceName" value="hello"/>

    //暴露给客户端的接口<property name="serviceInterface" value="lavasoft.sturmi.HelloService"/><property name="registryPort" value="8088"/></bean></beans>

     

    2.写一个接口(服务端)//客户端也要提供相同的接口与实现

    package lavasoft.sturmi;

    public interface HelloService { public String helloWorld(); public String sayHelloToSomeBody(String someBodyName);

    }

     

    3.写一个接口的实现

     

    package lavasoft.sturmi;

    public class HelloServiceImpl implements HelloService { public HelloServiceImpl() {}

     @Override public String helloWorld() {  // TODO Auto-generated method stub  return "helloWorld"; }

     @Override public String sayHelloToSomeBody(String someBodyName) {  // TODO Auto-generated method stub  return "你好," + someBodyName + "!";

     }

    }

    客户端实现的代码:

    package lavasoft.sturmi;

    import java.rmi.RemoteException;

    import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;

    public class HelloClient { private HelloService helloService;

     public static void main(String[] args) throws RemoteException {  ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext_rmi_client.xml");  HelloService hs = (HelloService) ctx.getBean("helloService");  System.out.println(hs.helloWorld());  System.out.println(hs.sayHelloToSomeBody("Lavasoft"));}

     public HelloService getHelloService() {  return helloService; }

     public void setHelloService(HelloService helloService){  this.helloService = helloService; }

     }

    服务端实现的代码:

    package lavasoft.sturmi;

    import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;

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

    }

     

    http://blog.sina.com.cn/s/blog_616e189f0100nkxn.html 

    同时还可以参考:

     

     


    最新回复(0)