亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統(tǒng) 1809 0

項(xiàng)目里遇到一個(gè)問(wèn)題:由于安全性的需要,必須由一個(gè)程序?qū)iT(mén)負(fù)責(zé)查詢(xún)某一數(shù)據(jù)庫(kù),然后通過(guò)安全的信道(例如HTTP)將查詢(xún)結(jié)果傳到安全區(qū)域之外的application。為了解決這個(gè)小小的remoting問(wèn)題,我們差點(diǎn)要?jiǎng)佑肊JB。不過(guò)幸虧朋友推薦,找到了Hessian這個(gè)輕量級(jí)的remoting on http工具。

Hessian其實(shí)很像web service,只不過(guò)它的協(xié)議不是SOAP,而是它自己規(guī)定的binary協(xié)議。Hessian的server端提供一個(gè)servlet基類(lèi),client端獲得一個(gè)service接口(也就是stub)之后調(diào)用上面的方法,stub將方法調(diào)用marshal之后通過(guò)HTTP傳到server,server借助reflection調(diào)用service方法。

簡(jiǎn)單、輕量。對(duì)付普通的remoting問(wèn)題,Hessian足夠了。我們又可以暫時(shí)忘掉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();
            



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2436


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


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国产在线观看一区二区三区 | 玖玖中文 | 成人久久视频 | 国产成人毛片亚洲精品不卡 | 五月婷花| 大学生一级黄色片 | 国产精品久久免费 | 亚洲精品成人网久久久久久 | 美女在线视频观看影院免费天天看 | 亚洲日本欧美在线 | 日韩精品亚洲人成在线观看 | 日本特级黄毛片毛片视频 | 亚洲欧洲中文字幕 | 天天拍天天操 | 国产精品社区在线观看 | 97视频在线播放 | 毛片免费看看 | 激情九九 | 思99热精品久久只有精品 | 99爱视频在线观看 | 久久99精品久久久久久臀蜜桃 | 99国产精品久久久久久久成人热 | 亚洲国产高清在线精品一区 | 天天躁日日躁成人字幕aⅴ 天天躁日日躁狠狠躁黑人躁 | 99久久中文字幕伊人情人 | 国产伦精品一区二区三区网站 | 亚洲欧洲精品成人久久曰 | 日韩欧美一区二区久久黑人 | 精品日韩在线视频 | 97久久伊人精品影院 | 日韩欧美久久一区二区 | 能免费看黄的网站 | 伊人久久精品亚洲午夜 | 好吊妞免费视频 | 国产真实伦视频在线观看 | 色天天综合久久久久综合片 | 久久久夜间小视频 | 99爱精品视频 | 天天干夜夜爱 | 天天操操操操操操 | 国产亚洲一区呦系列 |