用spring来实现rmi十分的方便

RMI,为远程方法调用,我们要用spring来实现调用:

步骤1:

编写远程接口和远程接口的实现类

接口:

package com.rmi;

public interface ISomeService {

 public String doSomeService(String some);
 public int doOtherService(int other);
}

实现类:

package com.rmi;

public class SomeService implements ISomeService {

 public int doOtherService(int other) {
  // TODO Auto-generated method stub
  return ++other;
 }

 public String doSomeService(String some) {
  // TODO Auto-generated method stub
  return some+" is proceeed";
 }

}

服务端的配置文件:

<?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.5.xsd">
 
 <bean id="someService" class="com.rmi.SomeService"></bean>
 <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  <property name="service" ref="someService"></property>
  <property name="serviceName" value="SomeService"></property>
  <property name="serviceInterface" value="com.rmi.ISomeService"></property>
 </bean>
 </beans>

服务端启动代码:

public class RMIServer {

 /**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi.xml");
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  while(true)
  {
   if(br.readLine().equals("exit"))
   {
    break;
   }
   
  }
  RmiServiceExporter rse = (RmiServiceExporter)ctx.getBean("serviceExporter");
  rse.destroy();

 }

}

步骤2:

编写客户端

客户端配置文件:

<?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.5.xsd">
 <bean id="someServiceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  <property name="serviceUrl" value="rmi://127.0.0.1/SomeService"/>
  <property name="serviceInterface" value="com.rmi.ISomeService"/>
 </bean>
</beans>
客户端调用服务端的代码:

package com.rmi;

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

public class RmiClient {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext ctx = new ClassPathXmlApplicationContext("rmi-client.xml");
  ISomeService service = (ISomeService) ctx.getBean("someServiceProxy");
  System.out.println(service.doSomeService("some request"));

 }

}

注意:客户端必须要有服务端ISomeService.class的文件,这样客户端才可以有关于该服务端接口的引用,这个是和一般的webservice的调用方法是一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值