`
ihuashao
  • 浏览: 4552073 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

Hessian,轻量级的Java Remoting方案

阅读更多

项目里遇到一个问题:由于安全性的需要,必须由一个程序专门负责查询某一数据库,然后通过安全的信道(例如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 .

  1. Hessian Client
  2. Hessian Service
  3. Hessian Client for a cell-phone
  4. Hessian Serialization

Hessian Client
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.

Hessian Service
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 Client for a cell-phone
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);

Hessian Serialization
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:轻量级的remoting onhttp工具介绍及基于Spring2的完整实例

    NULL 博文链接:https://merryis.iteye.com/blog/2092349

    hessian轻量级 rpc实现

    hessian轻量级 rpc实现

    轻量级的remoting on http工具(最新):hessian-4.0.60

    Hessian一般是通过Web应用来提供服务,因此非常类似于平时我们用的 WebService。 只是它不使用SOAP协议,但相比webservice而言更简单、快捷。

    hessian.jar 轻量级webservice实现

    hessian.jar 轻量级webservice实现 hessian.jar 轻量级webservice实现 hessian.jar 轻量级webservice实现

    hessian-3.0.20-src.jar

    hessian是一个轻量级的Java Remoting方案

    hessian示例远程轻量级传输

    远程传输的轻量级处理,hessian示例

    轻量级远程服务调用Hessian的入门实例和与Spring整合的实例.zip

    纯Hessian的入门例子教程,还有Hessian和Spring整合的例子教程。 代码上传之前都运行通过的

    hessian php与java通讯demo源码

    hessian php与java通讯demo源码

    hessian小例(java)

    这个是我自己写的一个hessian小例,其中包括两个project,一个是服务端,一个是客户端,代码比较简单,只是为了测试hessian在javaweb项目中的使用

    基于java的Hessian实现

    基于java实现hessian进行服务器之间数据交互demo项目 实现功能: 1.基于spring 2.5.6+hessian3.1.6带有签名安全机制 2.基于servlet代理机制实现HessianServlet,进行简单IP地址校验功能!

    Hessian的使用配置步骤

    远程方法调用的比较,Hessian方法的介绍和相关配置.Hessian是一个轻量级的remoting on http工具,采用的是Binary RPC协议,所以它很适合于发送二进制数据,同时又具有防火墙穿透能力。Hessian一般是通过Web应用来提供...

    WebService, 轻量级WebService

    轻量级WebService,打包了轻量级WebService hessian的一个简单demo,还有Xfire的一个简单demo,还有一些PPT文档,相对来说,对于没有入门的童鞋们来说是个不错的资源,免费共享给大家,互相共勉(我也是菜鸟)。

    WebService另一种轻量级实现—Hessian 学习笔记.rar

    WebService另一种轻量级实现—Hessian 学习笔记.rar

    hessian 源代码与jar包

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能. 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据

    spring 集成 hessian例子

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。参考文档地址...

    hessian3.1

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

    hessian-4.0.37-src.jar

    Hessian是一个轻量级的remoting onhttp工具,使用简单的方法提供了RMI的功能。 相比WebService,Hessian更简单、快捷。采用的是二进制RPC协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。

    hessian使用小例子

    hessian关于java使用的一个小例子

    Hessian remoting onhttp

    采用的是二进制RPC协议,Hessian更加简单、快捷,可以远程访问java类描述信息。

    Java和c#使用hessian通信

    一个简单的例子学习hessian服务:服务端为Java,客户端为C#。  先要准备好C#和Java的第三方类库:http://hessian.caucho.com/  Hssiancharp.dll  hessian-4.0.37.jar  Hessian服务端(java)  打开eclipse...

Global site tag (gtag.js) - Google Analytics