您的位置:首页 > 其它

hessian技术的使用

2014-03-12 15:03 197 查看
一、为什么选择hessian?

       在工作中有这么一种情况,两个开发团队在为同一个项目服务时,虽然功能模块不同,但需要一个团队为另一个团队提供数据,发布时只能将项目整合在一起,这样就存在让人揪心的问题,(开发环境不同,jar包冲突问题......等等),这时可以选择使用hessian。

1、hessian类型与webservice,但比webservice简单N倍

2、使用hessian需要考虑以下几点:

      服务端

    1)、 项目中需要包含Hessian的jar包   

     2)、设计一个接口,用来给客户端调用

     3)、实现该接口的功能

     4)、配置web.xml,配好相应的servlet

     5)、对象必须实现Serializable 接口

     客户端

     1)、java客户端包含Hessian.jar的包

     2)、具有和服务器端结构一样的接口,包括命名空间

     3)、利用HessianProxyFactory调用远程接口

测试代码如下:

服务端:

新建项目hessian_test、导入hessian.jar包

创建接口类

package com.hessian.interfaces;

public interface Hessiantest {

 String getData();

}

接口实现类

package com.hessian.interfaces.impl;

import com.hessian.interfaces.Hessiantest;

public class HessiantestImpl implements Hessiantest{

 public String getData() {

  // TODO Auto-generated method stub

  return "test......";

 }

}
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">

 <servlet>

   <servlet-name>hessian_test</servlet-name>

   <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>

   <init-param>

    <param-name>service-class</param-name>

    <!-- 配置接口实现类 -->

    <param-value>com.hessian.interfaces.impl.HessiantestImpl</param-value>

   </init-param>

  </servlet>

  <servlet-mapping>

   <servlet-name>hessian_test</servlet-name>

   <url-pattern>/hessian_test</url-pattern>

  </servlet-mapping>

 <welcome-file-list>

  <welcome-file>index.jsp</welcome-file>

 </welcome-file-list>

</web-app>

客户端:

package com.test;

import java.net.MalformedURLException;

import com.caucho.hessian.client.HessianProxyFactory;

import com.hessian.interfaces.Hessiantest;

public class Test {

 public static void main(String[] args) throws MalformedURLException {

  String url ="http://localhost:8080/hessian_test/hessian_test";

  HessianProxyFactory factory = new HessianProxyFactory();

  Hessiantest hessiantest=(Hessiantest) factory.create(Hessiantest.class, url);

  System.out.println("运行结果:"+hessiantest.getData());

 }

}

运行结果如下:

运行结果:test......

简单的项目调用基本就OK了,后续用到深层次会继续编写。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  技术 webservice hessian