Http Invoker的Spring Remoting支持

Spring HTTP Invoker是Java到Java遠程處理的重要解決方案。 該技術使用標準的Java序列化機制通過HTTP公開服務,并且可以被視為替代解決方案,而不是Hessian和Burlap中的自定義序列化。 而且,它僅由Spring提供,因此客戶端和服務器應用程序都必須基于Spring。

Spring通過HttpInvokerProxyFactoryBean和HttpInvokerServiceExporter支持HTTP調用程序基礎結構。 HttpInvokerServiceExporter,它將指定的服務bean導出為HTTP調用程序服務端點,可通過HTTP調用程序代理訪問。 HttpInvokerProxyFactoryBean是用于HTTP調用程序代理的工廠bean。

此外,還提供了有關Spring Remoting簡介和RMI Service&Client示例項目的Spring Remoting支持和RMI文章。

讓我們看一下Spring Remoting Support,以開發Http Invoker Service&Client。

二手技術:

  • JDK 1.6.0_31
  • 春天3.1.1
  • Tomcat 7.0
  • 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><dependency><groupId>org.springframework</groupId><artifactId>spring-remoting</artifactId><version>2.0.8</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</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   10 Mar 2012* @version 1.0.0**/
public interface ICacheService {/*** Get User Map** @return ConcurrentHashMap User Map*/public ConcurrentHashMap<Long, User> getUserMap();}

步驟5:創建CacheService類

CacheService類是通過實現ICacheService接口創建的。 它提供對遠程緩存的訪問…

