Java远程方法调用,即JavaRMI(JavaRemote Method Invocation),是Java编程语言里一种用于实现远程过程调用的应用程序编程接口。它使客户机上的运行的程序可以调用远程服务器上的对象。远程方法调用特性使Java编程人员能够在网络环境中分布操作。RMI全部的宗旨就是尽可能地简化远程接口对象的使用。

JAVA RMI极大地依赖于接口。在需要创建一个远程对象时,程序员通过传递一个接口来隐藏底层的实现细节。客户端得到的远程对象句柄正好与本地的根代码链接,由后者负责透过网络通信。这样一来,程序员只需关心如何通过自己的接口句柄发送消息。

RMI (Remote Method Invocation)是从JDK 1.1开始就出现的API功能,它让客户端在使用远端服务所提供的服务时,就如何使用本地服务一样,然而RMI在使用时必须一连串繁复的手续,像是服务介面在定义时必须继承Java.rmi.Remote介面、服务Server在实作时必须继承java.rmi.UnicastRemoteObject类别、必须使用rmic指令产生stub与skeleton等,设定上手续繁杂。

Spring RMI实际上是扩展了下javarmi的实现,可以使用bean的xml配置方式使用rmi。可以在Spring中透过org.springframework.remoting.rmi.RmiServiceExporter来简化使用RMI的手续,来实际看看例子,了解Spring在RMI上的使用与简化。

首先定义一个服务接口

package org.spring;
public interface RmiService {
public String doWork();
public int add(int a, int b);
}

服务接口实现

package org.spring;
public class RmiServiceImpl implements RmiService{
@Override
public String doWork() {
return "this message return from server";
}
@Override
public int add(int a, int b) {
return a+b;
}
}

在Bean定义档中定义,让Spring管理、生成Bean实例,如此即可注册、启动RMI服务

<?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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="rmiService" class="org.spring.RmiServiceImpl"/>
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="service">
<ref bean="rmiService"/>
</property>
<property name="serviceName">
<value>rmiService</value>
</property>
<property name="serviceInterface">
<value>org.spring.RmiService</value>
</property>
</bean>
</beans>

启动RMI服务

package org.spring;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIServer {
public static void main(String[] args) throws IOException {
new ClassPathXmlApplicationContext("config/rmi-server.xml");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (reader.readLine().equals("exit")) {
System.exit(0);
}
}
}
}

在客户端,依赖接口对应的jar包就可以了,然后再spring的配置文件中配置好需要访问的服务的地址和对应的接口名称

<?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" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="rmiServiceProxy" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl">
<value>rmi://localhost/rmiService</value>
</property>
<property name="serviceInterface">
<value>org.spring.RmiService</value>
</property>
</bean>
</beans>

注意到"serviceUrl"属性的设定,它是以"rmi://"开头,接着指定服务地址与服务名称,写个简单的客户端程式以使用RMI服务器上的服务

package org.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RMIClient {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("config/rmi-client.xml");
RmiService service = (RmiService) context.getBean("rmiServiceProxy");
String result1 = service.doWork();
System.out.println(result1);
int result2 = service.add(1, 2);
System.out.println(result2);
}
}

执行RMIServer.java类,在spring web应用中只要配置文件加载到spring的加载路径即可应用。然后再执行RMIClient.java类。

这样使用spring就能很方便的简化java 的rmi调用,并且由spring管理,在分布式的应用中就能做到客户端只依赖接口,不依赖实现。

 
 

SpringRMI解析1-使用示例的更多相关文章

  1. Java 集合系列05之 LinkedList详细介绍(源码解析)和使用示例

    概要  前面,我们已经学习了ArrayList,并了解了fail-fast机制.这一章我们接着学习List的实现类——LinkedList.和学习ArrayList一样,接下来呢,我们先对Linked ...

  2. Java 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  3. Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ...

  4. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  5. Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  6. Java 集合系列 10 Hashtable详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  7. Java 集合系列 06 Stack详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  8. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  9. Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

随机推荐

  1. Windows update 失败的解决方案

    由于刚刚装了新的系统,结果在电脑用了刚刚一周之后,我打开了自动更新,结果电脑就一直处于更新状态中.期初我以为是更新比较慢,等等可能就结束了,结果等了足足一晚上,到了第二天,电脑显示是:配置更新失败,正 ...

  2. 【python】classmethod & staticmethod 区别

    来源:http://blog.csdn.net/carolzhang8406/article/details/6856817 其他参考: http://blog.csdn.net/lovingprin ...

  3. iOS添加另一个控制器的时候要注意啊

    ios的父控制器控制器和子控制器之间的注意点: " >> #warning 需要把新创建出来的控制器添加成子控制器 . 不然子控制器里面的子控件无法响应点击事件 " 一 ...

  4. [Android Pro] InputStream.skip方法的思考

    参考 : http://blog.csdn.net/gsyzhu/article/details/8102286 在java.io.InputStream类中定义了skip这个方法.在API中的描述如 ...

  5. JAVA addShutdownHook测试

    public static void main(String[] args) { System.out.println("1111111111"); try { Thread.sl ...

  6. hdu1722(gcd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1722 题意:要使一块蛋糕既能均分给a个人,又能均分给b个人,问至少需要分成几块(不需要每块都一样大小) ...

  7. [转]DB2类型转换函数

    Src URL:http://www.cnblogs.com/QQParadise/articles/2642677.html

  8. python简介

    python起源 作者Guido van Rossum,荷兰人 在创建python之初,1989年12月份,Guido只是想用编程来打发圣诞的闲暇时光.Guido也希望能有一门语言既能够像C语言那样, ...

  9. Clr Via C#读书笔记---程序集的加载和反射

    #1 加载程序集 Assembly.Load: public class Assembly { public static Assembly Load(AssemblyName assemblyRef ...

  10. html 中几次方,平方米,立方米,下标,上标,删除线等的表示方法

    html 中几次方,平方米,立方米,上标,下标,删除线等的表示方法 上标下标删除线 小号字  M2 54 X24+Y1<3=100 NN <sup>上标</sup> &l ...