十五分钟学会用Hessian

了解Hessian

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

使用Hessian

在Eclipse建立一个Maven webapp项目:

十五分钟学会用Hessian

右键项目添加Hessian的依赖:

十五分钟学会用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

打开该页面说明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; }结果:

十五分钟学会用Hessian

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

上一篇:python学习笔记09-python编码与解码


下一篇:Saltstack自动化操作记录(1)-环境部署