package com.otv.cache.service;import java.util.concurrent.ConcurrentHashMap;import com.otv.user.User;/*** Cache Service Implementation** @author  onlinetechvision.com* @since   10 Mar 2012* @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:建立IHttpUserService接口

創建了代表Http用戶服務接口的IHttpUserService。 此外,它為Http客戶端提供了遠程方法。

package com.otv.http.server;import java.util.List;import com.otv.user.User;/*** Http User Service Interface** @author  onlinetechvision.com* @since   10 Mar 2012* @version 1.0.0**/
public interface IHttpUserService {/*** 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:創建HttpUserService類

HttpUserService類是通過實現IHttpUserService接口創建的。

package com.otv.http.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;/*** Http User Service Implementation** @author  onlinetechvision.com* @since   10 Mar 2012* @version 1.0.0**/
public class HttpUserService implements IHttpUserService {private static Logger logger = Logger.getLogger(HttpUserService.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().remove(user.getId());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 Remote Cache Service** @return ICacheService Remote Cache Service*/public ICacheService getCacheService() {return cacheService;}/*** Set Remote Cache Service** @param ICacheService Remote Cache Service*/public void setCacheService(ICacheService cacheService) {this.cacheService = cacheService;}}

步驟8:創建HttpUserService-servlet.xml

HttpUserService應用程序上下文如下所示。 該xml必須命名為your_servlet_name-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><!-- User Map Declaration --><bean id="UserMap" class="java.util.concurrent.ConcurrentHashMap" /><!-- Cache Service Declaration --><bean id="CacheService" class="com.otv.cache.service.CacheService"><property name="userMap" ref="UserMap"/></bean>   <!-- Http User Service Bean Declaration --><bean id="HttpUserService" class="com.otv.http.server.HttpUserService" ><property name="cacheService" ref="CacheService"/></bean><!-- Http Invoker Service Declaration --><bean id="HttpUserServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"><!-- service represents Service Impl --><property name="service" ref="HttpUserService"/><!-- serviceInterface represents Http Service Interface which is exposed --><property name="serviceInterface" value="com.otv.http.server.IHttpUserService"/></bean><!-- Mapping configurations from URLs to request handler beans --><bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><property name="mappings"><props><prop key="/HttpUserService">HttpUserServiceExporter</prop></props></property></bean></beans>

步驟9:創建web.xml

web.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"><display-name>OTV_SpringHttpInvoker</display-name><!-- Spring Context Configuration' s Path definition --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/HttpUserService-servlet.xml</param-value></context-param><!-- The Bootstrap listener to start up and shut down Spring's root WebApplicationContext. It is registered to Servlet Container --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Central dispatcher for HTTP-based remote service exporters. Dispatches to registered handlers for processing web requests.--><servlet><servlet-name>HttpUserService</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>2</load-on-startup></servlet><!-- Servlets should be registered with servlet container and mapped with url for the http requests. --><servlet-mapping><servlet-name>HttpUserService</servlet-name><url-pattern>/HttpUserService</url-pattern></servlet-mapping><welcome-file-list><welcome-file>/pages/index.xhtml</welcome-file></welcome-file-list></web-app>

步驟10:創建HttpUserServiceClient類

HttpUserServiceClient類已創建。 它調用遠程Http用戶服務并執行用戶操作。

package com.otv.http.client;import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.otv.http.server.IHttpUserService;
import com.otv.user.User;/*** Http User Service Client** @author  onlinetechvision.com* @since   24 Feb 2012* @version 1.0.0**/
public class HttpUserServiceClient {private static Logger logger = Logger.getLogger(HttpUserServiceClient.class);/*** Main method of the Http User Service Client**/public static void main(String[] args) {logger.debug("Http User Service Client is starting...");//Http Client Application Context is started...ApplicationContext context = new ClassPathXmlApplicationContext("httpClientAppContext.xml");//Remote User Service is called via Http Client Application Context...IHttpUserService httpClient = (IHttpUserService) context.getBean("HttpUserService");//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...httpClient.addUser(user);//The users are gotten via remote cache...httpClient.getUserList();//The user is deleted from remote cache...httpClient.deleteUser(user);logger.debug("Http User Service Client is stopped...");}
}

步驟11:創建httpClientAppContext.xml

Http客戶端應用程序上下文如下所示:

<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"><!-- Http Invoker Client Declaration --><bean id="HttpUserService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"><!-- serviceUrl demonstrates Http Service Url which is called--><property name="serviceUrl" value="http://remotehost:port/OTV_SpringHttpInvoker-0.0.1-SNAPSHOT/HttpUserService"/><!-- serviceInterface demonstrates Http Service Interface which is called --><property name="serviceInterface" value="com.otv.http.server.IHttpUserService"/></bean></beans>

步驟12:部署項目

OTV_SpringHttpInvoker Project部署到Tomcat之后,將啟動Http用戶服務客戶端,并且輸出日志如下所示:

....
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:819) - DispatcherServlet with name 'HttpUserService' processing POST request for [/OTV_SpringHttpInvoker-0.0.1-SNAPSHOT/HttpUserService]
15.03.2012 21:26:41 DEBUG (AbstractUrlHandlerMapping.java:124) - Mapping [/HttpUserService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@f9104a] and 1 interceptor
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.addUser
15.03.2012 21:26:41 DEBUG (HttpUserService.java:33) - User has been added to cache. User : Id : 1, Name : Bruce, Surname : Willis
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.addUser
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:957) - Null ModelAndView returned to DispatcherServlet with name 'HttpUserService': assuming HandlerAdapter completed request handling
15.03.2012 21:26:41 DEBUG (FrameworkServlet.java:913) - Successfully completed request
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:819) - DispatcherServlet with name 'HttpUserService' processing POST request for [/OTV_SpringHttpInvoker-0.0.1-SNAPSHOT/HttpUserService]
15.03.2012 21:26:41 DEBUG (AbstractUrlHandlerMapping.java:124) - Mapping [/HttpUserService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@f9104a] and 1 interceptor
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.getUserList
15.03.2012 21:26:41 DEBUG (HttpUserService.java:57) - User List : [Id : 1, Name : Bruce, Surname : Willis]
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.getUserList
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:957) - Null ModelAndView returned to DispatcherServlet with name 'HttpUserService': assuming HandlerAdapter completed request handling
15.03.2012 21:26:41 DEBUG (FrameworkServlet.java:913) - Successfully completed request
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:819) - DispatcherServlet with name 'HttpUserService' processing POST request for [/OTV_SpringHttpInvoker-0.0.1-SNAPSHOT/HttpUserService]
15.03.2012 21:26:41 DEBUG (AbstractUrlHandlerMapping.java:124) - Mapping [/HttpUserService] to HandlerExecutionChain with handler [org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter@f9104a] and 1 interceptor
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:73) - Incoming HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.deleteUser
15.03.2012 21:26:41 DEBUG (HttpUserService.java:45) - User has been deleted from cache. User : Id : 1, Name : Bruce, Surname : Willis
15.03.2012 21:26:41 DEBUG (RemoteInvocationTraceInterceptor.java:79) - Finished processing of HttpInvokerServiceExporter remote call: com.otv.http.server.IHttpUserService.deleteUser
15.03.2012 21:26:41 DEBUG (DispatcherServlet.java:957) - Null ModelAndView returned to DispatcherServlet with name 'HttpUserService': assuming HandlerAdapter completed request handling
15.03.2012 21:26:41 DEBUG (FrameworkServlet.java:913) - Successfully completed request
...

