使用spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。

下面通过一个例子,说明如何通过Spring集成RMI。

服务端发布服务

我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:

  1. package org.shirdrn.spring.remote.rmi;
  2. public interface AccountService {
  3. int queryBalance(String mobileNo);
  4. String shoopingPayment(String mobileNo, byte protocol);
  5. }

服务实现,示例如下所示:

  1. package org.shirdrn.spring.remote.rmi;
  2. import org.apache.log4j.Logger;
  3. public class MobileAccountServiceImpl implements AccountService {
  4. private static final Logger LOG = Logger.getLogger(MobileAccountServiceImpl.class);
  5. public int queryBalance(String mobileNo) {
  6. if (mobileNo != null)
  7. return 100;
  8. return 0;
  9. }
  10. public String shoopingPayment(String mobileNo, byte protocol) {
  11. StringBuffer sb = new StringBuffer().append("Your mobile number is /"").append(
  12. mobileNo).append("/", protocol type is /"").append(protocol)
  13. .append("/".");
  14. LOG.info("Message is: " + sb.toString());
  15. return sb.toString();
  16. }
  17. }

服务端发布服务,供客户端进行(远程方法)调用,Spring配置server.xml如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="serviceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter">
  6. <property name="serviceName" value="MobileAccountService" />
  7. <property name="service" ref="accountService" />
  8. <property name="serviceInterface"
  9. value="org.shirdrn.spring.remote.rmi.AccountService" />
  10. <property name="registryPort" value="8080" />
  11. <property name="servicePort" value="8088" />
  12. </bean>
  13. <bean id="accountService" class="org.shirdrn.spring.remote.rmi.MobileAccountServiceImpl" />
  14. </beans>

上面配置,指定了暴露的服务的名称,通过serviceName属性注入到RmiServiceExporter中,服务名称为MobileAccountService,客户端通过该服务名称就能够进行调用。

下面启动服务端,发布服务,如下所示:

  1. package org.shirdrn.spring.remote.rmi;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;
  3. public class RmiServer {
  4. public static void main(String[] args) throws InterruptedException {
  5. new ClassPathXmlApplicationContext("org/shirdrn/spring/remote/rmi/server.xml");
  6. Object lock = new Object();
  7. synchronized (lock) {
  8. lock.wait();
  9. }
  10. }
  11. }

客户端调用服务

客户端配置client.xml如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  5. <bean id="mobileAccountService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
  6. <property name="serviceUrl" value="rmi://192.168.1.103:8080/MobileAccountService" />
  7. <property name="serviceInterface"
  8. value="org.shirdrn.spring.remote.rmi.AccountService" />
  9. </bean>
  10. </beans>

配置中,将一个serviceUrl和serviceInterface注入给RmiProxyFactoryBean,即可进行远程方法调用。调用示例如下所示:

  1. package org.shirdrn.spring.remote.rmi;
  2. import org.apache.log4j.Logger;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class RmiClient {
  6. private static final Logger LOG = Logger.getLogger(RmiClient.class);
  7. public static void main(String[] args) {
  8. ApplicationContext ctx = new ClassPathXmlApplicationContext(
  9. "org/shirdrn/spring/remote/rmi/client.xml");
  10. AccountService accountService = (AccountService) ctx
  11. .getBean("mobileAccountService");
  12. String result = accountService.shoopingPayment("13800138000", (byte) 5);
  13. LOG.info(result);
  14. }
  15. }

可见,实现远程访问变得非常容易。

Spring与RMI集成实现远程访问的更多相关文章

  1. Quartz学习——Spring和Quartz集成详解(三)

    Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...

  2. Spring和Quartz集成

    本文转载自:http://blog.csdn.net/u010648555/article/details/54891264 Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企 ...

  3. 【Java Web开发学习】Spring发布RMI服务

    [Java 远程服务]Spring发布RMI服务 转载:https://www.cnblogs.com/yangchongxing/p/9084066.html RmiServiceExporter可 ...

  4. Java RMI 介绍和例子以及Spring对RMI支持的实际应用实例

    RMI 相关知识 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网 ...

  5. 在spring环境下集成ActiveMQ

    1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...

  6. 【原】Spring与MongoDB集成:配置

    MongoDB的API提供了DBObject接口来实现BSONObject的操作方法,BasicDBObject是具体实现.但是并没有提供DBObject与BeanObject的转换.在还没有了解到与 ...

  7. Spring与Hibernate集成中的Session问题

    主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...

  8. spring springMVC mybatis 集成

    最近闲来无事,整理了一下spring springMVC mybatis 集成,关于这个话题在园子里已经有很多人写过了,我主要是想提供一个完整的demo,涵盖crud,事物控制等. 整个demo分三个 ...

  9. 细说shiro之五:在spring框架中集成shiro

    官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...

随机推荐

  1. 关于付款条件ZTERM 的函数

    函数:PR_WF_PAYMENT_BLOCK_CHECKCALL FUNCTION 'FI_F4_ZTERM' F061 支付的过程条件FI_CHANGE_PAYMENT_CONDITIONS New ...

  2. More on Class Loading and Initialization

    上一篇博客中对于类的加载和初始化进行了详细的说明,但上一篇博客代码中的main()所在的类为导出类, 对其中一些问题的理解可能会引起误导和不明确,所以补充这篇博客进一步说明.以下面的代码为例进行说明: ...

  3. SpringBoot整合集成redis

    Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...

  4. 笔记:git和码云

    背景:之前使用GitHub,无奈网速原因,有时候竟无法连接,搜索解决方案而又鱼龙混杂淹没在信息的海洋. 于是尝试码云,界面简单,全中文,用起来很是顺手. 码云使用git来管理,操作上都是git的基本指 ...

  5. 【鸟哥的Linux私房菜】笔记2

    Linux的应用 学习资源整理 安装记录 >< 1.Linux的应用: 网络服务器 数据库 学术机构的高效运算任务 嵌入式系统 ... 2.挂载与磁盘分区 学习资源整理 学习 1.书上的网 ...

  6. 20165101刘天野 2017-2018-2 《Java程序设计》第4周学习总结

    #20165101刘天野 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容总结 第五章:子类与继承 面向对象程序设计语言有三大特性:封装.继承和多态性.继承是面向对 ...

  7. ResourceBundle和properties 读取配置文件区别

    java.util.ResourceBundle 和java.util.properties 读取配置文件区别 这两个类都是读取properties格式的文件的,而Properties同时还能用来写文 ...

  8. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. 关于CDN

    DNS域名解析过程 DNS即Domain Name System,是域名解析服务的意思.它在互联网的作用是:把域名转换成为网络可以识别的ip地址.人们习惯记忆域名,但机器间互相只认IP地址,域名与IP ...

  10. WEB开发中常见漏洞

    1.sql注入 SQL注入在黑客领域是一种非常常见的攻击手段,大家应该都听说过很多数据泄漏的案例,其中大部分都是采用SQL注入来获取数据的. SQL注入一般是前端向后台提交数据的时候,在数据中加入SQ ...