了解Hessian

Hessian是远程调用的一种技术,和WebService类似,但不同的是较WebService而言,它更轻量级,更简单,更快速。关于Hessian更详细全面的介绍可以查看http://hessian.caucho.com/。下面可以花十五分钟的时间,学会怎么简单的使用Hessian。

使用Hessian

在Eclipse建立一个Maven webapp项目:

右键项目添加Hessian的依赖:

Maven随后会去中央仓库下载Hessian相关的jar包。

添加一个提供Hessian服务的接口:

   1:  /**
   2:   * @ClassName: HessianService
   3:   * @Description: Hessian服务接口
   4:   * @author liping.action@gmail.com
   5:   * @date 2014-1-3 下午10:41:26
   6:   * 
   7:   */
   8:  public interface HessianService {
   9:      public String sayHello(String name);
  10:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

以及现实类:

   1:  public class HessianServiceImpl implements HessianService {
   2:      /*
   3:       * (non-Javadoc)
   4:       * 
   5:       * @see
   6:       * com.cnblogs.leefreeman.hessian.service.HessianService#sayHello(java.lang
   7:       * .String)
   8:       */
   9:      @Override
  10:      public String sayHello(String name) {
  11:          // TODO Auto-generated method stub
  12:          return "hello " + name;
  13:      }
  14:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

最后在web.xml中增加hessian相应的配置:

   1:  <servlet>
   2:         <servlet-name>service</servlet-name>
   3:         <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
   4:         <load-on-startup>1</load-on-startup>
   5:         <init-param>
   6:             <param-name>service-class</param-name>
   7:             <param-value>com.cnblogs.leefreeman.hessian.service.impl.HessianServiceImpl</param-value>
   8:         </init-param>
   9:      </servlet>
  10:      <servlet-mapping>
  11:         <servlet-name>service</servlet-name>
  12:         <url-pattern>/service</url-pattern>
  13:      </servlet-mapping>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

现在Hessian服务端就算完成,是不是觉得很简单。启动项目:

打开该页面说明Hessian服务端启动正常。接口下来编写客户端测试用例:

   1:  public class HessianServiceTest {
   2:      private final static String SERVICE_URL = "http://localhost:8080/hessian-project/service";
   3:   
   4:      @Test
   5:      public void testSayHello() {
   6:          HessianProxyFactory proxyFactory = new HessianProxyFactory();
   7:          try {
   8:              HessianService hessianService = (HessianService) proxyFactory
   9:                      .create(HessianService.class, SERVICE_URL);
  10:              System.out.println(hessianService.sayHello("liping"));
  11:          } catch (MalformedURLException e) {
  12:              e.printStackTrace();
  13:          }
  14:      }
  15:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }结果:

一个简单的Hession应用就完成了,相比搭建Webservice项目,要简单得多。

十五分钟学会用Hessian的更多相关文章

  1. 工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘

    关注「WeiyiGeek」公众号 设为「特别关注」每天带你玩转网络安全运维.应用开发.物联网IOT学习! 希望各位看友[关注.点赞.评论.收藏.投币],助力每一个梦想. 文章目录: 0x00 前言简述 ...

  2. 【转】三十分钟学会STL算法

    转载自: http://net.pku.edu.cn/~yhf/UsingSTL.htm 这是本小人书.原名是<using stl>,不知道是谁写的.不过我倒觉得很有趣,所以化了两个晚上把 ...

  3. 【linux】三十分钟学会AWK

    本文大部分内容翻译自我开始学习AWK时看到的一篇英文文章 AWK Tutorial ,觉得对AWK入门非常有帮助,所以对其进行了粗略的翻译,并对其中部分内容进行了删减或者补充,希望能为对AWK感兴趣的 ...

  4. [转载] 十五分钟介绍 Redis数据结构

    转载自http://blog.nosqlfan.com/html/3202.html?ref=rediszt Redis是一种面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存 ...

  5. 三十分钟学会AWK

    摘要: 本文大部分内容翻译自我开始学习AWK时看到的一篇英文文章 AWK Tutorial ,觉得对AWK入门非常有帮助,所以对其进行了粗略的翻译,并对其中部分内容进行了删减或者补充,希望能为对AWK ...

  6. 三十分钟学会 Less

    每一门技术的出现都是为了解决现存的问题,同样的,Less 的出现是为了解决 CSS 中过于呆板的写法.Less 官方文档 中对 Less 的使用有详细的介绍,总结一下为:Less = 变量 + 混合 ...

  7. 十五分钟介绍 Redis数据结构

    下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...

  8. 十五分钟介绍 Redis数据结构--学习笔记

    下面是一个对Redis官方文档<A fifteen minute introduction to Redis data types>一文的翻译,如其题目所言,此文目的在于让一个初学者能通过 ...

  9. 分分钟钟学会Python - 数据类型(set)

    目录 今日内容 具体内容 1.集合含义 2.独有方法 3.公共方法 4.特殊情况 5.总结 @ 今日内容 集合(set) 具体内容 1.集合含义 一个无序的不重复元素序列. 可以使用大括号 { } 或 ...

随机推荐

  1. string与int互换

    1:将string转化为int 1.) int i = Integer.parseInt(String s); 2.) int i = Integer.valueOf(my_str).intValue ...

  2. eclipse项目上面有个红叉,但是没有任何地方有错误

    eclipse项目上面有个红叉,但是没有任何地方有错误,clear,refresh,重启都试过了,依然没用, 后来我换了一个workspace,编译的时候提示: Description Resourc ...

  3. swift 手机号码正则表达式 记录一下

    func isTelNumber(num:NSString)->Bool { var mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$&qu ...

  4. Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  5. npm-async使用

    async.series(tasks, callback) tasks可以是对象或数组,返回类型就是参数类型 tasks中传入回调函数的第一个值非空即停止后面函数执行 按照顺序流程进行 async.s ...

  6. SNMP高速扫描器braa

    SNMP高速扫描器braa   SNMP(Simple Network Monitoring Protocol,简单网络管理协议)是网络设备管理标准协议.为了便于设备管理,现在联入网络的智能设备都支持 ...

  7. iOS how to stop a scrolling scrollView

    - (void)killScroll { CGPoint offset = scrollView.contentOffset; offset.y -= 1.0; [scrollView setCont ...

  8. three.js加载obj模型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  9. Synchronized同步性与可见性

    Synchronized是具有同步性与可见性的,那么什么是同步性与可见性呢? (1)同步性:同步性就是一个事物要么一起成功,要么一起失败,可谓是有福同享有难同当,就像A有10000去银行转5000给身 ...

  10. Python学习笔记(1)

    从今天开始正式学习python,教程看的是廖雪峰老师的Python 2.7教程.链接在此:http://www.liaoxuefeng.com/wiki/0014316089557264a6b3489 ...