步驟13:下載

OTV_SpringHttpInvoker

參考: Online Technology Vision博客上的JCG合作伙伴 Eren Avsarogullari 提供的Http Invoker的Spring Remoting支持 。


翻譯自: https://www.javacodegeeks.com/2012/04/spring-remoting-support-with-http.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/372900.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/372900.shtml
英文地址,請注明出處:http://en.pswp.cn/news/372900.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql 日期列表_MySQL 生成日期表

1、創建一個num表&#xff0c;用來存儲數字0~9CREATE TABLE num (i int);2、在num表中生成0~9INSERT INTO num (i) VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);3、生成一個存儲日期的表&#xff0c;datalist是字段名CREATE TABLE if not exists calendar(dateli…

學習后綴自動機想法

小序&#xff1a;學習后綴自動機是要有耐心的&#xff0c;clj的論文自己看真心酸爽&#xff01;&#xff08;還是自己太弱&#xff0c;ls&#xff0c;oyzx好勁啊&#xff0c;狂膜不止&#xff09; 剛剛在寫博客之前又看了篇論文&#xff0c;終于看懂了&#xff0c;好開心 正文&…

【BZOJ】3575: [Hnoi2014]道路堵塞

題目鏈接&#xff1a;http://www.lydsy.com/JudgeOnline/problem.php?id3575 大概的做法是&#xff0c;按照順序枚舉每一條要刪去的邊&#xff0c;(假設當前點為$u$&#xff0c;在最短路徑上的下一個點是$v$)然后強制不走${u->v}$這條邊&#xff0c;將$u$入隊&#xff0c;做…

結合使用slf4j和Logback教程

在當前文章中&#xff0c;我將向您展示如何配置您的應用程序以使用slf4j和logback作為記錄器解決方案。 Java簡單日志記錄外觀&#xff08;slf4j&#xff09;是各種日志記錄框架的簡單外觀&#xff0c;例如JDK日志記錄&#xff08;java.util.logging&#xff09;&#xff0c;lo…

mysql 分組top_MySQL:如何查詢出每個分組中的 top n 條記錄?

問題描述需求&#xff1a;查詢出每月 order_amount(訂單金額) 排行前3的記錄。例如對于2019-02&#xff0c;查詢結果中就應該是這3條&#xff1a;解決方法MySQL 5.7 和 MySQL 8.0 有不同的處理方法。1. MySQL 5.7我們先寫一個查詢語句。根據 order_date 中的年、月&#xff0c;…

ACM第四站————最小生成樹(普里姆算法)

對于一個帶權的無向連通圖&#xff0c;其每個生成樹所有邊上的權值之和可能不同&#xff0c;我們把所有邊上權值之和最小的生成樹稱為圖的最小生成樹。 普里姆算法是以其中某一頂點為起點&#xff0c;逐步尋找各個頂點上最小權值的邊來構建最小生成樹。 其中運用到了回溯&#…

利用jenkins的api來完成相關工作流程的自動化

[本文出自天外歸云的博客園] 背景 1. 實際工作中涉及到安卓客戶端方面的測試&#xff0c;外推或運營部門經常會有很多的渠道&#xff0c;而每個渠道都對應著一個app的下載包&#xff0c;這些渠道都記錄在安卓項目下的一個渠道列表文件中。外推或運營部門經常會有新的渠道產生&a…

擁有成本分析:Oracle WebLogic Server與JBoss

Crimson Consulting Group 撰寫的非常有趣的白皮書 &#xff0c;比較了Weblogic和JBoss之間的擁有成本 。 盡管JBoss是免費的&#xff0c;但該白皮書卻嚴肅地宣稱&#xff0c;從長遠來看&#xff0c;Weblogic更便宜。 盡管此研究是由Oracle贊助的&#xff0c;但它看起來非常嚴肅…

mysql limit 分頁 0_Mysql分頁之limit用法與limit優化

