Spring远程支持和开发RMI服务

Spring远程支持简化了启用远程服务的开发。 当前,Spring支持以下远程技术:远程方法调用(RMI),HTTP调用程序,Hessian,Burlap,JAX-RPC,JAX-WS和JMS。

远程方法调用(RMI) :Spring通过RmiProxyFactoryBean和RmiServiceExporter支持RMI。 RmiServiceExporter将任何Spring管理的bean导出为RMI服务并进行注册。 RmiProxyFactoryBean是一种工厂bean,可为RMI服务创建代理。 该代理对象代表客户端与远程RMI服务进行通信。

Spring的HTTP调用程序: Spring HTTP调用程序使用标准的Java序列化机制通过HTTP公开服务。 Spring通过HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter支持HTTP调用程序基础结构。 HttpInvokerServiceExporter,它将指定的服务bean导出为HTTP调用程序服务端点,可通过HTTP调用程序代理访问。 HttpInvokerProxyFactoryBean是用于HTTP调用程序代理的工厂bean。

Hessian: Hessian提供了一个基于HTTP的二进制远程协议。 Spring通过HessianProxyFactoryBean和HessianServiceExporter支持Hessian。

粗麻布:粗麻布是Caucho的基于XML的粗麻布替代品。 Spring提供了支持类,例如BurlapProxyFactoryBean和BurlapServiceExporter。

JAX-RPC: Spring通过JAX-RPC(J2EE 1.4的Web服务API)为Web服务提供远程支持。

JAX-WS: Spring通过JAX-WS(Java EE 5和Java 6中引入的JAX-RPC的继承者)为Web服务提供远程支持。

JMS: JmsInvokerServiceExporter和JmsInvokerProxyFactoryBean类提供了Spring中的JMS远程支持。

让我们看一下Spring RMI支持以开发Spring RMI Service&Client。

二手技术:

JDK 1.6.0_31
春天3.1.1
Maven的3.0.2

步骤1:建立已完成的专案

创建一个Maven项目,如下所示。 (可以使用Maven或IDE插件来创建它)。

步骤2:图书馆

Spring依赖项已添加到Maven的pom.xml中。

<!-- Spring 3.1.x dependencies -->
<properties><spring.version>3.1.1.RELEASE</spring.version>
</properties><dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency>
<dependencies>

步骤3:建立使用者类别

创建一个新的用户类。

package com.otv.user;import java.io.Serializable;/*** User Bean** @author  onlinetechvision.com* @since   24 Feb 2012* @version 1.0.0**/
public class User implements Serializable {private long id;private String name;private String surname;/*** Get User Id** @return long id*/public long getId() {return id;}/*** Set User Id** @param long id*/public void setId(long id) {this.id = id;}/*** Get User Name** @return long id*/public String getName() {return name;}/*** Set User Name** @param String name*/public void setName(String name) {this.name = name;}/*** Get User Surname** @return long id*/public String getSurname() {return surname;}/*** Set User Surname** @param String surname*/public void setSurname(String surname) {this.surname = surname;}@Overridepublic String toString() {StringBuilder strBuilder = new StringBuilder();strBuilder.append("Id : ").append(getId());strBuilder.append(", Name : ").append(getName());strBuilder.append(", Surname : ").append(getSurname());return strBuilder.toString();}
}

步骤4:建立ICacheService介面

创建了代表远程缓存服务接口的ICacheService接口。

package com.otv.cache.service;import java.util.concurrent.ConcurrentHashMap;import com.otv.user.User;/*** Cache Service Interface** @author  onlinetechvision.com* @since   27 Feb 2012* @version 1.0.0**/
public interface ICacheService {/*** Get User Map** @return ConcurrentHashMap User Map*/public ConcurrentHashMap<long, user> getUserMap();}

步骤5:创建CacheService类

CacheService类是通过实现ISchedulerService接口创建的。 它提供对远程缓存的访问…

