程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 小試Hessian實現Webservice

小試Hessian實現Webservice

編輯:關於JAVA

Hessian是一個輕量級的remoting on http工具,采用的是Binary RPC協議,所以它很適合於發送二進制數據,同時又具有防火牆穿透能力。Hessian一般是通過Web應用來提供服務,因此非常類似於平時我們用的WebService。只是它不使用SOAP協議,但相比webservice而言更簡單、快捷。

Hessian官網:http://hessian.caucho.com/

Hessian可通過Servlet提供遠程服務,需要將匹配某個模式的請求映射到Hessian服務。也可Spring框架整合,通過它的DispatcherServlet可以完成該功能,DispatcherServlet可將匹配模式的請求轉發到Hessian服務。Hessian的server端提供一個servlet基類, 用來處理發送的請求,而Hessian的這個遠程過程調用,完全使用動態代理來實現的,,建議采用面向接口編程,Hessian服務通過接口暴露。

Hessian處理過程示意圖:

客戶端——>序列化寫到輸出流——>遠程方法(服務器端)——>序列化寫到輸出流 ——>客戶端讀取輸入流——>輸出結果

下面詳細介紹最常用的兩種方式實現Hessian提供webservice:

純Hessian實現

配合Spring框架實現

在開始之前當然需要到官網上下載相關的lib包,放入項目的/WEB-INF/lib/下

【一】、純Hessian實現步驟:

1.基本代碼

首先編寫一個服務的接口類:HelloHessian.Java

Java代碼

  1. package michael.hessian;
  2. import Java.util.List;
  3. import Java.util.Map;
  4. /**
  5. * @author michael
  6. *
  7. */
  8. public interface HelloHessian {
  9. String sayHello();
  10. MyCar getMyCar();
  11. List myLoveFruit();
  12. Map myBabays();
  13. }

一個java bean文件MyCar.Java:

Java代碼

  1. package michael.hessian;
  2. import Java.io.Serializable;
  3. /**
  4. * @author michael
  5. *
  6. */
  7. public class MyCar implements Serializable {
  8. /**
  9. *
  10. */
  11. private static final long serialVersionUID = 4736905401908455439L;
  12. private String carName;
  13. private String carModel;
  14. /**
  15. * @return the carName
  16. */
  17. public String getCarName() {
  18. return carName;
  19. }
  20. /**
  21. * @return the carModel
  22. */
  23. public String getCarModel() {
  24. return carModel;
  25. }
  26. /**
  27. * @param pCarName the carName to set
  28. */
  29. public void setCarName(String pCarName) {
  30. carName = pCarName;
  31. }
  32. /**
  33. * @param pCarModel the carModel to set
  34. */
  35. public void setCarModel(String pCarModel) {
  36. carModel = pCarModel;
  37. }
  38. /**
  39. * @see Java.lang.Object#toString()
  40. * @return
  41. */
  42. @Override
  43. public String toString() {
  44. return "my car name:[" + this.carName + "] model:[" + this.carModel
  45. + "].";
  46. }
  47. }

服務端接口的實現類:HelloHessianImpl.Java

Java代碼

  1. package michael.hessian.impl;
  2. import Java.util.ArrayList;
  3. import Java.util.HashMap;
  4. import Java.util.List;
  5. import Java.util.Map;
  6. import michael.hessian.HelloHessian;
  7. import michael.hessian.MyCar;
  8. /**
  9. * @author michael
  10. *
  11. */
  12. public class HelloHessianImpl extends HessianServlet implements HelloHessian {
  13. public MyCar getMyCar() {
  14. MyCar car = new MyCar();
  15. car.setCarName("阿斯頓·馬丁");
  16. car.setCarModel("One-77");
  17. return car;
  18. }
  19. public Map myBabays() {
  20. Map map = new HashMap();
  21. map.put("son", "孫吳空");
  22. map.put("daughter", "孫小美");
  23. return map;
  24. }
  25. public List myLoveFruit() {
  26. List list = new ArrayList();
  27. list.add("apple");
  28. list.add("kiwi");
  29. list.add("orange");
  30. return list;
  31. }
  32. public String sayHello() {
  33. return "welcom to Hessian";
  34. }
  35. }