Mysql limit分頁語句用法與Oracle和MS SqlServer相比&#xff0c;mysql的分頁方法簡單的讓人想哭。--語法&#xff1a;SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset--舉例&#xff1a;select * from table limit 5; --返回前5行select * from table limit 0…

與硒的集成測試

總覽 我已經使用了一段時間&#xff0c;遇到了一些似乎可以使生活更輕松的事情。 我以為可以將其作為教程分享&#xff0c;所以我將向您介紹這些部分&#xff1a; 使用Maven設置Web項目&#xff0c;配置Selenium以在CI上作為集成測試運行 尋找使用“頁面對象”為網站中的頁面…

linux每天一小步---sed命令詳解

1 命令功能 sed是一個相當強大的文件處理編輯工具&#xff0c;sed用來替換&#xff0c;刪除&#xff0c;更新文件中的內容。sed以文本行為單位進行處理&#xff0c;一次處理一行內容。首先sed吧當前處理的行存儲在臨時的緩沖區中&#xff08;稱為模式空間pattern space&#xf…

mysql trace工具_100% 展示 MySQL 語句執行的神器-Optimizer Trace

在上一篇文章《用Explain 命令分析 MySQL 的 SQL 執行》中&#xff0c;我們講解了 Explain 命令的詳細使用。但是它只能展示 SQL 語句的執行計劃&#xff0c;無法展示為什么一些其他的執行計劃未被選擇&#xff0c;比如說明明有索引&#xff0c;但是為什么查詢時未使用索引等。…

MOXy作為您的JAX-RS JSON提供程序–服務器端

在以前的系列文章中&#xff0c;我介紹了如何利用EclipseLink JAXB&#xff08;MOXy&#xff09;創建RESTful數據訪問服務。 在本文中&#xff0c;我將介紹在服務器端利用MOXy的新JSON綁定添加對基于JAXB映射的JSON消息的支持有多么容易。 MOXy作為您的JAX-RS JSON提供程序–服…

006_過濾器

過濾器 過濾器&#xff08;Filter&#xff09;把附加邏輯注入到MVC框的請求處理&#xff0c;實現了交叉關注。所謂交叉關注&#xff08;Cross-Cutting Concerns&#xff09;&#xff0c;是指可以用于整個應用程序&#xff0c;而又不適合放置在某個局部位置的功能&#xff0c;否…

Android_項目文件結構目錄分析

android項目文件結構目錄分析 在此我們新建了一個helloworld的項目&#xff0c;先看一些目錄結構&#xff1a; 這么多的文件夾和文件中&#xff0c;我們重點關注是res目錄、src目錄、AndroidManifest.xml文件&#xff1a; 一、res目錄主要是用來存放android項目的各種資源文件&…

實體 聯系 模型mysql_數據庫系統概念讀書筆記――實體-聯系模型_MySQL

bitsCN.com數據庫系統概念讀書筆記——實體-聯系模型前言為了重新回顧我寫的消息系統架構&#xff0c;我需要重新讀一下數據庫系統概念的前三章&#xff0c;這里簡單的做一個筆記&#xff0c;方便自己回顧基本概念實體-聯系(E-R)數據模型基于對現實世界的這樣一種認識&#xff…

使用Twitter Bootstrap,WebSocket,Akka和OpenLayers玩(2.0)

原始帖子可以在ekito網站上找到。 對于我們的一位客戶&#xff0c;我們需要顯示一張具有實時更新的車輛位置的地圖。 因此&#xff0c;我開始使用Play制作原型&#xff01; 框架及其最新發布的版本2.0&#xff0c;使用Java API。 我從Play的網絡聊天室開始&#xff01; 2.0個樣…

同步時間

同步時間 [rootlocalhost 03]# ntpdate 0.centos.pool.ntp.org 轉載于:https://www.cnblogs.com/cglWorkBook/p/5556920.html

mysql 5.6.23免安裝_mysql5.6.23免安裝配置

1.官網下載&#xff0c;并解壓2.環境變量&#xff0c;path下&#xff0c;追加mysql的bin路徑D:\Program Files\mysql\bin;3.mysql目錄下的my-default.ini重命名為my.ini&#xff0c;并添加下面的代碼basedirD:/Program Files/mysql #mysql路徑datadirD:/Program Files/mysql/d…