Hessian,輕量級的Java Remoting方案 (轉)

amyz發表於2007-08-17
Hessian,輕量級的Java Remoting方案 (轉)[@more@]

專案裡遇到一個問題:由於性的需要,必須由一個專門負責查詢某一,然後透過安全的通道(例如HTTP)將查詢結果傳到安全區域之外的application。為了解決這個小小的remoting問題,我們差點要動用。不過幸虧朋友推薦,找到了Hessian這個輕量級的remoting on http工具。

Hessian其實很像 service,只不過它的不是P,而是它自己規定的binary協議。Hessian的server端提供一個基類,client端獲得一個service介面(也就是stub)之後上面的方法,stub將方法呼叫marshal之後透過HTTP傳到server,server藉助reflection呼叫service方法。

簡單、輕量。對付普通的remoting問題,Hessian足夠了。我們又可以暫時忘掉EJB和了。

——————————

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, devices like cell-phones can use it to connect to Resin servers. Because it's powerful, it can be used for EJB services.

The contains the latest information about Hessian including the .

Using a Hessian service from a client is like calling a method. The HessianFactory creates proxies which act like normal Java s, with possibility that the method might throw a protocol exception if the remote connection fails. Using HessianProxyFactory requires  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.

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 = ""; 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 = ""; HttpConnection c = (HttpConnection) Connector.open(url); c.setRequestMethod(HttpConnection.POST); OutputStream = 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."); 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();



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-963012/,如需轉載,請註明出處,否則將追究法律責任。

相關文章