一、简介
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 @Override10 public String sayHello(String name) {11 return "Hello " + name;12 }13 14 }
3、配置web.xml
1 26 7 8 20 21HessianServlet 9com.caucho.hessian.server.HessianServlet 1011 14home-class 12com.cnblogs.javalouvre.service.GreetServiceImpl 1315 18home-api 16com.cnblogs.javalouvre.service.GreetService 171 1922 25 26HessianServlet 23/GreetService 2427 31 32index.html 28index.htm 29index.jsp 30
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.BurlapServlet1 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 26 7 8 12 13GreetService 9com.cnblogs.javalouvre.service.GreetServiceImpl 101 1114 17 18GreetService 15/GreetService 1619 23 24index.html 20index.htm 21index.jsp 22
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 }