`
longforfreedom
  • 浏览: 196946 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Hessian4.0.2笔记(一)

    博客分类:
  • JAVA
阅读更多

Hessian是什么

 

Hessian is a simple binary protocol for connecting web services. The com.caucho.hessian.client and com.caucho.hessian.server packages do not require any other Resin classes, so can be used in smaller clients, like applets.

Because Hessian is a small protocol, J2ME devices like cell-phones can use it to connect to Resin servers. Because it's powerful, it can be used for EJB services.

The Hessian specification itself is a short and interesting description.

 

 

The Hessian binary web service protocol makes web services usable without requiring a large framework, and without learning yet another alphabet soup of protocols. Because it is a binary protocol, it is well-suited to sending binary data without any need to extend the protocol with attachments.

 

JAVA创建Hessian服务只需四步

 

1。创建服务接口

2。使用HessianProxyFactory创建客户端

3。服务器端实现服务接口

4。配置Servlet

 

下面将用将使用Netbeans6.8+Hessian4.0.2,经过以上四步创建一个简单的远程方法调用示例

一、创建项目

      分别创建一个名为HessianServer的JavaWeb项目和一个名为HessianClient的Java项目,

      将HessianServer设为主项目(只是方便一点,不设也无所谓)

      HessianServer上下文路径为/HessianServer(默认路径)!!!

      将hessian-4.0.2.jar分别添加至以上项目的Classpath中 下载地址http://hessian.caucho.com/

二、创建Hessian服务示例

1.创建服务接口

具体服务通过此服务器端此接口的实现类来提供,客户端通过此接口来调用实现的服务,所以HessianServer和HessianClient中都要有此接口

package org.migle.hessian;
/**
 *Hessian服务接口
 * @author migle longforfreedom@gmail.com
 * @version 0.1 2009-12-13
 *
 */
public interface Hello {

    public String sayHello(String smt);
    public void printHello(String smt);
}

 

 

2.使用HessianProxyFactory创建客户端

package org.migle.hessianclient;

import com.caucho.hessian.client.HessianProxyFactory;
import java.net.MalformedURLException;
import org.migle.hessian.Hello;

/**
 *Hessian客户端
 * @author migle longforfreedom@gmail.com
 * @version  0.1 2009-12-13
 */
public class Main {

    public static void main(String[] args) throws MalformedURLException {
        String url = "http://localhost/HessianServer/hessian";
        HessianProxyFactory factory = new HessianProxyFactory();
        Hello d = (Hello) factory.create(Hello.class, url);
        System.out.println(d.sayHello("migle"));//打印从服务器端获取的字符串
        d.printHello("Hessian"); //在服务器端控制台打印 "Hello Hessian"
    }
}

 

 

3.服务器端的服务接口实现

服务接口的实现可以是扩展自HessianServlet,也可以是直接一个POJO的实现来回避依赖于HessianServlet

<?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">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>hessian</servlet-name>
        <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
        <init-param>
            <param-name>service-class</param-name>
            <param-value>org.migle.hessian.impl.HelloImpl</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>hessian</servlet-name>
        <url-pattern>/hessian</url-pattern>
    </servlet-mapping>
</web-app>

 

 4.Servet配置

package org.migle.hessian.impl;

import org.migle.hessian.Hello;

/**
 * 服务接口的具体实现
 * @author migle
 * @version v0.1 2009-12-13
 */
public class HelloImpl implements Hello {

    public String sayHello(String smt) {
        return smt != null ? "hello " + smt : "hello hessian";
    }

    public void printHello(String smt) {
        System.out.println("Hello " + smt);
    }
}

 

 三、运行结果:

按F6运行HessianServer。

打开HessianClient中的Main.java按Shift+F6运行,HessianClient的输出如下所示:


Tomcat控制台输入如下所示:


可以看到调用 sayHello方法时,返回一个String到客户端,而printHello方法则直接在服务器端控制台输出。

 

 

Hessian基本Http以二进行方式传输数据,不依赖于特定的Servelt引擎,使用简单,没有复杂的配置,根本不需要什么插件之类的来配合开发。

相当小巧,Hessian4.0.2只有不到340KB,Hessian3只有不到300KB。

 

 

 

 

更多内容可以参考以下:

Java、PHPRPC、Hessian、Burlap、AMF3、XML 序列化的效率对比

 

主题:Hessian 机制剖析

 

  • 大小: 52.4 KB
  • 大小: 122.5 KB
2
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics