Spring与RMI集成实现远程访问
使用spring对RMI的支持,可以非常容易地构建你的分布式应用。在服务端,可以通过Spring的org.springframework.remoting.rmi.RmiServiceExporter可以暴露你的服务;在客户端,通过org.springframework.remoting.rmi.RmiProxyFactoryBean可以使用服务端暴露的服务,非常方便。这种C/S模型的访问方式,可以屏蔽掉RMI本身的复杂性,如服务端Skeleton和客户端Stub等的处理细节,这些对于服务开发和服务使用的人员来说,都是透明的,无需过度关注,而集中精力开发你的商业逻辑。
下面通过一个例子,说明如何通过Spring集成RMI。
服务端发布服务
我们定义了服务接口,服务端实现该服务接口来完成其复杂的逻辑,客户端可以通过该接口调用服务端暴露的服务,如下所示:
- package org.shirdrn.spring.remote.rmi;
- public interface AccountService {
- int queryBalance(String mobileNo);
- String shoopingPayment(String mobileNo, byte protocol);
- }
服务实现,示例如下所示:
- 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如下所示:
- <?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,客户端通过该服务名称就能够进行调用。
下面启动服务端,发布服务,如下所示:
- 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如下所示:
- <?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,即可进行远程方法调用。调用示例如下所示:
- 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集成实现远程访问的更多相关文章
- Quartz学习——Spring和Quartz集成详解(三)
Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企业级应用更好的使用Quartz进行任务的调度.下面就对Spring集成Quartz进行简单的介绍和示例讲解!和上一节 Quar ...
- Spring和Quartz集成
本文转载自:http://blog.csdn.net/u010648555/article/details/54891264 Spring是一个很优秀的框架,它无缝的集成了Quartz,简单方便的让企 ...
- 【Java Web开发学习】Spring发布RMI服务
[Java 远程服务]Spring发布RMI服务 转载:https://www.cnblogs.com/yangchongxing/p/9084066.html RmiServiceExporter可 ...
- Java RMI 介绍和例子以及Spring对RMI支持的实际应用实例
RMI 相关知识 RMI全称是Remote Method Invocation-远程方法调用,Java RMI在JDK1.1中实现的,其威力就体现在它强大的开发分布式网络应用的能力上,是纯Java的网 ...
- 在spring环境下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- 【原】Spring与MongoDB集成:配置
MongoDB的API提供了DBObject接口来实现BSONObject的操作方法,BasicDBObject是具体实现.但是并没有提供DBObject与BeanObject的转换.在还没有了解到与 ...
- Spring与Hibernate集成中的Session问题
主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...
- spring springMVC mybatis 集成
最近闲来无事,整理了一下spring springMVC mybatis 集成,关于这个话题在园子里已经有很多人写过了,我主要是想提供一个完整的demo,涵盖crud,事物控制等. 整个demo分三个 ...
- 细说shiro之五:在spring框架中集成shiro
官网:https://shiro.apache.org/ 1. 下载在Maven项目中的依赖配置如下: <!-- shiro配置 --> <dependency> <gr ...
随机推荐
- (转)js原生自定义事件的触发dispatchEvent
1. 对于标准浏览器,其提供了可供元素触发的方法:element.dispatchEvent(). 不过,在使用该方法之前,我们还需要做其他两件事,及创建和初始化.因此,总结说来就是: 1 2 3 d ...
- mysql数据库补充知识7 索引原理与慢查询优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Python之函数2 嵌套,作用域和闭包(Day12)
一.函数对象 1.函数是第一类对象,即函数可以当做数据传递 1.1 可以被引用 1.2 可以当做参数传递 1.3 返回值可以是函数 1.4 可以当做容器类型的元素 二.函数的嵌套 1.函数嵌套的调用: ...
- 分享几个基于 Yii2 的开源项目
Yii2 Starter Kit(Yii2 开箱即用):https://github.com/trntv/yii2-starter-kit GetYii:https://github.com/iiYi ...
- .ssh中的文件的分别意义
当我们在用户的主目录使用如下命令: cd (进入个人主目录,默认为/home/hadoop) ssh-keygen -t rsa -P '' (注:最后是二个单引号) 表示在用户的主目录创建ssh登陆 ...
- nodejs异步调用async
犹豫nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦,async的流程控制就是为了简化这些操作var async = require('async'); 1.serie ...
- c# 类的序列化,以及嵌套问题
简单的序列化,网上很多,但是突然想到一个问题,如果一个类里用到了另一个,那么怎么办,今天试了试,只需要加上序列号标签就可以了 using System.Collections; using Syste ...
- INSPIRED启示录 读书笔记 - 第15章 特约用户
产品开发伙伴 为了解决两个问题——既深入洞察目标用户的需求,又赢得用户对产品的推荐,建议征集特约用户协助完成产品研发 在项目的开始阶段物色至少六位积极.活跃.乐于分享的目标户,要求是他们在产品的目标用 ...
- poj 2762 Going from u to v or from v to u?【强连通分量缩点+拓扑排序】
Going from u to v or from v to u? Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 15812 ...
- python爬虫-韩寒新浪博客博文
博客地址:http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html 爬第一页博文 #-*-coding:utf--*- import re # ...