package com.otv.cache.service;import java.util.concurrent.ConcurrentHashMap;import com.otv.user.User;/*** Cache Service Implementation** @author  onlinetechvision.com* @since   6:04:49 PM* @version 1.0.0**/
public class CacheService implements ICacheService {//User Map is injected...ConcurrentHashMap<long, user> userMap;/*** Get User Map** @return ConcurrentHashMap User Map*/public ConcurrentHashMap<long, user> getUserMap() {return userMap;}/*** Set User Map** @param ConcurrentHashMap User Map*/public void setUserMap(ConcurrentHashMap<long, user> userMap) {this.userMap = userMap;}}

步骤6:建立IRMIUserService接口

创建代表RMI服务接口的IRMIUserService接口。 另外,它为RMI客户端提供了远程方法。

package com.otv.rmi.server;import java.util.List;import com.otv.user.User;/*** RMI User Service Interface** @author  onlinetechvision.com* @since   24 Feb 2012* @version 1.0.0**/
public interface IRMIUserService {/*** Add User** @param  User user* @return boolean response of the method*/public boolean addUser(User user);/*** Delete User** @param  User user* @return boolean response of the method*/public boolean deleteUser(User user);/*** Get User List** @return List user list*/public List<User> getUserList();}

步骤7:创建RMIUserService类

RMIUserService类(又名RMI对象)是通过实现IRMIUserService接口创建的。

package com.otv.rmi.server;import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;import com.otv.cache.service.ICacheService;
import com.otv.user.User;/*** RMI User Service Implementation** @author  onlinetechvision.com* @since   24 Feb 2012* @version 1.0.0**/
public class RMIUserService implements IRMIUserService {private static Logger logger = Logger.getLogger(RMIUserService.class);//Remote Cache Service is injected...ICacheService cacheService;/*** Add User** @param  User user* @return boolean response of the method*/public boolean addUser(User user) {getCacheService().getUserMap().put(user.getId(), user);logger.debug("User has been added to cache. User : "+getCacheService().getUserMap().get(user.getId()));return true;}/*** Delete User** @param  User user* @return boolean response of the method*/public boolean deleteUser(User user) {getCacheService().getUserMap().put(user.getId(), user);logger.debug("User has been deleted from cache. User : "+user);return true;}/*** Get User List** @return List user list*/public List<User> getUserList() {List<User> list = new ArrayList<User>();list.addAll(getCacheService().getUserMap().values());logger.debug("User List : "+list);return list;}/*** Get RMI User Service** @return IRMIUserService RMI User Service*/public ICacheService getCacheService() {return cacheService;}/*** Set RMI User Service** @param IRMIUserService RMI User Service*/public void setCacheService(ICacheService cacheService) {this.cacheService = cacheService;}
}

步骤8:创建RMIServerStarter类别

RMI服务器启动程序类已创建。 它启动RMI服务器。

package com.otv.rmi.server.starter;import org.springframework.context.support.ClassPathXmlApplicationContext;/*** RMI Server Starter** @author  onlinetechvision.com* @since   27 Feb 2012* @version 1.0.0**/
public class RMIServerStarter {public static void main(String[] args) {//RMI Server Application Context is started...new ClassPathXmlApplicationContext("rmiServerAppContext.xml");}
}

步骤9:创建rmiServerAppContext.xml

RMI服务器应用程序上下文的内容如下所示。

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- Beans Declaration --><bean id="UserMap" class="java.util.concurrent.ConcurrentHashMap" /><bean id="CacheService" class="com.otv.cache.service.CacheService"><property name="userMap" ref="UserMap"/></bean>   <bean id="RMIUserService" class="com.otv.rmi.server.RMIUserService" ><property name="cacheService" ref="CacheService"/></bean><!-- RMI Server Declaration --><bean class="org.springframework.remoting.rmi.RmiServiceExporter"><!-- serviceName represents RMI Service Name --><property name="serviceName" value="RMIUserService"/><!-- service represents RMI Object(RMI Service Impl) --><property name="service" ref="RMIUserService"/><!-- serviceInterface represents RMI Service Interface exposed --><property name="serviceInterface" value="com.otv.rmi.server.IRMIUserService"/><!-- defaults to 1099 --><property name="registryPort" value="1099"/></bean></beans>

步骤10:创建RMIServiceClient类

RMIServiceClient类已创建。 它调用RMI用户服务并执行用户操作。

package com.otv.rmi.client;import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.otv.rmi.server.IRMIUserService;
import com.otv.rmi.server.RMIUserService;
import com.otv.user.User;/*** RMI Service Client** @author  onlinetechvision.com* @since   24 Feb 2012* @version 1.0.0**/
public class RMIServiceClient {private static Logger logger = Logger.getLogger(RMIUserService.class);/*** Main method of the RMI Service Client**/public static void main(String[] args) {logger.debug("RMI Service Client is starting...");//RMI Client Application Context is started...ApplicationContext context = new ClassPathXmlApplicationContext("rmiClientAppContext.xml");//Remote User Service is called via RMI Client Application Context...IRMIUserService rmiClient = (IRMIUserService) context.getBean("RMIUserService");//New User is created...User user = new User();user.setId(1);user.setName("Bruce");user.setSurname("Willis");//The user is added to the remote cache...rmiClient.addUser(user);//The users are gotten via remote cache...rmiClient.getUserList();//The user is deleted from remote cache...rmiClient.deleteUser(user);logger.debug("RMI Service Client is stopped...");}
}

步骤11:创建rmiClientAppContext.xml

RMI客户端应用程序上下文的内容如下所示。

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!-- RMI Client Declaration --><bean id="RMIUserService" class="org.springframework.remoting.rmi.RmiProxyFactoryBean"><!-- serviceUrl represents RMI Service Url called--><property name="serviceUrl" value="rmi://x.x.x.x:1099/RMIUserService"/><!-- serviceInterface represents RMI Service Interface called --><property name="serviceInterface" value="com.otv.rmi.server.IRMIUserService"/><!-- refreshStubOnConnectFailure enforces automatic re-lookup of the stub if acall fails with a connect exception --><property name="refreshStubOnConnectFailure" value="true"/></bean></beans>

步骤12:运行项目

如果运行RMI Server时启动了RMI Service Client,则将在RMI Server输出日志下方显示。 同样,可以通过IDE打开两个单独的控制台来运行RMI Server和Client。 :

....
04.03.2012 14:23:15 DEBUG (RmiBasedExporter.java:59) - RMI service [com.otv.rmi.server.RMIUserService@16dadf9] is an RMI invoker
04.03.2012 14:23:15 DEBUG (JdkDynamicAopProxy.java:113) - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.otv.rmi.server.RMIUserService@16dadf9]
04.03.2012 14:23:15  INFO (RmiServiceExporter.java:276) - Binding service 'RMIUserService' to RMI registry: RegistryImpl[UnicastServerRef [liveRef: [endpoint:[192.168.1.7:1099](local),objID:[0:0:0, 0]]]]
04.03.2012 14:23:15 DEBUG (AbstractAutowireCapableBeanFactory.java:458) - Finished creating instance of bean 'org.springframework.remoting.rmi.RmiServiceExporter#0'
04.03.2012 14:23:15 DEBUG (AbstractApplicationContext.java:845) - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3c0007]
04.03.2012 14:23:15 DEBUG (AbstractBeanFactory.java:245) - Returning cached instance of singleton bean 'lifecycleProcessor'04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.addUser
04.03.2012 14:25:43 DEBUG (RMIUserService.java:33) - User has been added to cache. User : Id : 1, Name : Bruce, Surname : Willis
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.addUser04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList
04.03.2012 14:25:43 DEBUG (RMIUserService.java:57) - User List : [Id : 1, Name : Bruce, Surname : Willis]
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.deleteUser
04.03.2012 14:25:43 DEBUG (RMIUserService.java:45) - User has been deleted from cache. User : Id : 1, Name : Bruce, Surname : Willis
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.deleteUser04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList
04.03.2012 14:25:43 DEBUG (RMIUserService.java:57) - User List : []
04.03.2012 14:25:43 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of RmiServiceExporter remote call: com.otv.rmi.server.IRMIUserService.getUserList

步骤13:下载

OTV_SpringRMI

参考: Online Technology Vision博客中的JCG合作伙伴 Eren Avsarogullari的Spring Remoting支持和RMI服务开发 。


翻译自: https://www.javacodegeeks.com/2012/04/spring-remoting-support-and-developing.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/373683.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

cesium绘制网格_Cesium学习笔记-工具篇37-风场绘制

这两天重新接触到流场&#xff0c;于是研究下&#xff0c;在大牛们的轮子上也算实现了效果&#xff1a;1二维2三维主要参考以下三篇文章&#xff1a;《WebGL风向图》给出制作风向图通常步骤&#xff1a;1. 在屏幕上生成一系列随机粒子位置并绘制粒子。2. 对于每一个粒子&#x…

ToString:身份哈希码的十六进制表示形式

我以前在方便的Apache Commons ToStringBuilder上写过博客&#xff0c;最近有人问我&#xff0c;在生成的String输出中出现的看似神秘的文本是什么构成的。 询问该问题的同事正确地推测出他正在查看的是哈希码&#xff0c;但与他实例的哈希码不匹配。 我解释说ToStringBuilder将…

HTML+CSS笔记 CSS中级 缩写入门

盒子模型代码简写回忆盒模型时外边距(margin)、内边距(padding)和边框(border)设置上下左右四个方向的边距是按照顺时针方向设置的&#xff1a;上右下左。语法:margin:10px 15px 12px 14px;/*上设置为10px、右设置为15px、下设置为12px、左设置为14px*/通常有三种缩写的方法:1、…

JavaScript学习随记——常见全局对象属性及方法

<script type"text/javascript" charset"utf-8">//全局对象&#xff1a; Object、Array、Math等/*** 全局的方法&#xff1a;* 1.encodeURI、escape、decodeURIComponet 编码* 2.decodeURI、unescape、encodeURIComponet 解码* 3.parseInt、parseF…

spring boot 定时任务

package com.ict.conf; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled;Configuration EnableScheduling // 启用定时任务 …

搬运机器人举杯贺所需的条件_智能搬运机器人比赛规则

附件4&#xff1a;分拣机器人(智能搬运机器人)比赛规则1、比赛目的设计一个轮式或人形小型机器人&#xff0c;在比赛场地里移动&#xff0c;将不同颜色、形状或者材质的物体分类搬运到不同的对应位置。比赛的记分根据机器人将物体放置的位置精度和完成时间来决定分值的高低。它…

我们多么想要新的Java日期/时间API?

当前的Java.net 民意测验问题是&#xff1a;“ 对于用Java 8实现的JSR-310&#xff08;新的日期和时间API&#xff09;有多重要&#xff1f; ”在我撰写本文时&#xff0c;将近150位受访者投了赞成票&#xff0c;绝大多数人回答“非常”&#xff08;53&#xff05;&#xff09;…

JavaScript学习随记——Function

每个函数都是Function类型的实例&#xff0c;而且都与其他引用类型一样具有属性和方法。由于函数是对象&#xff0c;因此函数名实际上也是一个指向函数对象的指针&#xff0c;不会于某个函数绑定。 函数的定义方式 <script type"text/javascript" charset"ut…

登录id 黑苹果_黑苹果MacOSCatalina无法登录AppStore修复

先上图&#xff1a;惨红色的提示信息&#xff0c;把你拒之App Store门外&#xff0c;但是对之放弃、不与之斗争不是我们的节奏&#xff0c;请看破敌攻略&#xff1a;1.查看你的“关于本机”-->“概览”-->“系统报告”&#xff0c;如图&#xff1a;找到你的“网络”-->…

我们三十以后才明白

当我们懂得珍惜时光的时候,已经发现自己不再年轻. 三十岁,才慢慢的明白. 男女三十而立&#xff0c;三十岁应该是人生的转折点&#xff0c;它不是青春韶华的终结&#xff0c;而是生命的第二起跑线。 三十岁&#xff0c;面对的不应该是没落&#xff0c;而是认知的新起点。很多曾…

Web开发的入门指导

Web开发的入门指导web开发编程技术你点开此文&#xff0c;说明你对Web开发是有兴趣的&#xff0c;或者你正在思考开始学习Web开发。在这里&#xff0c;我会告诉你成为一名Web开发者的路线&#xff0c;是对初学者关于Web开发的指导。这篇文章不会教你如何写代码&#xff0c;而是…

新东方mti百科知识pdf_20南航翻硕mti初试417上岸经验贴

南京航空航天大学mti初试417分排名第一:‌基础英语88:1&#xff0c;外刊阅读:从2月到6月开始一直打卡外刊app&#xff0c;友邻优课&#xff0c;流利阅读等2&#xff0c;背单词:扇贝单词app&#xff0c;新东方专八单词绿皮书&#xff0c;华研专八单词等3&#xff0c;星火专八阅读…

JavaScript学习随记——属性类型

<!DOCTYPE HTML> <html><head><meta http-equiv"Content-Type" content"text/html; charsetutf-8" /><title>属性类型</title></head> <body><script type"text/javascript" charset"…

Shell if else语句

if 语句通过关系运算符判断表达式的真假来决定执行哪个分支。Shell 有三种 if ... else 语句&#xff1a; if ... fi 语句&#xff1b;if ... else ... fi 语句&#xff1b;if ... elif ... else ... fi 语句。1) if ... else 语句 if ... else 语句的语法&#xff1a; if [ ex…

过滤日志中不相关的堆栈跟踪行

我喜欢堆栈痕迹。 不是因为我喜欢错误&#xff0c;而是因为发生错误的那一刻&#xff0c;堆栈跟踪是无价的信息源。 例如&#xff0c;在Web应用程序中&#xff0c;堆栈跟踪向您显示完整的请求处理路径&#xff0c;从HTTP套接字到过滤器&#xff0c;Servlet&#xff0c;控制器&a…

Can't create/write to file '/tmp/#sql_887d_0.MYD' (Errcode: 17)

lsof |grep "#sql_887d_0.MYD" 如果没有被占用就可以删掉 。 https://wordpress.org/support/topic/cant-createwrite-to-file-error Hello, just today I saw this kind of error on every page on my blog. WordPress database error: [Cant create/write to file …

python3怎么创建文件_Python3.5 创建文件的简单实例

实例如下所示&#xff1a;#codingutf-8Created on 2012-5-29author: xiaochouimport osimport timedef nsfile(s):The number of new expected documents#判断文件夹是否存在&#xff0c;如果不存在则创建b os.path.exists("E:\\testFile\\")if b:print("File …

Dijkstra 最短路算法(只能计算出一条最短路径,所有路径用dfs)

上周我们介绍了神奇的只有五行的 Floyd 最短路算法&#xff0c;它可以方便的求得任意两点的最短路径&#xff0c;这称为“多源最短路”。本周来来介绍指定一个点&#xff08;源点&#xff09;到其余各个顶点的最短路径&#xff0c;也叫做“单源最短路径”。例如求下图中的 1 号…

JavaScript学习随记——错误类型

错误类型&#xff1a; 执行代码期间可能会发生的错误有多种类型。每种错误都有对应的错误类型&#xff0c;而当错误发生时&#xff0c;就会抛出相应类型的错误对象。 ECMA-262定义的7种错误类型 Error&#xff1a; 是错误的基类型&#xff0c;其他错误类型都继承该类型。Error…

多个集合中的共同和独特元素

本周&#xff0c;我们将暂时中断较高级别的问题和技术文章&#xff0c;以解决我们中许多人可能面临的一些代码问题。 没什么花哨的或太辛苦的&#xff0c;但是有一天它可能会节省您15分钟的时间&#xff0c;偶尔回到基础上也很不错。 因此&#xff0c;让我们开始吧。 有时&…