Spring Remoting: HTTP Invoker--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-http-invoker.jsp
Concept Overview
In the earlier articles we saw an introduction to spring remoting and its support for RMI, Hessian and Burlap. In this tutorial we look at one more support for remoting - HttpInvoker. HttpInvoker combines the ease of Hessian and Burlap, in that it is very easy to set up. It serializes and deserializes java object for trasport over the network. However, probably the only drawback is that Http Invoker is bound to java and hence the clients all need to be java based. This is the recommended choice for remoting for java-java based communication. The main classes are :org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter - This is a servlet API based Http request handler. It is used to export the remote services. It takes in a service property that is the service to be exported and aServiceInterface that specifies the interface that the service is tied to.
org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean - This is a proxy factory for creating http invoker proxies. It has a serviceUrl property that must be an http url exposing an http invoker service. This class serializes the objects that are sent to remote services and deserializes the objects back.
Sample Program Overview

- aopalliance.jar
- commons-logging.jar
- log4j.jar
- org.springframework.aop.jar
- org.springframework.asm.jar
- org.springframework.beans.jar
- org.springframework.context.jar
- org.springframework.context.support.jar
- org.springframework.core.jar
- org.springframework.expression.jar
- org.springframework.web.jar
- org.springframework.web.servlet.jar

- Client sends a message call
- This message call is handled by a HTTP Proxy created by HttpInvokerProxyFactoryBean
- The HTTP Proxy converts the call into a remote call over HTTP
- The HTTP Service Adapter created by HttpInvokerServiceExporter intercepts the remote call over HTTP
- It forwards the method call to Service

Create the GreetingService interface as shown below.
Create a method named getGreeting() that takes a name as a parameter and returns the greeting message (see line 5 below).
|
1
2
3
4
5
6
|
package com.studytrails.tutorials.springremotinghttpinvokerserver;public interface GreetingService {String getGreeting(String name);} |
Create a class GreetingServiceImpl as shown below.
It implements the GreetingService interface (described earlier)
Implement the getGreeting() method by sending a greeting message (see lines 6-8 below).
|
1
2
3
4
5
6
7
8
9
10
|
package com.studytrails.tutorials.springremotinghttpinvokerserver;public class GreetingServiceImpl implements GreetingService{@Overridepublic String getGreeting(String name) {return "Hello " + name + "!";}} |
Create the httpinvoker-servlet.xml file (see below).
Declare the 'greetingService' (see lines 14-15 below).
Export the 'greetingService' using Spring's HttpInvokerServiceExporter class (see lines 17-21 below).
Note the following properties of HttpInvokerServiceExporter class:
- service: the service class bean which shall handle the HTTP call (see line 19 below)
- serviceInterface: The interface to be used by Spring to create proxies for HTTP (see line 20 below)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<?xml version="1.0" encoding="UTF-8" ?>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/mvc<bean id="greetingService"class="com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingServiceImpl" /><bean name="/greetingService.http"class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"><property name="service" ref="greetingService" /><property name="serviceInterface" value="com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingService"/></bean></beans> |
Create the web.xml file (see below).
Create the servlet mapping for the url pattern '*.http' (see line 16 below) for Spring's DispatcherServlet (see line 10 below)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!DOCTYPE web-app PUBLIC"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"<web-app><display-name>Spring Remoting: Http Invoker Server</display-name><servlet><servlet-name>httpinvoker</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>httpinvoker</servlet-name><url-pattern>*.http</url-pattern></servlet-mapping></web-app> |

Create the GreetingService interface as shown below.
Copy the GreetingService interface created for Http Invoker Server (described above) and paste it in Http Invoker Client source code while retaining the java package structure.
Note: For reference, the source code is shown below.
|
1
2
3
4
5
6
|
package com.studytrails.tutorials.springremotinghttpinvokerserver;public interface GreetingService {String getGreeting(String name);} |
Create a class TestSpringRemotingHttpInvoker shown below to test Spring Http Invoker Remoting.
Load spring configuration file (see line 11 below)
Get a reference to GreetingService using the bean name 'greetingService' (see line 12 below)
Call the GreetingService.getGreting() method by passing the name 'Alpha' (see line 13 below)
Print the greeting message (see line 14 below).
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.studytrails.tutorials.springremotinghttpinvokerclient;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingService;public class TestSpringRemotingHttpInvoker {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");GreetingService greetingService = (GreetingService)context.getBean("greetingService");String greetingMessage = greetingService.getGreeting("Alpha");System.out.println("The greeting message is : " + greetingMessage);}} |
Create the spring-config-client.xml file (see below).
Declare the 'greetingService' using Spring's HttpInvokerProxyFactoryBean class (see lines 13-16 below).
Note the following properties of HttpInvokerProxyFactoryBean class:
- serviceUrl : refers the URL of the remote service (see line 14 below).
Note URL part 'greetingService.http' corresponds to bean name property of HttpInvokerServiceExporter bean defined in httpinvoker-servlet.xml (defined earlier) - serviceInterface: The interface to be used by Spring to create proxies for Http Invoker (see line 15 below)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<?xml version="1.0" encoding="UTF-8"?>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="<bean id="greetingService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"><property name="serviceUrl" value="http://localhost:8080/springremotinghttpinvokerserver/greetingService.http"/><property name="serviceInterface" value="com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingService"/></bean></beans> |
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
- Save the springremotinghttpinvokerserver-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment

(Alternatively you can go the folder containing the springremotinghttpinvokerserver-installer.jar and execute the jar using java -jar springremotinghttpinvokerserver-installer.jarcommand)
- You will see a wizard page as shown below

- Enter the location of the directory where you want the program to install and run (say, C:\Temp)

- The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
This shows that the Http Invoker Server program has run successfully on your machine

This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies)on your machine and automatically run the program for you as shown in the steps below. To run the sampleprogram, you only need Java Runtime Environment (JRE) on your machine and nothing else.
- Save the springremotinghttpinvokerclient-installer.jar on your machine
- Execute/Run the jar using Java Runtime Environment

