使用Spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
public interface AccountService {
int queryBalance(String mobileNo);
String shoopingPayment(String mobileNo, byte protocol);
}
服务实现,示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
public class MobileAccountServiceImpl implements AccountService {
private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}
public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}
服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
</beans>
上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}
客户端调用服务
客户端配置client.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>
</beans>
配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}
}
可见,实现远程访问变得非常容易。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
public interface AccountService {
int queryBalance(String mobileNo);
String shoopingPayment(String mobileNo, byte protocol);
}
服务实现,示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
public class MobileAccountServiceImpl implements AccountService {
private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
public int queryBalance(String mobileNo) {
if (mobileNo != null)
return 100;
return 0;
}
public String shoopingPayment(String mobileNo, byte protocol) {
StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
mobileNo).append("/", protocol type is /"").append(protocol)
.append("/".");
LOG.info("Message is: " + sb.toString());
return sb.toString();
}
}
服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
<property name="serviceName" value="MobileAccountService" />
<property name="service" ref="accountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
<property name="registryPort" value="8080" />
<property name="servicePort" value="8088" />
</bean>
<bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
</beans>
上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiServer {
public static void main(String[] args) throws InterruptedException {
new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
Object lock = new Object();
synchronized (lock) {
lock.wait();
}
}
}
客户端调用服务
客户端配置client.xml如下所示:
[xhtml] view plaincopy
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
<property name="serviceInterface"
value="org.shirdrn.spring.remote.rmi.AccountService" />
</bean>
</beans>
配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:
[java] view plaincopy
package org.shirdrn.spring.remote.rmi;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class RmiClient {
private static final Logger LOG = Logger.getLogger(RmiClient.class);
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"org/shirdrn/spring/remote/rmi/client.xml");
AccountService accountService = (AccountService) ctx
.getBean("mobileAccountService");
String result = accountService.shoopingPayment("13800138000", (byte) 5);
LOG.info(result);
}
}
可见,实现远程访问变得非常容易。
发表评论
-
jmap详解
2013-06-14 18:11 0jmap详解 1. jmap -heap pid ... -
JVM gc参数设置与分析
2013-06-14 18:00 0JVM gc参数设置与分析 ... -
jvm 调优
2013-05-29 14:46 0(转)JVM调优总结 一、相关概念 基本回收算法 ... -
Hibernate缓存何时使用和如何使用
2013-05-27 13:46 5631. 关于hibernate缓存的问 ... -
转 春天的故事-Spring Security3十五日研究
2013-05-17 10:06 0前言 南朝《述异 ... -
Spring中Bean的管理 – 使用BeanWrapper
2013-04-22 16:08 0Spring中Bean的管理 – 使用ApplicatonCo ... -
Spring 工作流程
2013-04-22 12:02 622请求的分发 请求首先到达DispatcherServlet,应 ... -
(转)Java对象池示例
2013-04-19 11:38 554Java对象池示例(转自http://www.cnblogs. ... -
JAVA泛型? T K V E等代表的意思
2013-04-18 10:54 0平时看java源代码的时候,如果碰到泛型的话,我想? T K ... -
Java Volatile transient
2013-04-18 10:24 615Java Volatile transient Java Vo ... -
spring mvc
2013-04-17 00:01 633Spring-mvc 的处理流程 请求的分发 请求首先到达 ... -
apache commons介绍
2013-04-17 00:01 830目前先转此文章,需要的时候再各个研究一下。 Apach ... -
消息队列ZeroMQ
2013-04-16 10:20 2488(转)ZeroMQ简介 通讯socket语言apijms编程 ...
相关推荐
Spring-RMI (RMI调用, HTTP调用) 本人测试过了
spring rmi 2.5.x版本与3.0.x版本不兼容解决方案
spring-rmi Spring RMI调用方法,本人测试过了
Spring整合RMI的使用,实现不同项目间方法的远程调用。
Spring Framework 是一种轻量级的解决方案,是构建你...明式事务管理,通过RMI或Web服务远程访问你的逻辑,以及用于持久存储数据的各种选项。 它提供了一个全功能的 MVC 框架,并使你能够将 AOP 透明地集成到你的软件中
Framework supports declarative transaction management, remote access to your logic through RMI or web services, and various options for persisting your data. It offers a full-featured MVC framework, ...
(9) spring-remoting.jar 这个jar文件包含支持EJB、JMS、远程调用Remoting(RMI、Hessian、Burlap、Http Invoker、JAX-RPC)方面的类。 (10) spring-support.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、...
13) spring-mock.jar需spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jarspring2.0和spring2.5及以上版本的jar包区别Spring 2.5的Jar打包 在Spring 2.5中, Spring Web MVC...
spring-rmi-示例 项目是从 code.google.com/p/springrmiexample 导出的,我这边稍作修改 这个项目是如何在 Spring 的帮助下设置 RMI 服务器和客户端的示例。 该项目包含2个子项目: Spring RMI 示例服务器,即 Web...
SpringRMI小例子代码,仅供参考。SpringRMI小例子代码,仅供参考。
由于spring2和spring3的rmi方式调用方式不同引起的,通过查阅相关文档后发现,spring3不在需要生成skeleton和stub了,所以把这个类从spring-context中删除了,解决办法就是想办法将它再加进来
1. 远程调用RMI(Remote Method Invocation): 通过使用 RmiProxyFactoryBean 和 RmiServiceExporter,并且,Spring支持两个传统的RMI(使用 java.rmi.Remote接口和java.rmi.RemoteException)和通过RMI调用器实现的...
闲来无事,做了个Spring RMI的小例子,非常易懂。
spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-mock.jar来进行辅助测试,正式应用系统中是用不得这些类的...
2. Spring 2.0 的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 更简单的XML配置 2.2.2. 新的bean作用域 2.2.3. 可扩展的XML编写 2.3. 面向切面编程(AOP) 2.3.1. 更加简单的AOP XML配置 2.3.2. 对@AspectJ 切面的...
Spring集成rmi,实现远程服务,使用RMI集成实例
rmi与spring整合实例
Spring Framework 是一个轻...Spring Framework 支 持声明式事务管理,通过 RMI 或 Web 服务远程访问你的逻辑,并支持多种选择持久化你的 数据。它提供了一个全功能的 MVC 框架,使您能够将 AOP 透明地集成到您的软件。
本人搜集了网上的部分资料。 并且将自己实验用的程序,说明文件,以及RMI与SPRING整和的相关资料,都集中到了资源中。 希望能对初学者有所帮助。
为了避免业务逻辑重新开发,顾使用spring rmi,把所有的bean作为rmi服务暴漏出来,在客户端只需要把项目依赖过来就ok,或者把以前的接口导入过来。 参考文档:...