Hessian


Hessian是远程调用的一种技术,和WebService类似,但不同的是较WebService而言,它更轻量级,更简单,更快速。

关于Hessian更详细全面的介绍可以查看http://hessian.caucho.com/

下面就用一个例子来简单的使用Hessian。

一、创建服务端:

1、在Eclipse建立一个Maven webapp项目hessian,如图:

Hessian

2、在项目中添加Hessian的依赖pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>hessian</groupId>

  <artifactId>com.xt.hessian</artifactId>

  <packaging>war</packaging>

  <version>0.0.1-SNAPSHOT</version>

  <name>com.xt.hessian Maven Webapp</name>

  <url>http://maven.apache.org</url>

  <dependencies>

     <dependency>

         <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <version>4.8.2</version>

     </dependency>

    <dependency>

<groupId>com.caucho</groupId>

<artifactId>hessian</artifactId>

<version>4.0.7</version>

     </dependency>

    <dependency>

    <groupId>javax.servlet</groupId>

    <artifactId>servlet-api</artifactId>

    <version>2.5</version>

    </dependency>

  </dependencies>

  <build>

    <finalName>com.xt.hessian</finalName>

  </build>

</project>

3、添加一个提供Hessian服务的接口和实现类:

package com.xt.hessian;

/**

     * @ClassName: HessianService

     * @Description: Hessian服务接口

     * @date 2015-04-29 下午10:41:26

     * 

     */

public interface HessianService {

public String sayHello(String name);

}




package com.xt.hessian.impl;

import com.caucho.hessian.server.HessianServlet;

import com.xt.hessian.HessianService;

public class HessianServiceImpl extends HessianServlet implements HessianService   {


/**

* @Description: Hessian服务接口实现类

*/

private static final long serialVersionUID = 1L;


@Override

    public String sayHello(String name) {

       return "hello " + name;

    }

}


4、web.xml配置:


<?xml version="1.0" encoding="UTF-8"?> 


<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 


    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 


    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 


  <display-name>Archetypes Created Web Application</display-name>

  <servlet>

      <servlet-name>service</servlet-name>

      <servlet-class>com.xt.hessian.impl.HessianServiceImpl</servlet-class>

   </servlet>

  <servlet-mapping>

     <servlet-name>service</servlet-name>

     <url-pattern>/service</url-pattern>

  </servlet-mapping>

</web-app>

二、创建客户端:创建测试类:


package hessian;


import java.net.MalformedURLException;


import org.junit.Test;


import com.caucho.hessian.client.HessianProxyFactory;

import com.xt.hessian.HessianService;


public class HessianServiceTest {

private final static String SERVICE_URL = "http://localhost:8088/hessian/service";

 

@Test

public void testSayHello() {

HessianProxyFactory proxyFactory = new HessianProxyFactory();

try {

HessianService hessianService = (HessianService)            proxyFactory.create(HessianService.class, SERVICE_URL);

               System.out.println(hessianService.sayHello("hessian!!!"));

            } catch (MalformedURLException e) {

               e.printStackTrace();

            }

 }

}


运行结果:

Hessian

hello hessian!!!


三、注意:如果服务端不启动,会报如下错误:

Hessian


















本文转自lzf0530377451CTO博客,原文链接:http://blog.51cto.com/8757576/1640280 ,如需转载请自行联系原作者