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 JDK 1.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是一种轻量级的二进制RPC协议,它可以使得Java和其他语言之间的通信变得更加简单和高效。下面是一个简单的Hessian服务端和客户端的例子: 服务端(Java): 1. 首先,需要在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.caucho</groupId> <artifactId>hessian</artifactId> <version>4.0.38</version> </dependency> ``` 2. 创建一个实现Hessian服务接口的类,例如: ```java public class HelloServiceImpl implements HelloService { public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 3. 在Spring配置文件中添加以下内容: ```xml <bean id="helloService" class="com.example.HelloServiceImpl"/> <bean id="hessianServiceExporter" class="org.springframework.remoting.caucho.HessianServiceExporter"> <property name="service" ref="helloService"/> <property name="serviceInterface" value="com.example.HelloService"/> </bean> ``` 4. 启动应用程序,服务端就可以监听来自客户端的请求了。 客户端(C#): 1. 首先,需要在项目中添加对HessianSharp.dll的引用。 2. 创建一个HessianProxyFactory对象,并使用它来创建一个HelloService接口的代理对象,例如: ```csharp HessianProxyFactory factory = new HessianProxyFactory(); HelloService service = (HelloService)factory.Create(typeof(HelloService), "http://localhost:8080/helloService"); ``` 3. 调用代理对象的方法,例如: ```csharp string result = service.sayHello("world"); Console.WriteLine(result); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值