转载自http://www.cnblogs.com/yjmyzz/p/hessian-helloworld.html

hessian是一个采用二进制格式传输的服务框架,相对传统soap web service,更轻量,更快速。官网地址:http://hessian.caucho.com/

目前已经支持N多语言,包括:java/c#/flex/php/ruby...

maven的依赖项如下:

1 <dependency>
2 <groupId>com.caucho</groupId>
3 <artifactId>hessian</artifactId>
4 <version>4.0.37</version>
5 </dependency>

入门示例:

一、服务端开发

1.1 先建服务接口

1 package yjmyzz.cnblogs.com.service;
2
3 public interface HelloService {
4
5 public String helloWorld(String message);
6 }

1.2 提供服务实现

 1 package yjmyzz.cnblogs.com.service.impl;
2
3 import yjmyzz.cnblogs.com.service.HelloService;
4
5 public class HelloServiceImpl implements HelloService {
6
7 @Override
8 public String helloWorld(String message) {
9 return "hello," + message;
10 }
11
12 }

1.3 修改web.xml

 1 <!DOCTYPE web-app PUBLIC
2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3 "http://java.sun.com/dtd/web-app_2_3.dtd" >
4
5 <web-app>
6 <display-name>hessian-showcase</display-name>
7
8 <welcome-file-list>
9 <welcome-file>index.jsp</welcome-file>
10 </welcome-file-list>
11
12 <servlet>
13 <servlet-name>hessian-service</servlet-name>
14
15 <servlet-class>
16 com.caucho.hessian.server.HessianServlet
17 </servlet-class>
18
19 <init-param>
20 <param-name>home-class</param-name>
21 <param-value>
22 <!-- 服务实现类 -->
23 yjmyzz.cnblogs.com.service.impl.HelloServiceImpl
24 </param-value>
25 </init-param>
26
27 <init-param>
28 <param-name>home-api</param-name>
29 <!-- 服务接口 -->
30 <param-value>yjmyzz.cnblogs.com.service.HelloService</param-value>
31 </init-param>
32
33 </servlet>
34
35 <servlet-mapping>
36 <servlet-name>hessian-service</servlet-name>
37 <url-pattern>/hessian</url-pattern>
38 </servlet-mapping>
39
40 </web-app>

部署到tomcat或其它web容器中即可。
1.4 导出服务接口jar包

最终服务是提供给客户端调用的,客户端必须知道服务的接口信息(包括接口方法中的传输dto定义),所以得将这些java文件导出成jar,提供给调用方。

方法很简单:eclipse中在接口package(包括dto对应的package)上右击,选择Export

再选择Jar File

二、客户端调用

同样先添加maven的hessian依赖项,同时引入上一步导出的服务接口jar包,然后参考下面的示例代码:

 1 import java.net.MalformedURLException;
2 import org.junit.Test;
3 import yjmyzz.cnblogs.com.service.HelloService;
4 import com.caucho.hessian.client.HessianProxyFactory;
5
6
7 public class ServiceTest {
8 @Test
9 public void testService() throws MalformedURLException {
10
11 String url = "http://localhost:8080/hessian-showcase/hessian";
12 System.out.println(url);
13
14 HessianProxyFactory factory = new HessianProxyFactory();
15 HelloService helloService = (HelloService) factory.create(HelloService.class, url);
16 System.out.println(helloService.helloWorld("jimmy"));
17
18 }
19 }

三、与Spring的整合

spring-web包里提供的org.springframework.remoting.caucho.HessianServiceExporter类,可以将普通方法导出成hessian服务。关键是解决org.springframework.web.servlet.DispatcherServlet的url访问路径问题,一般情况下,我们是这样配置的

 1     <!-- spring mvc -->
2 <servlet>
3 <servlet-name>appServlet</servlet-name>
4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5 <init-param>
6 <param-name>contextConfigLocation</param-name>
7 <param-value>classpath:servlet-context.xml</param-value>
8 </init-param>
9 <load-on-startup>1</load-on-startup>
10 <async-supported>true</async-supported>
11 </servlet>
12
13 <servlet-mapping>
14 <servlet-name>appServlet</servlet-name>
15 <url-pattern>/</url-pattern>
16 </servlet-mapping>