(Alternatively you can go the folder containing the springremotinghttpinvokerclient-installer.jar and execute the jar using java -jar springremotinghclient-installer.jar command)
- You will see a wizard page as shown below

- Enter the location of the directory where you want the program to install and run (say, C:\Temp)

- The installer will copy the program on your machine and automatically execute it. The expected output indicating that the program has run successfully on your machine is shown in the image below.
This shows that the Http Invoker Client program has run successfully on your machine

This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotinghttpinvokerserver . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
The WAR file for this example is available as springremotinghttpinvokerserver.war in the download folder specified by you earlier (e.g. C:\Temp). The path for the WAR file is <DOWNLOAD_FOLDER_PATH>/springremotinghttpinvokerserver/dist/springremotinghttpinvokerserver.war.
This WAR file can be deployed in any webserver of your choice and example can be executed. 
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called springremotinghttpinvokerclient . All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
Spring Remoting: HTTP Invoker--转的更多相关文章
- Spring Remoting by HTTP Invoker Example--reference
Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for ...
- Spring Remoting: Remote Method Invocation (RMI)--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...
- Lingo (Spring Remoting) : Passing client credentials to the server
http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...
- Spring Remoting: Burlap--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...
- Spring Remoting: Hessian--转
原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...
- Asynchronous calls and remote callbacks using Lingo Spring Remoting
http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...
- Spring远程调用技术<3>-Spring的HTTP Invoker
前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙. 另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...
- spring3.2.2 remoting HTTP invoker 实现方式
最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加memcached等的缓存系统,感觉挺好的,很大程度上的降低耦合,然后还明确分配了数据层跟应用层任务.也方便定位.找到问题 ...
- spring remoting源码分析--Hessian分析
1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...
随机推荐
- [ZT]Language codes – MFC
Below is table with all MFC language codes. I think it can be sometimes very useful. First column c ...
- Android兼容包multidex的开发和构建方法
在Android开发中,函数方法超过65k限制后,我们就常常会用到multidex分包解决,但是multidex的配置,对系统apk的构建.签名.打包复杂性大大的增加,严重的降低了构建效率.那这个问题 ...
- cmdCreateViewTag
start ).Y < grid.Curve.get_EndPoint().Y) { grid = gridTmp; ...
- Mac OS X 系统12个常用的文本编辑快捷键(移动、选中)
经常和文字处理打交道?如果多多使用下面这 12 个快捷键,在移动.选择.复制等操作文字时效率会大大提升. 6 个移动光标的快捷键第一组快捷键可以用来在文本中快速移动光标: 跳到本行开头 – Comma ...
- 使用Facebook的SDK判斷來訪者是否已經按讃并成為本站粉絲團的成員
今天公司裡要做活動,其中有一項活動內容是要求來訪者按一下facebook粉絲團的讃,按了讃之後贈送現金.Facebook被墻大家眾所周知,在百度搜了一下發現因為被墻的原因導致國內涉及到Facebook ...
- C#:注册机的实现【提供源代码下载】
代码下载 C#软件注册机 软件运行结果 参考文章 http://www.cnblogs.com/hanzhaoxin/archive/2013/01/04/2844191.html
- 利用Android Studio、MAT对Android进行内存泄漏检测
利用Android Studio.MAT对Android进行内存泄漏检测 Android开发中难免会遇到各种内存泄漏,如果不及时发现处理,会导致出现内存越用越大,可能会因为内存泄漏导致出现各种奇怪的c ...
- Unity3D Shader入门指南(一)
动机 自己使用Unity3D也有一段时间了,但是很多时候是流于表面,更多地是把这个引擎简单地用作脚本控制,而对更深入一些的层次几乎没有了解.虽然说Unity引擎设计的初衷就是创建简单的不需要开发者操心 ...
- AndroidStudio小技巧--依赖库
同步发表于http://avenwu.net/2015/02/12/androidstudio_library_dependency Fork on github https://github.com ...
- OsmocomBB 编译安装
工具: sudo apt-get install libtool shtool autoconf git-core pkg-config make gcc gnuarm: ## 32 bit wget ...