一、简介
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:服务对应的接口类。