試著寫了一個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;
   }


}


     官網上的例子是沒有實現(xiàn)MathService接口的,而且也能運行成功,但我覺得有點不合邏輯,不應該將該實現(xiàn)類作為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實現(xiàn)的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(23)


   使用python實現(xiàn)的client,需加入hessianlib.py。

   以上就是一個完整的Hessian實現(xiàn)。

   Spring也提供了對Hessian的集成,若使用spring,server端的service實現(xiàn)類則不需實現(xiàn)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 目錄里創(chuàng)建一個名為 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一樣去操作了。原來實現(xiàn)一個webservice是可以這么簡單的。