Posted on 2010-01-27 21:42
瘋狂 閱讀(8215)
評論(0) 編輯 收藏 所屬分類:
spring
簡介
相比WebService,Hessian更簡單、快捷。采用的是二進制RPC協議(Binary),因為采用的是二進制協議,所以它很適合于發送二進制數據。Hessian通常通過Web應用來提供服務,因此非常類似于WebService。只是它不使用SOAP協議。
Hessian通過Servlet提供遠程服務。需要將匹配某個模式的請求映射到Hessian服務。Spring的DispatcherServlet可以完成該功能,DispatcherServlet可將匹配模式的請求轉發到Hessian服務。Hessian的server端提供一個servlet基類, 用來處理發送的請求,而Hessian的這個遠程過程調用,完全使用動態代理來實現的,,推薦采用面向接口編程,因此,Hessian服務建議通過接口暴露。
Hessian處理過程示意圖:
客戶端——>序列化寫到輸出流——>遠程方法(服務器端)——>序列化寫到輸出流 ——>客戶端讀取輸入流——>輸出結果
環境搭建
Hessian的下載和安裝請按如下步驟進行:
(1)登http://www.caucho.com/hessian/下載Hessian。
(2)把Hessian相應的Jar包放到Web應用下,所有的jar文件都應該放在WEB-INF/lib下,該文件也不例外。
兩種方式
純Hessian
這種方式主要是適用于工程中沒有適用像spring框架的情況下,好處是配置方便,但是當內容多的情況下,配置的內容很多。
下面我就把我在做實例的一些相關步驟描述如下:
1:把Hessian相應jar包放入至工程中。
2:由于Hessian是采用面向接口編程的,所以編寫一個接口,因為客戶端僅僅需要接口,而無須真實的實現類。
package jzh.demo;
public interface IHello {
String sayHello();
}
3:編寫一個類實現這個接口。
package jzh.demo.impl;
import jzh.demo.IHello;
import com.caucho.hessian.server.HessianServlet;
public class Hello extends HessianServlet implements IHello {
public String sayHello() {
return "Hello world";
}
}
4:web.xml的詳細配置
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class>
<init-param>
<param-name>home-class</param-name>
<param-value>jzh.demo.imple.Hello</param-value>
</init-param>
<init-param>
<param-name>home-api</param-name>
<param-value>jzh.demo.IHello</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
5:客戶端遠程調用服務器端提供的接口,利用的就是Hessian的HessianProxyFactory,來實現遠程代理。
1) 把服務器端的生成的jar包,放入工程中。
2) 相應的片段程序如下:
String url = "http://220.114.108.185:8080/Hessian/Hello";
HessianProxyFactory factory = new HessianProxyFactory();
try {
IHello hello =(IHello)factory.create(IHello.class,url);
System.out.println(hello.sayHello());
} catch (MalformedURLException e) {
e.printStackTrace();
}
6:功能完成。
Hessian與Spring整合
相比上一種方式,這個方式就有點麻煩了。Hessian通過Servlet提供遠程服務。需要將匹配某個模式的請求映射到Hessian服務。Spring的DispatcherServlet可以完成該功能,DispatcherServlet可將匹配模式的請求轉發到Hessian服務,web.xml只是定義了“請求轉發器”,該轉發器將匹配/remoting/*的請求截獲,轉發給context的bean處理。而HessianServiceExporter提供bean服務。
所以Hessian與Spring整合主要就是一下兩個工作:
1:通過DispatcherServlet來攔截URL請求。
2:HessianServiceExporter提供bean服務,Spring使用HessianServiceExporter,將一個常規bean導出成Hessian服務。
下面我就把我在做實例的一些相關步驟描述如下:
1:和上面的一樣。
2:和上面的一樣。
3:和上面的一樣。
4:web.xml的詳細配置
<servlet>
<servlet-name>remoting</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>remoting</servlet-name>
<url-pattern>/remoting/*</url-pattern>
</servlet-mapping>
5:配置remoting-servlet.xml文件
<!-- 定義普通bean實例-->
<bean id="hello" class="jzh.demospring.impl.Hello"/>
<!-- 使用HessianServiceExporter 將普通bean導出成Hessian服務-->
<bean name="/HessianRemoting" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 需要導出的目標bean-->
<property name="service" ref="hello"/>
<!-- Hessian服務的接口-->
<property name="serviceInterface" value="jzh.demospring.IHello"/>
</bean>
6:客戶端定義一個remote-client.xml文件
<bean id="myServiceClient" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl">
<value>http://220.114.99.62:8080/HessianSpring/remoteing/HessianRemoting</value>
</property>
<property name="serviceInterface">
<value>jzh.demospring.IHello</value>
</property>
</bean>
7:客戶端調用。
try
{
ApplicationContext context = new ClassPathXmlApplicationContext("remote-client.xml");
IHello hello =(IHello)context.getBean("myServiceClient");
System.out.println(hello.sayHello());
}
catch (Exception e)
{
e.printStackTrace();
}
轉載自:http://hi.baidu.com/javahua/blog/item/289e4a66c50a152dab184cec.html