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. C++与C#对比学习:类初始化

    类和柏拉图的理念世界 我们知道面向对象编程中到处是一个个的类,但类只是个概念性的东西,不是个实体,不占内存,你没实例化之前也不能用它.只有把类实例化成一个对象后,它才是一个真正存在的实体.占有内存,能 ...

  2. 【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  3. 【hadoop2.6.0】数据丢失问题解决

    想自己走一遍从代码到运行的流程,结果各种错,郁闷啊. 问题① http://localhost:50070/ 里面一进去就告诉我块丢了.... 解决: bin/hadoop fsck -delete ...

  4. 零基础十分钟学会用git在coding.net上传(pull)和push

    ---恢复内容开始--- 对于入门者来说,特别是刚刚接触计算机的人来说,模仿是最快的学习方式了,先能够会使用(对于初学者来说,这种使用新事物的感觉很能爽的)至于原理,以后再说.下面先让初学者快速的学会 ...

  5. Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作

    %测试数据 'ex1data1.txt', 第一列为 population of City in 10,000s, 第二列为 Profit in $10,000s 1 6.1101,17.592 5. ...

  6. Linux进程的前后台切换

    一.Linux前后台切换的相关命令:   1.&  在命令的后面加上这个符合,让命令进程在后台运行  例如: #ping 127.0.0.1 &        // 此时命令ping ...

  7. iOS,Objective-C,相册功能的实现。

    #import "ViewController.h" #define kuan [UIScreen mainScreen].bounds.size.width #define ga ...

  8. 数据结构和算法 – 7.散列和 Hashtable 类

    7.1.散列函数 散列是一种常见的存储数据的技术,按照这种方式可以非常迅速地插入和取回数据.散列所采用的数据结构被称为是散列表.尽管散列表提供了快速地插入.删除.以及取回数据的操作,但是诸如查找最大值 ...

  9. GPT vs MBR 分区 ,,, Legacy BIOS vs UEFI BIOS

    MBR与GPT两种磁盘分区格式的区别 http://itoedr.blog.163.com/blog/static/120284297201378114053240 GPT Partition Tab ...

  10. 发现一php木马代码

    <?php ;//无需验证密码! $shellname='hello~地球~猴子星球欢迎你 '; define('myaddress',__FILE__); error_reporting(E_ ...