试着写了一个Hessian的例子,是参考caucho官网上的一个example,很简单,也没什么实际的意义,但足以领会Hessian的用法。

1、建立一个Remote Interface

package  com.hessian.test;

public   interface  MathService  {
 
    
public int add(int a, int b);

}


   2、Service Implementation
    

package  com.hessian.test;

import  com.caucho.hessian.server.HessianServlet;

public   class  HessianMathService  extends  HessianServlet  implements  MathService  {

   
public int add(int a, int b){
      
return a + b;
   }


}


     官网上的例子是没有实现MathService接口的,而且也能运行成功,但我觉得有点不合逻辑,不应该将该实现类作为MathService接口暴露给client端。

3、web.xml
    

< servlet >
   
< servlet-name > math </ servlet-name >
   
< servlet-class >
      com.hessian.test.HessianMathService
   
</ servlet-class >
</ servlet >
< servlet-mapping >
   
< servlet-name > math </ servlet-name >
   
< url-pattern > /hessian/math </ url-pattern >
</ servlet-mapping >


4、Java client
     

public   class  HessianClientTest  {
   
public static void main(String[] args){
       String url 
= "http://localhost:8080/hessiantest/hessian/math";
       HessianProxyFactory factory 
= new HessianProxyFactory();
       MathService math 
= null;
       
try {
           math 
= (MathService)factory.create(MathService.class, url);
       }
 catch (MalformedURLException e) {
           System.out.println(
"occur exception: " + e);
       }

       System.out.println(
"3 + 2 = " + math.add(32));
   }

}


   使用java实现的client,通过HessianProxyFactory的create即可获取到服务接口。

5、python client

from  hessianlib  import  Hessian

url 
=   " http://localhost:8080/hessiantest/hessian/math "
proxy 
=  Hessian(url)
print   " 2 + 3 = " , proxy.add( 2 3 )


   使用python实现的client,需加入hessianlib.py。

   以上就是一个完整的Hessian实现。

   Spring也提供了对Hessian的集成,若使用spring,server端的service实现类则不需实现HessianServlet,使用Spring的DispatcherServlet来配置一个Servlet暴露你的服务。
   web.xml
    

< servlet >
   
< servlet-name > remote </ servlet-name >
   
< servlet-class >         
org.springframework.web.servlet.DispatcherServlet
   
</ servlet-class >
   
< load-on-startup > 1 </ load-on-startup >
</ servlet >

< servlet-mapping >
   
< servlet-name > remote </ servlet-name >
   
< url-pattern > /remote/* </ url-pattern >
</ servlet-mapping >


   还需要在 WEB-INF 目录里创建一个名为 remote-servlet.xml(remote为你配置的servlet名)的应用上下文。
   remote-servlet.xml

< bean  id ="mathService"  class =" com.hessian.test.HessianMathService" >
</ bean >

< bean  name ="/math"          class ="org.springframework.remoting.caucho.HessianServiceExporter" >
   
< property  name ="service"  ref =" mathService " />
   
< property  name ="serviceInterface"  value =" com.hessian.test.MathService " />
</ bean >


server端做以上操作即可。

client端可以延用之前的操作,若使用spring则可通过 HessianProxyFactoryBean在客户端连接服务,在spring的配置中加入:

< bean  class ="com.hessian.test.HessianClientTest" >
     
< property  name ="mathService"  ref ="mathService" />
</ bean >

< bean  id =" mathService "          class ="org.springframework.remoting.caucho.HessianProxyFactoryBean" >
   
< property  name ="serviceUrl"              value ="http://remotehost:8080/hessiantest/remote/math" />
   
< property  name ="serviceInterface"  value ="com.hessian.test.MathService" />
</ bean >


加入以上的配置后,就可像使用其他的bean一样去操作了。原来实现一个webservice是可以这么简单的。