2.配置文件web.XML修改

在web.XML配置文件裡增加如下信息:

XML代碼

  1. <servlet>
  2. <servlet-name>HelloHessianservlet-name>
  3. <servlet-class>
  4. com.caucho.hessian.server.HessianServlet
  5. servlet-class>
  6. <init-param>
  7. <param-name>home-classparam-name>
  8. <param-value>michael.hessian.impl.HelloHessianImplparam-value>
  9. init-param>
  10. <init-param>
  11. <param-name>home-apiparam-name>
  12. <param-value>michael.hessian.HelloHessianparam-value>
  13. init-param>
  14. <load-on-startup>1load-on-startup>
  15. servlet>
  16. <servlet-mapping>
  17. <servlet-name>HelloHessianservlet-name>
  18. <url-pattern>/HessianServiceurl-pattern>
  19. servlet-mapping>
  20. <servlet-mapping>

3.Java客戶端驗證

Java代碼

  1. package michael.hessian.clIEnt;
  2. import Java.Net.MalformedURLException;
  3. import Java.util.Map;
  4. import michael.hessian.HelloHessian;
  5. import michael.hessian.MyCar;
  6. import com.caucho.hessian.clIEnt.HessianProxyFactory;
  7. /**
  8. * @author michael
  9. *
  10. */
  11. public class HessianClIEntTest {
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. String url = "http://localhost:8082/J2EE_sJSky/HessianService";
  17. HessianProxyFactory factory = new HessianProxyFactory();
  18. try {
  19. HelloHessian hello = (HelloHessian) factory.create(
  20. HelloHessian.class, url);
  21. System.out.println(hello.sayHello());
  22. MyCar car = hello.getMyCar();
  23. System.out.println(car.toString());
  24. for (Map.Entry entry : hello.myBabays().entrySet()) {
  25. System.out.println(entry.getKey() + " " + entry.getValue());
  26. }
  27. for (String str : hello.myLoveFruit()) {
  28. System.out.println(str);
  29. }
  30. } catch (MalformedURLException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

運行結果如下:

Java代碼

  1. welcom to Hessian
  2. my car name:[阿斯頓·馬丁] model:[One-77].
  3. daughter 孫小美
  4. son 孫吳空
  5. apple
  6. kiwi
  7. orange

【二】、spring+hessian 實現服務端:

1.基本代碼見上面的demo

2.修改配置文件

在web.XML,增加內容如下:

XML代碼

  1. <servlet>
  2. <servlet-name>springhessianservlet-name>
  3. <servlet-class>
  4. org.springframework.web.servlet.DispatcherServlet
  5. servlet-class>
  6.     
  7.         1    
  8.         
  9.     
  10.         
  11.         springhessian    
  12.         /springhessian/*    
  13.        

在/WEB-INF/增加一個叫servlet-name-servlet.xml配置文件。該文件的命名有一定的規則,紅色servlet-name需要和web.XML中的springhessian定義的名稱相匹配,比如本例子應叫springhessian-servlet.XML,這樣簡單也不容易出錯。

當然該文件也可以自己任意命名。如果是自定義的文件名稱不符合上述默認的規則,需要在中增加相關屬性,人為指定加載配置文件,否則會報錯。

范例:springhessian-servlet.XML

XML代碼

  1.     
  2.     
  3.     
  4.     
  5.         class="michael.hessian.impl.HelloHessianImpl" />    
  6.     
  7. <bean name="/helloHessianService"
  8. class="org.springframework.remoting.caucho.HessianServiceExporter">
  9. <property name="service" ref="helloHessianImpl" />
  10. <property name="serviceInterface"
  11. value="michael.hessian.HelloHessian" />
  12. bean>
  13. beans>

3.Java客戶端驗證

和上面的例子相似,只需要把訪問的url替換成新的即可如下:

Java代碼

  1. String url=http://localhost:8082/J2EE_sJSky/springhessian/helloHessianService

運行結果和上面例子一樣。

4.spring配置客戶端

增加一個spring的bean配置文件hessian-clIEnt.XML

XML代碼

  1. >
  2. <beans>
  3. <bean id="helloHessianClIEnt"
  4. class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
  5. <property name="serviceUrl">
  6. <value>
  7. http://localhost:8082/J2EE_sJSky/springhessian/helloHessianService
  8. value>
  9. property>
  10. <property name="serviceInterface"
  11. value="michael.hessian.HelloHessian" />
  12. bean>
  13. beans>

測試代碼HessianSpringClIEnt.Java:

Java代碼

  1. package michael.hessian.clIEnt;
  2. import Java.util.Map;
  3. import michael.hessian.HelloHessian;
  4. import michael.hessian.MyCar;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXMLApplicationContext;
  7. /**
  8. * @author michael
  9. *
  10. */
  11. public class HessianSpringClIEnt {
  12. /**
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. try {
  17. ApplicationContext context = new ClassPathXMLApplicationContext(
  18. "hessian-clIEnt.XML");
  19. HelloHessian hello = (HelloHessian) context
  20. .getBean("helloHessianClIEnt");
  21. System.out.println(hello.sayHello());
  22. MyCar car = hello.getMyCar();
  23. System.out.println(car.toString());
  24. for (Map.Entry entry : hello.myBabays().entrySet()) {
  25. System.out.println(entry.getKey() + " " + entry.getValue());
  26. }
  27. for (String str : hello.myLoveFruit()) {
  28. System.out.println(str);
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }

運行結果和上面完全一致。

5.com.caucho.hessian.io.HessianProtocolException: expected string at 0x6d異常處理

我在spring+hessian整合測試過程中,客戶端調用時,發生了異常,而服務端錯誤內容如下:

Java代碼

  1. 2011-4-25 16:14:44 org.apache.catalina.core.StandardWrapperValve invoke
  2. 嚴重: Servlet.service() for servlet remoting threw exception
  3. com.caucho.hessian.io.HessianProtocolException: expected string at 0x6d
  4. at com.caucho.hessian.io.Hessian2Input.error(Hessian2Input.Java:2882)
  5. at com.caucho.hessian.io.Hessian2Input.expect(Hessian2Input.Java:2830)
  6. at com.caucho.hessian.io.Hessian2Input.readString(Hessian2Input.Java:1362)
  7. at com.caucho.hessian.io.Hessian2Input.readMethod(Hessian2Input.Java:272)
  8. at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.Java:249)
  9. at com.caucho.hessian.server.HessianSkeleton.invoke(HessianSkeleton.Java:221)
  10. at org.springframework.remoting.caucho.Hessian2SkeletonInvoker.invoke(Hessian2SkeletonInvoker.Java:67)
  11. at org.springframework.remoting.caucho.HessianServiceExporter.handleRequest(HessianServiceExporter.Java:147)
  12. at org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter.handle(HttpRequestHandlerAdapter.Java:49)
  13. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.Java:819)
  14. at org.springframework.web.servlet.DispatcherServlet.DOService(DispatcherServlet.Java:754)
  15. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.Java:399)
  16. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.Java:364)
  17. at javax.servlet.http.HttpServlet.service(HttpServlet.Java:710)
  18. at javax.servlet.http.HttpServlet.service(HttpServlet.Java:803)
  19. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:290)
  20. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:206)
  21. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:230)
  22. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:175)
  23. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:128)
  24. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.Java:104)
  25. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.Java:109)
  26. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:261)
  27. at org.apache.coyote.http11.Http11Processor.process(Http11Processor.Java:844)
  28. at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.Java:581)
  29. at org.apache.tomcat.util.Net.JIoEndpoint$Worker.run(JIoEndpoint.Java:447)
  30. at java.lang.Thread.run(Thread.Java:619)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved