黑龙江建筑职业技术学院招生网站,wordpress模板中文版,定制网络流量监控软件,成都科技网站建设找哪家Spring HTTP Invoker是Java到Java远程处理的重要解决方案。 该技术使用标准的Java序列化机制通过HTTP公开服务#xff0c;并且可以被视为替代解决方案#xff0c;而不是Hessian和Burlap中的自定义序列化。 而且#xff0c;它仅由Spring提供#xff0c;因此客户端和服务器应… 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 ServiceClient示例项目的Spring Remoting支持和RMI文章。 让我们看一下Spring Remoting Support以开发Http Invoker ServiceClient。 二手技术 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 --
propertiesspring.version3.1.1.RELEASE/spring.version
/propertiesdependenciesdependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-remoting/artifactIdversion2.0.8/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${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 ConcurrentHashMapLong, 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...ConcurrentHashMapLong, User userMap;/*** Get User Map** return ConcurrentHashMap User Map*/public ConcurrentHashMapLong, User getUserMap() {return userMap;}/*** Set User Map** param ConcurrentHashMap User Map*/public void setUserMap(ConcurrentHashMapLong, 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 ListUser 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 ListUser getUserList() {ListUser list new ArrayListUser();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 xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://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 idUserMap classjava.util.concurrent.ConcurrentHashMap /!-- Cache Service Declaration --bean idCacheService classcom.otv.cache.service.CacheServiceproperty nameuserMap refUserMap//bean !-- Http User Service Bean Declaration --bean idHttpUserService classcom.otv.http.server.HttpUserService property namecacheService refCacheService//bean!-- Http Invoker Service Declaration --bean idHttpUserServiceExporter classorg.springframework.remoting.httpinvoker.HttpInvokerServiceExporter!-- service represents Service Impl --property nameservice refHttpUserService/!-- serviceInterface represents Http Service Interface which is exposed --property nameserviceInterface valuecom.otv.http.server.IHttpUserService//bean!-- Mapping configurations from URLs to request handler beans --bean idurlMapping classorg.springframework.web.servlet.handler.SimpleUrlHandlerMappingproperty namemappingspropsprop key/HttpUserServiceHttpUserServiceExporter/prop/props/property/bean/beans 步骤9创建web.xml web.xml的配置如下 ?xml version1.0 encodingUTF-8?
web-app xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlnshttp://java.sun.com/xml/ns/javaee xmlns:webhttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd xsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd idWebApp_ID version2.5display-nameOTV_SpringHttpInvoker/display-name!-- Spring Context Configuration s Path definition --context-paramparam-namecontextConfigLocation/param-nameparam-value/WEB-INF/HttpUserService-servlet.xml/param-value/context-param!-- The Bootstrap listener to start up and shut down Springs root WebApplicationContext. It is registered to Servlet Container --listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listener!-- Central dispatcher for HTTP-based remote service exporters. Dispatches to registered handlers for processing web requests.--servletservlet-nameHttpUserService/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classload-on-startup2/load-on-startup/servlet!-- Servlets should be registered with servlet container and mapped with url for the http requests. --servlet-mappingservlet-nameHttpUserService/servlet-nameurl-pattern/HttpUserService/url-pattern/servlet-mappingwelcome-file-listwelcome-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 xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd!-- Http Invoker Client Declaration --bean idHttpUserService classorg.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean!-- serviceUrl demonstrates Http Service Url which is called--property nameserviceUrl valuehttp://remotehost:port/OTV_SpringHttpInvoker-0.0.1-SNAPSHOT/HttpUserService/!-- serviceInterface demonstrates Http Service Interface which is called --property nameserviceInterface valuecom.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.HttpInvokerServiceExporterf9104a] 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.HttpInvokerServiceExporterf9104a] 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.HttpInvokerServiceExporterf9104a] 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