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

Concept Overview

The previous tutorial presents an overview of spring remoting and lists down various remoting protocols supported by Spring. In this tutorial we look at Spring support for Hessian. Hessian is a web service protocol that transfers binary data between a remote service and its client. Hessian has been released by Caucho Technology. It requires that the web service be hosted on an http server. The client uses HTTP protocol to invoke remote methods on the server. The important classes are : - 
org.springframework.remoting.caucho.HessianServiceExporter - This exports the specified service as a servlet based http request handler. Hessian services exported by this class can be accessed by any hessian client. 
org.springframework.remoting.caucho.HessianProxyFactoryBean - This is the factory bean for Hessian clients. The exposed service is configured as a spring bean. The ServiceUrl property specifies the URL of the service and theServiceInterface property specifies the interface of the implemented service.

Sample Program Overview

The example below is a GreetingService implemented as a remote Hessian service. 

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
  • hessian-3.1.5.jar
Interaction Flow

  • Client sends a message call
  • This message call is handled by a Hessian Proxy created by HessianProxyFactoryBean
  • The Hessian Proxy converts the call into a remote call over HTTP
  • The Hessian Service Adapter created by HessianServiceExporter intercepts the remote call over HTTP
  • It forwards the method call to Service
Hessian Server Code Package Structure

Hessian 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.springremotinghessianserver;
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.springremotinghessianserver;
public class GreetingServiceImpl implements GreetingService{
@Override
public String getGreeting(String name) {
return "Hello " + name + "!";
}
}

Create the hessian-servlet.xml file (see below).

Declare the 'greetingService' (see lines 14-15 below).

Export the 'greetingService' using Spring's HessianServiceExporter class (see lines 17-21 below). 
Note the following properties of HessianServiceExporter class:

  • service: the service class bean which shall handle the Hessian call (see line 18 below)
  • serviceInterface: The interface to be used by Spring to create proxies for Hessian (see line 19 below)
hessian-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" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="greetingService"
class="com.studytrails.tutorials.springremotinghessianserver.GreetingServiceImpl" />
<bean name="/greetingService.http"
class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="greetingService" />
<property name="serviceInterface" value="com.studytrails.tutorials.springremotinghessianserver.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
18
19
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Spring Remoting: Hessian Server</display-name>
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>

Hessian Client Code Package Structure

Hessian Client Source Code

Create the GreetingService interface as shown below. 
Copy the GreetingService interface created for Hessian Server (described above) and paste it in Hessian 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.springremotinghessianserver;
public interface GreetingService {
String getGreeting(String name);
}

Create a class TestSpringRemotingHessian shown below to test Spring Hessian 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).

TestSpringRemotingHessian.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.studytrails.tutorials.springremotinghessianclient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.studytrails.tutorials.springremotinghessianserver.GreetingService;
public class TestSpringRemotingHessian {
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 HessianProxyFactoryBean class (see lines 13-16 below). 
Note the following properties of HessianProxyFactoryBean class:

  • serviceUrl : refers the URL of the remote service (see line 14 below). 
    Note URL part 'greetingService' corresponds to bean name property of HessianServiceExporter bean defined in hessian-servlet.xml (defined earlier)
  • serviceInterface: The interface to be used by Spring to create proxies for Hessian (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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="greetingService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:8080/springremotinghessianserver/greetingService.http"/>
<property name="serviceInterface" value="com.studytrails.tutorials.springremotinghessianserver.GreetingService"/>
</bean>
</beans>

Running Sample Program
Hessian 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 Hessian Server Sample Program
  • Save the springremotinghessianserver-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


(Alternatively you can go the folder containing the springremotinghessianserver-installer.jar and execute the jar using java -jar springremotinghessianserver-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 Hessian Server program has run successfully on your machine

Hessian 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 Hessian Client Sample Program
  • Save the springremotinghessianclient-installer.jar on your machine
  • Execute/Run the jar using Java Runtime Environment


(Alternatively you can go the folder containing the springremotinghessianclient-installer.jar and execute the jar using java -jar springremotinghessianclient-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 Hessian Client program has run successfully on your machine

Browsing the Program
Hessian 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 springremotinghessianserver . 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 springremotinghessianserver.war in the download folder specified by you earlier (e.g. C:\Temp). The path for the WAR file is <DOWNLOAD_FOLDER_PATH>/springremotinghessianserver/dist/springremotinghessianserver.war. 
This WAR file can be deployed in any webserver of your choice and example can be executed. 

Hessian 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 springremotinghessianclient . 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.

转载于:https://www.cnblogs.com/davidwang456/p/5480808.html

Spring Remoting: Hessian--转相关推荐

  1. Spring整合Hessian

    Spring整合Hessian from:http://lavasoft.blog.51cto.com/62575/191871 Spring让Hessian变得不但强大,而且易用,但是易用背后,却有 ...

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

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

  3. Spring Remoting: HTTP Invoker--转

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

  4. Spring Remoting: Burlap--转

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

  5. http invoker_Http Invoker的Spring Remoting支持

    http invoker Spring HTTP Invoker是Java到Java远程处理的重要解决方案. 该技术使用标准的Java序列化机制通过HTTP公开服务,并且可以看作是替代方法,而不是He ...

  6. Http Invoker的Spring Remoting支持

    Spring HTTP Invoker是Java到Java远程处理的重要解决方案. 该技术使用标准的Java序列化机制通过HTTP公开服务,并且可以被视为替代解决方案,而不是Hessian和Burla ...

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

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

  8. spring源码分析之spring-web remoting模块概况及基本概念

    spring-web总体分为三部分:caucho.httpinvoker.jaxws,其总体构造图如下: uml结构: 先看看网上搜索到的上述实现的原理吧:Spring RMI,Hessian/Bur ...

  9. (转载)spring jar包详细介绍

    spring.jar是包含有完整发布的单个jar包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到spring-moc ...

最新文章

  1. 小程序这件事 撸起袖子加油干
  2. 医生还未失业,IBM Watson已跌入深渊 | 极客头条
  3. XML(一)XML大揭秘
  4. 企业网络推广专员浅析企业网络推广日常维护要做好
  5. Linux 常用命令与设置
  6. js判断是否为数字_第23题:JavaScript 中如何判断变量是否为数字 ?
  7. 插件完整_紫天学习星球教学:布料模拟插件完整功能使用详解01(中文)
  8. visio科学图形包_科学网—科研必备:几款好用的流程图工具,助力你的论文/科研绘图...
  9. 使用ajax获取用户所在地的天气
  10. 浅淡 RxJS WebSocket
  11. oracle批量替换保留字,常见的oracle保留字
  12. 怎么把文本文档变成html,如何将word文档转换成txt文本
  13. 华为防火墙配置IPSEC实现二个站点间网络互通 隧道模式 CLI配置 (三)
  14. vue-awesome-swiper滑动失效的问题解决方案
  15. 打印机语言PCL与PostScript的比较
  16. Minecraft mod制作简易教程(三)——创建一个物品
  17. mysql to sqlserver_mysql to sqlserver
  18. Axure RP 9 for Mac 中文版 专业产品原型设计工具
  19. MM模块物料-供应商-PO-表
  20. Google Chrome Windows平台稳定版离线安装包下载

热门文章

  1. 一个电脑能装几块固态_花了20000块给电脑升级了磁盘阵列,速度达到10G连续读写...
  2. python dateformatter_Python dates.DateFormatter方法代码示例
  3. asp 表格渐变颜色_加班到半夜,同事却用WPS表格小技巧10分钟搞定工作!
  4. 服务端程序的初步实现
  5. python输入123输出321_C语言编程:输出一个3位整数的逆序数,如输入123,输出321....
  6. html打开新窗口设置窗口属性,HTML之:让网页中的a标签属性统一设置-如‘新窗口打开’...
  7. 访问其他程序中的数据(ContentResolver的CRUD操作)
  8. vins中imu融合_双目版 VINS 项目发布,小觅双目摄像头作为双目惯导相机被推荐...
  9. Fifth Week:Node.js学习
  10. kaggle 房价预测经典文章