Hessian,輕量級的Java Remoting方案 (轉)
專案裡遇到一個問題:由於性的需要,必須由一個專門負責查詢某一,然後透過安全的通道(例如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.
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 = ""; 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.
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
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.
Object obj = ...; OutputStream os = new FileOutputStream("test."); 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();
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-963012/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Remoting: HessianSpringREM
- iOS 輕量級 HTML 解析方案iOSHTML
- 輕量級日誌收集方案LokiLoki
- 超輕量級反爬蟲方案爬蟲
- JAVA輕量級鎖簡介Java
- Android輕量級事件通訊方案Android事件
- java 中的鎖 -- 偏向鎖、輕量級鎖、自旋鎖、重量級鎖Java
- 輕量級超級 css 工具CSS
- 一個輕量級的iOS皮膚切換方案(內附Demo)iOS
- Golang wails2 輕量級跨端桌面解決方案GolangAI跨端
- TDengine 助力西門子輕量級數字化解決方案
- Preact -- React的輕量解決方案React
- Spring的輕量級實現Spring
- 輕量級UI元件 MagicUI元件
- 輕量級IOC框架:Ninject框架
- 微服務架構下的輕量級定時任務解決方案微服務架構
- JAVA物件分析之偏向鎖、輕量級鎖、重量級鎖升級過程Java物件
- lima 輕量級虛擬機器docker替代方案 (macos平臺)虛擬機DockerMac
- Unix/Linux系統下輕量級Shell工具(轉)Linux
- Oracle釋出開源的輕量級 Java 微服務框架 HelidonOracleJava微服務框架
- 不想寫sql?試試這款輕量級JAVA ORM框架!SQLJavaORM框架
- [轉載]用 Go 寫一個輕量級的 ldap 測試工具GoLDA
- Flutter路由輕量級框架FRouterFlutter路由框架
- 輕量級orm框架——gzero指南ORM框架
- 輕量級Web框架Flask(二)Web框架Flask
- [ElasticSearch ]2輕量級搜尋Elasticsearch
- 輕量級標記語言
- Magic:輕量級JavaScript UI元件JavaScriptUI元件
- 超輕量級PHP框架BroPHPPHP框架
- mysqlrouter 輕量級中介軟體MySql
- 一個更適合Java初學者的輕量級開發工具:BlueJJava
- Javalin2.4.0釋出,Kotlin/Java輕量級Web框架JavaKotlinWeb框架
- Pekwm:一個輕量級的 Linux 桌面Linux
- Soa: 一個輕量級的微服務庫微服務
- iOS 一個輕量級的元件化思路iOS元件化
- 一個輕量級RPC的實現RPC
- 基於ObjC的輕量級Promise庫——RWPromiseKitOBJPromise
- 輕量級API測試工具PandariaAPI