SpringRMI解析1-使用示例
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-使用示例的更多相关文章
- Java 集合系列05之 LinkedList详细介绍(源码解析)和使用示例
		概要 前面,我们已经学习了ArrayList,并了解了fail-fast机制.这一章我们接着学习List的实现类——LinkedList.和学习ArrayList一样,接下来呢,我们先对Linked ... 
- Java 集合系列07之 Stack详细介绍(源码解析)和使用示例
		概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ... 
- Java 集合系列10之 HashMap详细介绍(源码解析)和使用示例
		概要 这一章,我们对HashMap进行学习.我们先对HashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用HashMap.内容包括:第1部分 HashMap介绍第2部分 HashMa ... 
- Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
		概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ... 
- Java 集合系列 09 HashMap详细介绍(源码解析)和使用示例
		java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ... 
- Java 集合系列 10 Hashtable详细介绍(源码解析)和使用示例
		java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ... 
- Java 集合系列 06 Stack详细介绍(源码解析)和使用示例
		java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ... 
- Java 集合系列 05  Vector详细介绍(源码解析)和使用示例
		java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ... 
- Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例
		java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ... 
随机推荐
- nyoj739_笨蛋难题四
			笨蛋难题四 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 这些日子笨蛋一直研究股票,经过调研,终于发现xxx公司股票规律,更可喜的是 笨蛋推算出这家公司每天的股价, ... 
- codeforces  492B. Vanya and Lanterns 解题报告
			题目链接:http://codeforces.com/problemset/problem/492/B #include <cstdio> #include <cstdlib> ... 
- struts2环境配置
			struts2环境配置 struts2框架,大多数框架都在使用.由于工作需要,开始做Java项目.先学个struts2. 一.下载struts2 有好多版本,我下载的是struts-2.2.1.1. ... 
- python数据库(mysql)操作
			http://fantefei.blog.51cto.com/2229719/1282443 
- RAD Studio/Delphi 2010 3615下载+破解
			RAD Studio/Delphi 2010 3615下载+破解 官方下载地址: http://altd.embarcadero.com/download/RADStudio2010/delphicb ... 
- 在linux中,rpm和yum有什么区别?
			rpm是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装.查询.卸载.升级等工作.但是rpm软件包之间的依赖性问题往往会很繁琐,尤其是软件由多个rpm包组成时.Yum(全称为 Y ... 
- JAVA一些常用的时间操作
			项目中经常有对时间进行处理的需求,下面是一些常用的操作整理,方便以后再次使用以及做相关复习. 1.字符串转换为日期 /** * 字符串转换为日期 * @param dateStr 需要转换的日期 * ... 
- samba 报错
			[root@GitLab data_nfs]# smbclient //localhost/public WARNING: The security=share option is deprecate ... 
- 数据结构和算法 – 5.模式匹配和文本处理
			了使用正则表达式,需要把 RegEx 类引入程序.大家可以在 System.Text.RegularExpression 名字域中找到这种类.一旦把这种类导入了程序,就需要决定想要用 RegEx 类来 ... 
- MVC中数据的内部校验
			针对MVC中实例类型的修饰,我们可以采用DataAnnotations类来完成,该类所处的命名空间是System.ComponentModel.DataAnnotations; 一.通过开类型,能够修 ... 
