Hessian、Burlap是由Caucho Technology(http://www.caucho.com/)所提出,透過HTTP實現的遠程服務。
Hessian是將物件以中性的二進位訊息使用HTTP進行傳送,而不若RMI使用Java的序列化格式,由於該二進位訊息是中性的,因此不受限於某種程式語言所實現的客戶端或伺服端,二進位資料在傳輸時所需的頻寬較小是其優點。
Burlap則是將物件以XML文件格式進行傳送,XML文件且有較高的可讀性,只要應用程式可以剖析XML文件就可以解讀所接收的訊息,當然也不受限於某種語言所實現的客戶端與伺服端。
在Spring中使用Hessian及Burlap的方法是類似的,由於Hessian、Burlap是透過HTTP傳送,所以在使用它們時要搭配 Spring Web框架來使用,也就是使用到DispatcherServlet,舉個實際的例子來示範如何使用Hessian,以 RMI 介紹的RMI例子來說,可以直接使用已撰寫好的ISomeService、SomeServiceImpl,而伺服端要在 web.xml中配置DispatcherServlet:
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
→ http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/service-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping>
</web-app>
在Hessian的伺服端這邊,使用org.springframework.remoting.caucho.HessianServiceExporter來發佈服務:
- service-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="urlMapping"
class="org.springframework.web.servlet.
→ handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/some.service">serviceExporter</prop>
</props>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.
→ view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="someService"
class="onlyfun.caterpillar.SomeServiceImpl"/>
<bean id="serviceExporter"
class="org.springframework.remoting.
→ caucho.HessianServiceExporter">
<property name="service">
<ref bean="someService"/>
</property>
<property name="serviceInterface">
<value>onlyfun.caterpillar.ISomeService</value>
</property>
</bean>
</beans>
注意到在SimpleUrlHandlerMapping的設置上,請求some.service的會轉發給serviceExporter,在這邊您不 用註冊服務名稱,也就是沒有"serviceName"屬性, Hessian、Burlap不需要,如果您使用的是Burlap,則設定上在serviceExporter的"class"屬性只要改用 org.springframework.remoting.caucho.BurlapServiceExporter類別即可。
接下來您只要啟動Servlet容器,在載入以上設計Web應用程式之後,則Hessian伺服端就會啟動了,記得要在您的lib目錄中加入 Hessian所需的API類別之.jar檔案,這可以使用Spring下載檔案中lib目錄下caucho目錄的hessian-2.1.12.jar 檔案,如果使用Burlap的話,當然要記得必須加入Burlap API所需的.jar檔案。
Hessian客戶端的撰寫則可以使用先前撰寫的RMIClientDemo專案來改寫,事實上只要修改一下Bean定義檔即可,例如:
- hessian-client.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="someServiceProxy"
class="org.springframework.remoting.
→ caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>
http://localhost:8080/HessianServerDemo/some.service
</value>
</property>
<property name="serviceInterface">
<value>onlyfun.caterpillar.ISomeService</value>
</property>
</bean>
</beans>
注意到"serviceUrl"屬性的設定,它是個標準的HTTP請求位址,來撰寫個簡單的客戶端程式以使用Hessian伺服器上的服務:
- HessianClient.java
package onlyfun.caterpillar;
import org.springframework.context.ApplicationContext;
import org.springframework.context.
support.FileSystemXmlApplicationContext;
public class HessianClient {
public static void main(String[] args) {
ApplicationContext context =
new FileSystemXmlApplicationContext(
"hessian-client.xml");
ISomeService service =
(ISomeService) context.getBean("someServiceProxy");
String result1 = service.doSomeService("Some request");
System.out.println(result1);
int result2 = service.doOtherService(1);
System.out.println(result2);
}
}
同樣的必須記得,您要加入包括Hessian API的.jar檔案,執行的結果與 RMI 是相同的,如果您要使用Burlap,則設定上只要改用org.springframework.remoting.caucho.BurlapProxyFactoryBean即可,當然要記得必須加入Burlap API所需的.jar檔案。