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/beanshttp://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/beanshttp://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的调用方法是一样的



本文转自 tianya23 51CTO博客,原文链接:http://blog.51cto.com/tianya23/682008,如需转载请自行联系原作者

相关文章
|
Java Spring 调度
Java Spring RMI一些尝试
最近在做一个调度服务器的项目,因为各个服务器上面的定时任务比较多,分别执行的话运维检查起来比较麻烦.就想到搞一个专门的调度服务器来统一管理这些任务.因为随时可能增加新的服务器或者新的命令,要是全写在一起每次要把整个程序替换掉,上线不方便.
1093 0
|
XML Java 数据格式
SpringRMI解析1-使用示例
Java远程方法调用,即JavaRMI(Java Remote Method Invocation),是Java编程语言里一种用于实现远程过程调用的应用程序编程接口。它使客户机上的运行的程序可以调用远程服务器上的对象。
758 0
|
2月前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
53 0
|
3月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
52 0
|
2月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
137 0
|
1月前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
43 2
|
2月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
|
1月前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
17天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例