Hessian 是一種二進制 HTTP 遠程調用協議,配合 Spring 的 HessianServiceExporter 與 HessianProxyFactoryBean,幾行配置即可發布/調用服務,比傳統 Web Service 輕量得多。本文給出服務端與客戶端完整配置示例。
<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>
<!-- 業務實現 -->
<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"/>
</bean>
String url = "https://localhost:8888/spring2/hessian/hessianService";
HessianProxyFactory factory = new HessianProxyFactory();
HessianService service = (HessianService) factory.create(HessianService.class, url);
String ret = service.sayHello("Raymond.chen");
<bean id="hessianService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="https://localhost:8888/spring2/hessian/hessianService"/>
<property name="serviceInterface" value="com.weijy.webservice.hessian.HessianService"/>
</bean>
注入後像普通 Bean 一樣使用:hessianService.sayHello(...)。
Serializable)。HessianProxyFactory.setHttpClient)復用連接。