Hessian,轻量级的Java Remoting方案
来源:互联网 发布:淘宝化妆品批准文号 编辑:程序博客网 时间:2024/05/18 02:43
项目里遇到一个问题:由于安全性的需要,必须由一个程序专门负责查询某一数据库,然后通过安全的信道(例如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/index.xtp">Hessian home page contains the latest information about Hessian including the Hessian/Hessian-draft-spec.xtp">Hessian specification .
Hessian-Client>Hessian ClientUsing 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.
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.
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>Hessian ServiceWhile 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.
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 Client for a cell-phoneHessian can be used for even small Java devices. The following classes from com.caucho.Hessian.client can be extracted into a J2ME jar:
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.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>Hessian SerializationThe Hessian classes can be used for serialization and deserialization.
Object obj = ...;OutputStream os = new FileOutputStream("test.xml");HessianOutput out = new HessianOutput(os);out.writeObject(obj);os.close();
InputStream is = new FileInputStream("test.xml");HessianInput in = new HessianInput(is);Object obj = in.readObject(null);is.close();<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
- Hessian,轻量级的Java Remoting方案
- Hessian——轻量级远程调用方案
- Java Remoting选取方案
- SSH2对于Java的轻量级实现方案-ganymed
- 基于 Hessian 轻量级远程调用的原理及示例
- Example of Remoting by Hessian
- 轻量级的代码生成方案选择-Java基础-Java-编程开发
- 轻量级的代码生成方案选择
- 轻量级的代码生成方案选择
- 在Spring中使用Hessian Remoting技术。
- 在Spring中使用Hessian Remoting技术
- 在Spring中使用Hessian Remoting技
- 在Spring中使用Hessian Remoting技术
- 比WebService更简单的远程调用方案框架:Hessian
- java hessian
- Hessian轻量级二进制远程调用框架
- Hessian轻量级二进制远程调用框架
- Android Hessian轻量级二进制远程调用框架
- Sun ONE Application Server 开发者指南(21)
- 在jboss启动时加载class方法(定时操作)
- JavaServer Faces(JSF)全面掌握(之扫盲篇)
- JavaServer Faces中的页面导航
- Database-Level Roles
- Hessian,轻量级的Java Remoting方案
- Swing入门基础
- JDO架构介绍
- IIS6和Tomcat5的整合
- PowerDesigner9 做出的 DbManager.java
- Java用户界面本地化就这么简单之理论
- 调试JSP文件
- OptimalJ开发中使用的主要模型
- jsp操作Execl分析