原文地址: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 RMIHessian 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

Required Libraries
  • 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
Interaction Flow

  • 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
Http Invoker Server Code Package Structure

Http Invoker Server Source Code

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).

GreetingService.java
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).

GreetingServiceImpl.java
1
2
3
4
5
6
7
8
9
10
package com.studytrails.tutorials.springremotinghttpinvokerserver;
 
public class GreetingServiceImpl implements GreetingService{
 
@Override
public 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)
httpinvoker-servlet.xml
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" ?>
 
 
 
<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)

web.xml
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>
Http Invoker Client Code Package Structure

Http Invoker Client Source Code

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.

GreetingService.java
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).

TestSpringRemotingHttpInvoker.java
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)
spring-config-client.xml
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"?>
xsi:schemaLocation="
 
<bean id="greetingService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceInterface" value="com.studytrails.tutorials.springremotinghttpinvokerserver.GreetingService"/>
</bean>
 
</beans>
Running Sample Program
Http Invoker Server Sample Program

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.

Download And Automatically Run Http Invoker Server Sample Program
  • 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

Http Invoker Client Sample Program

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.

Download And Automatically Run Http Invoker Client Sample Program
  • 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

Browsing the Program
Http Invoker Server Sample Code

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.

Redeploying this sample program in a different web server

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. 

Http Invoker Client Sample Code

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--转的更多相关文章

  1. Spring Remoting by HTTP Invoker Example--reference

    Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for ...

  2. Spring Remoting: Remote Method Invocation (RMI)--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-rmi.jsp Concept Overview Spring pr ...

  3. Lingo (Spring Remoting) : Passing client credentials to the server

    http://www.jroller.com/sjivan/entry/lingo_spring_remoting_passing_client Lingo (Spring Remoting) : P ...

  4. Spring Remoting: Burlap--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-burlap.jsp Concept Overview In the ...

  5. Spring Remoting: Hessian--转

    原文地址:http://www.studytrails.com/frameworks/spring/spring-remoting-hessian.jsp Concept Overview The p ...

  6. Asynchronous calls and remote callbacks using Lingo Spring Remoting

    http://www.jroller.com/sjivan/entry/asynchronous_calls_and_callbacks_using Asynchronous calls and re ...

  7. Spring远程调用技术<3>-Spring的HTTP Invoker

    前面提到RMI使用java标准的对象序列化机制,但是很难穿透防火墙.  另一方面,Hessian和Burlap能很好地穿透防火墙,但是使用私有的对象序列化机制. Spring提供的http invke ...

  8. spring3.2.2 remoting HTTP invoker 实现方式

    最近跟朋友聊天,聊到他们现在项目的架构都是把数据层跟应用层分离开来,中间可以加memcached等的缓存系统,感觉挺好的,很大程度上的降低耦合,然后还明确分配了数据层跟应用层任务.也方便定位.找到问题 ...

  9. spring remoting源码分析--Hessian分析

    1. Caucho 1.1 概况 spring-remoting代码的情况如下: 本节近分析caucho模块. 1.2 分类 其中以hession为例,Hessian远程服务调用过程: Hessian ...

随机推荐

  1. JS_Ajax基础

    一:Ajax ajax 的全称是Asynchronous(异步) JavaScript and XML 在不刷新页面的情况下从服务器获取,提交数据的一种数据交互方式; 二:Ajax使用步骤概括 //1 ...

  2. 在C#中如何读取枚举值的描述属性

    在C#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...

  3. 关于年终奖励的扣税算法BUG

    这么多年,第一次拿年终奖,于是查一下年终奖是怎么扣税的,根据 国税发[2005]9号 适用公式为: 应纳税额=雇员当月取得全年一次性奖金×适用税率一速算扣除数 年终奖: /= 的税率是3% 按照网上说 ...

  4. Jenkins 插件 CIFS

    Jenkis编译后我们往往需要把文件发布的其他的服务器上,典型的插件如下:   Publish Over CIFS Plugin   Publish Over FTP Plugin   Publish ...

  5. ES5 数组方法map

    概述 map() 方法返回一个由原数组中的每个元素调用一个指定方法后的返回值组成的新数组. 语法 array.map(callback[, thisArg]) 参数 callback 原数组中的元素经 ...

  6. 曲率已驱动了头发——深度分析谷歌AlphaGo击败职业棋手

    这篇是我们自开设星际随笔以来写得最长的一篇.我们也花了不少力气.包括把那5盘棋各打了两遍的谱,包括从Nature官网上把那篇谷歌的报告花了200元下载下来研究它的算法(后来发现谷 歌网站上可以免费下载 ...

  7. LANDR:在线母带处理

    二前年没看这报道,我就有这样的想法.最近也在完成个别功能,但还是慢,原因有二个:1) 一个人做太慢了,这个要做好有太多工作要做:2) 音乐相关知识功底太差,很多时间在学基础的乐理知识. LANDR是一 ...

  8. SAP 系统管理内容

    SAP 系统管理内容包含非常广泛,从底层硬件起到各种操作系统及各种系统软件及SAP软件组件等都会涉及到.SAP系统支持主流的IBM AIX.HP UNIX.Windows.Linux平台及Oracle ...

  9. Android 5.0 Default SMS App以及运营商授权SMS App

    已同步更新至个人blog:http://dxjia.cn/2015/08/android-5-default-sms-app/ 题外话:博友们有没有好用的写博客客户端推荐啊,cnblogs推荐的win ...

  10. Lua在给定范围内,生成指定个数不重复随机数组

    本篇主要是参考 lua连续随机数 这篇文章完成.大家可以去原贴查看学习. 生成随机数组,暂时发现两种方法 1.把生成的数放到一个表里面,每次随机时判断这个表里是否有,若有再随机一次(问了朋友,很多人都 ...