一、簡介
Hessian是一個由Caucho Technology開發的輕量級二進制RPC協議,因為采用的是二進制協議,所以它很適合於發送二進制數據。下面以hessian-3.0.20版本為例演示如何將Hessian整合到Spring中。
二、配置詳解
1、在web.xml中的配置
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- /WEB-INF/config/applicationContext.xml,
- /WEB-INF/Hessian-servlet.xml
- </param-value>
- </context-param>
- <servlet>
- <servlet-name>Hessian</servlet-name>
- <servlet-class>
- org.springframework.web.servlet.DispatcherServlet
- </servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Hessian</servlet-name>
- <url-pattern>/hessian/*</url-pattern>
- </servlet-mapping>
1)Hessian要求遠程服務通過Servlet暴露出來,所以我們使用Spring的DispatcherServlet來暴露我們的服務。
2)我們必須在WEB-INF目錄下創建一個文件名格式為 [Servlet Name]-servlet.xml 的配置文件,由於我們設定servlet-name為Hessian,所以我們在這里創建一個名為Hessian-servlet.xml的文件。
2、Hessian-servlet.xml文件的配置
- <!-- 業務類 -->
- <bean id="hessianService" class="com.cjm.webservice.hessian.HessianServiceImpl"/>
- <!-- 遠程服務 -->
- <bean name="/hessianService" class="org.springframework.remoting.caucho.HessianServiceExporter">
- <property name="service" ref="hessianService"/>
- <property name="serviceInterface">
- <value>
- com.cjm.webservice.hessian.HessianService
- </value>
- </property>
- </bean>
1)實際業務類是通過Spring的HessianServiceExporter類來暴露給客戶端的。
2)service:指定服務對應的業務類。
3)serviceInterface:指定業務類實現哪個接口。Spring推薦采用面向接口編程,因此,Hessian服務建議通過接口暴露。
4)Hessian的遠程服務名為/hessianService。筆者使用的web服務器是Tomcat-5.5.23,端口是8888,web應用名為spring2,則遠程服務的URL為:http://localhost:8888/spring2/hessian/hessianService。
3、業務類源代碼
- //接口類:
- public interface HessianService {
- public String sayHello(String username);
- public HessianModel getHessianModel(String username, String password);
- }
- //實現類:
- public class HessianServiceImpl implements HessianService {
- public String sayHello(String username){
- return "Hello " + username;
- }
- public HessianModel getHessianModel(String username, String password) {
- return new HessianModel(username, password);
- }
- }
- //領域模型類:
- public class HessianModel implements Serializable{
- private String username;
- private String password;
- public HessianModel(String username, String password){
- this.username = username;
- this.password = password;
- }
- ……
- }
4、客戶端調用服務范例
1)在Jsp頁面中調用服務
- String url = "http://localhost:8888/spring2/hessian/hessianService";
- HessianProxyFactory factory = new HessianProxyFactory();
- HessianService hessianServer =
- (HessianService)factory.create(HessianService.class, url);
- String ret = hessianServer.sayHello("Raymond.chen");
- out.print(ret);
- HessianModel model = hessianServer.getHessianModel("uid", "pwd");
- out.print("username: " + model.getUsername() + "<br>");
A)結果顯示:Hello Raymond.chen
B)客戶端程序必須引用hessian-3.0.20.jar文件和遠程服務對應的接口類。
C)當調用的遠程服務方法返回一個自定義類時,該自定義類必須實現Serializable接口。
2)在Spring環境中調用服務
- <bean id="testHessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
- <property name="serviceUrl" value="http://localhost:8888/spring2/hessian/hessianService"/>
- <property name="serviceInterface" value="com.cjm.webservice.hessian.HessianService"/>
- </bean>
- <!- Struts2中調用服務 -->
- <bean id="orgAction" class="com.cjm.web.action.OrganizationAction" parent="baseAction">
- <property name="organizationService" ref="organizationService"/>
- <property name="testHessianService" ref="testHessianService"/>
- </bean>
OrganizationAction.java部分源代碼:
- private HessianService testHessianService;
- HessianModel model = testHessianService.getHessianModel("uid", "pwd");
- System.out.println("username: " + model.getUsername());
A)使用HessianProxyFactoryBean來連接服務。
B)serviceUrl:遠程服務的URL。
C)serviceInterface:服務對應的接口類。
