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 ...
随机推荐
- STL的string和wstring
STL有字符串处理类——stirng和wstring,但是用的时候会觉得不是很方便,因为它不能像TCHAR一样根据定义的宏在char类型字符串和wchar_t进行转换,总不能因为程序要Unicode就 ...
- 分享20个吸引眼球的高品质免费PSD网站模板
当你设计网站的时候,你需要一个美丽的界面来吸引你的听众.在这篇文章中,我将分享一些吸引眼球的商业PSD模板,你可以从中受到启发 EaglesTroop Business Bonfire Pocket ...
- vector的 emplace 和 insert 以及使用vector进行iterator遍历 且 erase的时候注意事项
vector<int> first;//Size()==2 first.push_back(); first.push_back(); //first.insert(2); vector& ...
- Node-restify 简介
restify 是Node.js的模块.虽然restify的API或多或少的参考了express,但restify不是一个MVC框架,它是一套为了能够正确构建REST风格API而诞生的框架. http ...
- [算法导论]哈希表 @ Python
直接寻址方式: class HashTable: def __init__(self, length): self.T = [None for i in range(length)] class Da ...
- Asp.net Core 使用Redis存储Session
前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...
- 【LeetCode】258. Add Digits (2 solutions)
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Sublime Text 使用技巧
之前就一直在用Sublime Text 来作为默认的文本编辑工具,但也只是简单的用用,一些Sublime Text本身的快捷键什么的都没研究过,今天特地在网上看了一下,快捷键比较多,要想熟练运用还得在 ...
- iOS客户端的在线安装和更新——针对ADHoc证书
这篇文章纯给自己留个备份,所以对AdHoc证书内部分发和对iOS客户端开发不了解的请直接无视. 一般在iOS游戏或应用开发过程中,正式发布到App Store之前,都需要内部的测试,客户端的安装是个不 ...
- 【网络编程】——connect函数遇见EINTR的处理
最近在公司项目中突然报错如下 “connect: Interrupted system call”, 经过查找代码发现是在创建 socket 中执行了 connect 函数失败导致.上网查阅资料发现这 ...