项目里遇到一个问题:由于安全性的需要,必须由一个程序专门负责查询某一数据库,然后通过安全的信道(例如HTTP)将查询结果传到安全区域之外的application。为了解决这个小小的remoting问题,我们差点要动用EJB。不过幸亏朋友推荐,找到了Hessian这个轻量级的remoting on http工具。
Hessian其实很像web service,只不过它的协议不是SOAP,而是它自己规定的binary协议。Hessian的server端提供一个servlet基类,client端获得一个service接口(也就是stub)之后调用上面的方法,stub将方法调用marshal之后通过HTTP传到server,server借助reflection调用service方法。
简单、轻量。对付普通的remoting问题,Hessian足够了。我们又可以暂时忘掉EJB和RMI了。
——————————
Hessian is a simple binary protocol for connecting web services. The com.caucho.hessian.client and com.caucho.hessian.server packages do not require any other Resin classes, so can be used in smaller clients, like applets.
Because Hessian is a small protocol, J2ME devices like cell-phones can use it to connect to Resin servers. Because it's powerful, it can be used for EJB services.
The Hessian home page contains the latest information about Hessian including the Hessian specification .
Using a Hessian service from a Java client is like calling a method. The HessianProxyFactory creates proxies which act like normal Java objects, with possibility that the method might throw a protocol exception if the remote connection fails. Using HessianProxyFactory requires JDK1.3.
Each service will have a normal Java interface describing the service. The trivial hello, world example just returns a string. Because the Hessian services support Java serialization, any Java type can be used.
API for Basic service
package hessian.test;
public interface Basic {
public String hello();
}
|
The following is an example of a standalone Hessian client. The client creates a HessianProxyFactory. The client uses the factory to create client stubs with the given target URL and a Java interface for the API. The returned object is a stub implementing the API.
Hessian Client for Basic service
package hessian.test;
import com.caucho.hessian.client.HessianProxyFactory;
public class BasicClient {
public static void main(String []args)
throws Exception
{
String url = "http://www.caucho.com/hessian/test/basic";
HessianProxyFactory factory = new HessianProxyFactory();
Basic basic = (Basic) factory.create(Basic.class, url);
System.out.println("Hello: " + basic.hello());
}
}
|
That's it! There are no more complications to using the client. The service can add methods and use any Java type for parameters and results.
While most Hessian services will use Resin-CMP or Resin-EJB, to take advantage of the benefits of EJB, the Hessian library makes it possible to write services by extending HessianServlet.
Any public method is treated as a service method. So adding new methods is as easy as writing a normal Java class.
Because the service is implemented as a Servlet, it can use all the familiar servlet data in the ServletContext, just like a normal servlet.
Hello Service
package hessian.test;
import com.caucho.hessian.server.HessianServlet;
public class BasicService extends HessianServlet implements Basic {
public String hello()
{
return "Hello, world";
}
}
|
Hessian can be used for even small Java devices. The following classes from com.caucho.hessian.client can be extracted into a J2ME jar:
- MicroHessianInput
- MicroHessianOutput
- HessianRemote
- HessianServiceException
- HessianProtocolException
The following example shows the code for using a cell phone as a client. It's a bit more complicated than using the proxy, since the client is responsible for creating the connection and writing the data.
Hello, world
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
...
MicroHessianInput in = new MicroHessianInput();
String url = "http://www.caucho.com/hessian/test/basic";
HttpConnection c = (HttpConnection) Connector.open(url);
c.setRequestMethod(HttpConnection.POST);
OutputStream os = c.openOutputStream();
MicroHessianOutput out = new MicroHessianOutput(os);
out.call("hello", null);
os.flush();
is = c.openInputStream();
MicroHessianInput in = new MicroHessianInput(is);
Object value = in.readReply(null);
|
The Hessian classes can be used for serialization and deserialization.
Serialization
Object obj = ...;
OutputStream os = new FileOutputStream("test.xml");
HessianOutput out = new HessianOutput(os);
out.writeObject(obj);
os.close();
|
Deserialization
InputStream is = new FileInputStream("test.xml");
HessianInput in = new HessianInput(is);
Object obj = in.readObject(null);
is.close();
|
分享到:
相关推荐
Hessian是一种轻量级的远程方法调用(RMI)协议,它基于HTTP并采用二进制编码,使得网络传输效率更高。Hessian的目标是提供一种简单、快速的方式来进行跨网络的服务调用,尤其是在处理Java与Java之间或者Java与其他...
hessian是一个轻量级的Java Remoting方案
标题中的“Hessian remoting on http”指的是在HTTP协议上运行的Hessian远程调用机制,这是一种轻量级的RPC(Remote Procedure Call)解决方案。 Hessian的核心是其二进制协议,该协议设计的目标是提供一种比XML更...
在IT行业中,分布式服务框架Dubbo是阿里巴巴开源的一款高性能、轻量级的服务治理方案,它使得服务提供者可以在一个系统中发布服务,而服务消费者可以在任何地方调用这些服务。Hessian是Apache的一个子项目,它是一种...
Hessian 协议是一种轻量级的 remoting on http 工具,使用简单的方法提供了 RMI 的功能。采用的是二进制 RPC 协议,所以它很适合于发送二进制数据。Hessian 主要用作面向对象的消息通信。 Hessian 协议报文格式可以...
在Spring框架中集成Hessian是为了实现远程方法调用(Remote Method Invocation, RMI),这是一种轻量级的序列化协议,可以高效地传输Java对象。Hessian使得服务提供者和服务消费者之间能够通过网络进行快速的数据...
在Spring框架中,Hessian是一种轻量级的远程方法调用(RPC)协议,它能够实现Java对象到字节流的序列化,从而使得服务端的对象可以在客户端直接调用,仿佛对象就在本地一样。Hessian提供了高效的二进制RPC协议,使得...
Spring MVC 是一个基于模型-视图-控制器(MVC)设计模式的轻量级 web 框架,而 Hessian 是一种高效的远程方法调用(RPC)协议,它允许跨网络透明地调用 Java 或 C++ 方法。下面我们将详细探讨这两个技术以及它们的...
而Hessian,作为一种二进制协议,因其轻量级、高效的特点,常被用于Android应用程序与Java服务端之间的通信。本文将深入探讨Hessian在Android通讯中的运用,以及如何通过MyEclipse开发环境进行实践。 Hessian是一种...
Hessian协议基于HTTP,允许服务端和客户端之间进行轻量级的交互。它将Java对象转换为紧凑的二进制格式,以减少网络传输的数据量,提高性能。在源码层面,实现Hessian涉及到以下几个关键点: 1. **序列化和反序列化*...
Hessian是一种轻量级的二进制远程过程调用(RPC)协议,它基于HTTP,并且通常通过Servlet来暴露Web服务。Hessian的设计目标是为了提供高效、简单的远程调用方式,使得客户端能够像调用本地方法一样调用远程服务。 *...
Spring框架为了提供远程服务调用的支持,将其集成到自己的服务中,使得开发者能够利用Hessian的特性来实现高效、轻量级的服务间通信。本文将深入探讨Hessian在Spring中的应用以及相关研究。 一、Hessian简介 ...
Hessian是由Caucho公司开发的一种轻量级的二进制Remoting协议,它提供了高效的序列化和反序列化机制,使得跨平台的远程方法调用(RMI)变得更加便捷。Hessian支持HTTP传输,可以在防火墙开放的HTTP端口上进行通信,...
在实际开发中,这种集成方案常用于分布式系统,特别是在需要快速、轻量级的跨网络服务通信时,Hessian提供的二进制序列化比基于XML的方案如SOAP更为高效。而Spring则负责管理和协调这些服务,提供了一种声明式、面向...
3. Hessian和Burlap:这两种协议是基于HTTP的轻量级二进制协议,它们提供了更高效的序列化机制。Hessian是二进制的,而Burlap是基于XML的,两者都提供了自动类型转换的能力,使得跨语言调用成为可能。 4. JMS(Java...
它提供了轻量级、高效的RPC(Remote Procedure Call)框架,使得Java对象能够跨网络边界透明地调用。Hessian协议的特点在于其二进制格式,相比XML-RPC或SOAP等基于文本的协议,传输的数据更小,速度更快。 其次,...
Spring Hessian是一个基于Java的RPC框架,它允许远程方法...总结来说,Spring Hessian是实现轻量级远程服务调用的一种有效方式。通过理解并掌握其配置、工作原理和优化技巧,开发者可以构建高效、可靠的分布式系统。
标题 "Hessian3.1与Spring2.5的整合" 涉及的是在Java开发环境中,如何将Hessian远程调用服务与Spring框架相结合,以便实现高效、轻量级的分布式服务通信。Hessian是一种二进制协议,它允许远程方法调用(RMI)在HTTP上...
而Hessian是一种轻量级的远程过程调用(RPC)协议,它允许服务端的方法被客户端透明地调用,无需理解底层网络协议。在这个“SpringMVC整合Hessian简单案例”中,我们将探讨如何将这两个技术结合,实现高效且便捷的...
Hessian是一种轻量级的二进制Remoting协议,由Caucho公司开发。它允许开发者通过HTTP协议快速、高效地进行远程方法调用(RPC)。Hessian提供了一种序列化机制,将Java对象转化为二进制流,使得在网络间传输时更节省...