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.pswp.cn/news/373683.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/373683.shtml
英文地址,請注明出處:http://en.pswp.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;讓我們開始吧。 有時&…