一、簡介
Hessian和Burlap是由Caucho Technology提供的基於HTTP協議的輕量級遠程服務解決方案。他們都致力於借助盡可能簡單那的API和通信協議來簡化Web服務。
Hession和Burlap就如同一個事物的兩面,但是每一個解決方案都服務於略微不同的目的。Hession就像RMI一樣,使用二進制盡心客戶端和服務端的交互。但與其他二進制遠程調用技術(如,RMI)不同的是,它的二進制消息可以移植到其他開發語言中(如,PHP、Python、C++、C#)。Burlap是一種基於XML的遠程調用技術,這使得它可以自然而然的移植到任何能夠解析XML的語言上。正因為如此,Burlap比起Hessian的二進制格式而言有更強的可讀性。但,與其他基於XML的遠程技術(如,SOAP、XML-RPC)不同,Burlap的消息結構盡可能的簡單,不需要額外的外部定義語言(如,WSDL、IDL)。
你可能想知道如何在Hession和Burlap之間抉擇,很大程度,他們是一樣的。唯一的區別在於Hession的消息似乎二進制的,在帶寬上更有優勢,而Burlap的消息是XML的,有更好的可讀性。
由於Hessian和Burlap都是基於HTTP協議的,他們都解決了RMI所頭疼的防火牆滲透問題。但是當傳遞過來的RPC消息中包含序列化對象時,RMI就完勝Hessian和Burlap了。因為Hessian和Burlap都采用私有的序列化機制,如果數據模型非常復雜,那么Hessian和Burlap的序列化模型可能無法勝任。
二、Hession開發步驟
1、編寫服務接口
1 package com.cnblogs.javalouvre.service; 2 3 public interface GreetService { 4 5 String sayHello(String name); 6 7 }
2、編寫服務實現類,須繼承自com.caucho.hessian.server.HessianServlet
1 package com.cnblogs.javalouvre.service; 2 3 import com.caucho.hessian.server.HessianServlet; 4 5 public class GreetServiceImpl extends HessianServlet implements GreetService { 6 7 private static final long serialVersionUID = 1880738686281295739L; 8 9 @Override 10 public String sayHello(String name) { 11 return "Hello " + name; 12 } 13 14 }
3、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <servlet> 8 <servlet-name>HessianServlet</servlet-name> 9 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 10 <init-param> 11 <param-name>home-class</param-name> 12 <param-value>com.cnblogs.javalouvre.service.GreetServiceImpl</param-value> 13 </init-param> 14 <init-param> 15 <param-name>home-api</param-name> 16 <param-value>com.cnblogs.javalouvre.service.GreetService</param-value> 17 </init-param> 18 <load-on-startup>1</load-on-startup> 19 </servlet> 20 21 <servlet-mapping> 22 <servlet-name>HessianServlet</servlet-name> 23 <url-pattern>/GreetService</url-pattern> 24 </servlet-mapping> 25 26 <welcome-file-list> 27 <welcome-file>index.html</welcome-file> 28 <welcome-file>index.htm</welcome-file> 29 <welcome-file>index.jsp</welcome-file> 30 </welcome-file-list> 31 32 </web-app>
4、測試客戶端
1 package com.cnblogs.javalouvre.client; 2 3 import java.net.MalformedURLException; 4 5 import com.caucho.hessian.client.HessianProxyFactory; 6 import com.cnblogs.javalouvre.service.GreetService; 7 8 public class Client { 9 10 public static void main(String[] args) { 11 String url = "http://10.108.1.138:8080/Hessian/GreetService"; 12 13 try { 14 GreetService service = (GreetService) (new HessianProxyFactory()).create(GreetService.class, url); 15 System.out.println(service.sayHello("Jobs")); 16 } catch (MalformedURLException e) { 17 e.printStackTrace(); 18 } 19 } 20 21 }
三、Burlap開發步驟
1、編寫服務接口(同Hessian示例的接口)
2、編寫服務實現類,須繼承自com.caucho.burlap.server.BurlapServlet
1 package com.cnblogs.javalouvre.service; 2 3 import com.caucho.burlap.server.BurlapServlet; 4 5 public class GreetServiceImpl extends BurlapServlet implements GreetService { 6 7 @Override 8 public String sayHello(String name) { 9 return "Hello " + name; 10 } 11 12 }
3、配置web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 5 version="2.5"> 6 7 <servlet> 8 <servlet-name>GreetService</servlet-name> 9 <servlet-class>com.cnblogs.javalouvre.service.GreetServiceImpl</servlet-class> 10 <load-on-startup>1</load-on-startup> 11 </servlet> 12 13 <servlet-mapping> 14 <servlet-name>GreetService</servlet-name> 15 <url-pattern>/GreetService</url-pattern> 16 </servlet-mapping> 17 18 <welcome-file-list> 19 <welcome-file>index.html</welcome-file> 20 <welcome-file>index.htm</welcome-file> 21 <welcome-file>index.jsp</welcome-file> 22 </welcome-file-list> 23 24 </web-app>
4、測試客戶端
1 package com.cnblogs.javalouvre.client; 2 3 import java.net.MalformedURLException; 4 5 import com.caucho.burlap.client.BurlapProxyFactory; 6 import com.cnblogs.javalouvre.service.GreetService; 7 8 public class Client { 9 10 public static void main(String[] args) { 11 String url = "http://10.108.1.138:8080/Burlap/GreetService"; 12 13 try { 14 GreetService service = (GreetService) (new BurlapProxyFactory()).create(GreetService.class, url); 15 System.out.println(service.sayHello("Jobs")); 16 } catch (MalformedURLException e) { 17 e.printStackTrace(); 18 } 19 } 20 21 }