这是spring mvc的入口,拦截所有访问路径,可以把这一节再复制一份,追加在后面,只不过url-pattern指定成特定的规则

 1 <!-- spring mvc -->
2 <servlet>
3 <servlet-name>appServlet</servlet-name>
4 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5 <init-param>
6 <param-name>contextConfigLocation</param-name>
7 <param-value>classpath:servlet-context.xml</param-value>
8 </init-param>
9 <load-on-startup>1</load-on-startup>
10 <async-supported>true</async-supported>
11 </servlet>
12
13 <servlet-mapping>
14 <servlet-name>appServlet</servlet-name>
15 <url-pattern>/</url-pattern>
16 </servlet-mapping>
17
18
19 <!-- hessian -->
20 <servlet>
21 <servlet-name>hessianServlet</servlet-name>
22 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
23 <init-param>
24 <param-name>contextConfigLocation</param-name>
25 <param-value>classpath:hessian-context.xml</param-value>
26 </init-param>
27 <load-on-startup>1</load-on-startup>
28 </servlet>
29
30 <servlet-mapping>
31 <servlet-name>hessianServlet</servlet-name>
32 <url-pattern>/hessian/*</url-pattern>
33 </servlet-mapping>

这样,所有以/hessian/开头的访问路径,约定成hessian服务地址,详细配置在hessian-context.xml中,内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
8
9
10 <bean id="helloServiceImpl" class="com.cnblogs.yjmyzz.service.hessian.support.HelloServiceImpl" />
11
12 <!-- 使用HessianServiceExporter 将普通bean导出成Hessian服务 -->
13 <bean name="/service"
14 class="org.springframework.remoting.caucho.HessianServiceExporter">
15 <property name="service" ref="helloServiceImpl" />
16 <!-- Hessian服务的接口 -->
17 <property name="serviceInterface" value="com.cnblogs.yjmyzz.service.hessian.HelloService" />
18 </bean>
19
20 </beans>

这样,就能直接以http://localhost:8080/spring-mvc4-rest/hessian/service 发布hessian服务了

再来看看客户端如何整合,类似的,我们需要一个配置文件,比如:hessian-client.xml,内容如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://www.springframework.org/schema/context
7 http://www.springframework.org/schema/context/spring-context.xsd">
8
9 <bean id="hessianClient"
10 class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
11 <property name="serviceUrl">
12 <value>http://localhost:8080/spring-mvc4-rest/hessian/service</value>
13 </property>
14 <property name="serviceInterface">
15 <value>com.cnblogs.yjmyzz.service.hessian.HelloService</value>
16 </property>
17 </bean>
18
19 </beans>

调用示例:

 1 package com.cnblogs.yjmyzz.test;
2 import java.net.MalformedURLException;
3
4 import org.junit.Test;
5 import org.springframework.context.ApplicationContext;
6 import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8 import com.cnblogs.yjmyzz.service.hessian.HelloService;
9
10 public class HessianServiceTest {
11 @SuppressWarnings("resource")
12 @Test
13 public void testService() throws MalformedURLException {
14 ApplicationContext context = new ClassPathXmlApplicationContext(
15 "hessian-client.xml");
16 HelloService hello = (HelloService) context.getBean("hessianClient");
17 System.out.println(hello.helloWorld("jimmy.yang"));
18 }
19 }

示例源码地址:http://code.taobao.org/p/spring-mvc4-rest/src/

[转载] hessian学习的更多相关文章

  1. [转载] Java学习之Hessian通信基础

    转载自http://blog.sina.com.cn/s/blog_7f73e06d0100xn9j.html 一.首先先说Hessian是什么?    Hessian:hessian是一个轻量级的r ...

  2. 【转载】学习资料存档:jQuery的deferred对象详解

    我在以前的文章里提到promise和deferred,这两个东西其实是对回调函数的一种写法,javascript的难点之一是回调函数,但是我们要写出优秀的javascript代码又不得不灵活运用回调函 ...

  3. 转载:学习Entity Framework 中的Code First

    看完觉得不错,适合作为学习资料,就转载过来了 原文链接:http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html 这是上周就写 ...

  4. 转载-python学习笔记之输入输出功能读取和写入数据

    读取.写入和 Python 在 “探索 Python” 系列以前的文章中,学习了基本的 Python 数据类型和一些容器数据类型,例如tuple.string 和 list.其他文章讨论了 Pytho ...

  5. 转载-Python学习笔记之文件读写

    Python 文件读写 Python内置了读写文件的函数,用法和C是兼容的.本节介绍内容大致有:文件的打开/关闭.文件对象.文件的读写等. 本章节仅示例介绍 TXT 类型文档的读写,也就是最基础的文件 ...

  6. 轻量级远程调用框架-Hessian学习笔记-Demo实现

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议,因为采用的是二进制协 ...

  7. 转载——JavaScript学习笔记:取数组中最大值和最小值

    转载自:http://www.w3cplus.com/javascript/calculate-the-max-min-value-from-an-array.html. 取数组中最大值 可以先把思路 ...

  8. Java学习之路-Hessian学习

    Hessian是基于HTTP的轻量级远程服务解决方案,Hessian像Rmi一样,使用二进制消息进行客户端和服务器端交互.但与其他二进制远程调用技术(例如Rmi)不同的是,它的二进制消息可以移植其他非 ...

  9. 转载OPENCV学习随笔

    转载自 亦轩Dhc http://www.cnblogs.com/daihengchen/p/5492729.html 学习笔记:使用opencv做双目测距(相机标定+立体匹配+测距).   最近在做 ...

随机推荐

  1. Python自学笔记-面向对象编程(Mr seven)

    类的成员可以分为三大类:字段.方法和属性. 一.字段 字段包括:普通字段和静态字段,他们在定义和使用中有所区别,而最本质的区别是内存中保存的位置不同, 普通字段属于对象 静态字段属于类 二.方法 方法 ...

  2. python之HTMLParser解析HTML文档

    HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...

  3. 分布式锁的实现(redis)

    1.单机锁 考虑在并发场景并且存在竞态的状况下,我们就要实现同步机制了,最简单的同步机制就是加锁. 加锁可以帮我们锁住资源,如内存中的变量,或者锁住临界区(线程中的一段代码),使得同一个时刻只有一个线 ...

  4. Ubuntu14.04LTS下安装Node.js&NPM以及个人博客hexo的初始化配置

    什么是hexo Hexo 是一款基于node 的静态博客网站生成器作者 :tommy351是一个台湾的在校大学生...相比其他的静态网页生成器而言有着,生成静态网页最快,插件丰富(已经移植了大量Oct ...

  5. BZOJ-3709-[PA2014]Bohater(贪心)

    Description 在一款电脑游戏中,你需要打败n只怪物(从1到n编号).为了打败第i只怪物,你需要消耗d[i]点生命值,但怪物死后会掉落血药,使你恢复a[i]点生命值.任何时候你的生命值都不能降 ...

  6. jsp中的盲区-面试可能会问到的东西

    1.今天看到一个有趣的JSP题目. <body>    This is my JSP page. <br>    <%        int a = 10;    %&g ...

  7. Chrome等浏览器下出现net::ERR_BLOCKED_BY_CLIENT的解决办法

    当我们在做开发时,调试页面图片会出现部分图片无法正常显示,并且确认图片的地址正确: 按F12 Debug查看报错原因,提示net::ERR_BLOCKED_BY_CLIENT错误,但当我们点击图片地址 ...

  8. Struts2运行流程-源码剖析

    本文为原创,如需转载,请标明出处:http://www.cnblogs.com/gudu1/p/7726172.html 首先说一下查看这些框架源码的感受,每一次深入探究 Spring.Struts ...

  9. Android SDK国内更新

    恩,废话就不多说了,俩网址,mirrors.neusoft.edu.cn和ubuntu.buct.edu.cn,在这里感谢wecloud和北京化工大学,十分感谢提供方便之门

  10. [mysql使用(3)] 使用mysql的时候遇到的一些错误

    1.Err1055,出现这个问题往往是在执行sql语句时候,在最后一行会出现这个问题. [Err] 1055 - Expression #1 of ORDER BY clause is not in ...