flex j2EE 例子 和BlazeDs 關於MessageBroker,endpoints 和 channels

注:this scrap is my traslation from http://livedocs.adobe.com/blazeds/1/blazeds_devguide/

      this is my virgin blog,my be you will give me some courage,thanks.

 

 

MessageBroker 

 

        MessageBroker 我們稱他爲信息經紀人,它職責就是把messages 發給到services 就是我們的程序服務,它是BlazeDs在服務器上的核心。endpoint 處理完一個request,也即是把messages 從request抽取出來,然後傳給MessageBroker ,然後MessageBroker 就檢測這個消息的destination ,接着把它傳給它想到的服務目的地。假若我們的服務有安全保護,MessageBroker 要先運行authentication ,authentication 是在信息傳遞前先做check。你要在你BlazeDS  WEB應用WEB-INF/web.xml 文件中配置你的MessageBroker。

 

   Endpoints

 

       BlazeDS 基於servlet的Endpoints(端點)是在J2EE servlet container中的,這就意味着 servlet 爲 endpoints操縱着 the I/O and HTTP sessions ,實際上Endpoints被MessageBrokerServlet控制,除了MessageBrokerServlet,我們的 J2EE server還在web.xml文件中註冊一個 HTTP session 監聽者,以使BlazeDS 有監聽的屬性,和支持。BlazeDS Flex 客戶端用channels (通道)和endpoints通信,在服務器上,channels 和endpoints有一對一的對應關係,也許因爲他們兩個用的數據格式要一樣。比如一個channels 用 AMF消息格式,如AMFChannel格式,必須有一個也用這種格式的endpoints與它配對。同樣一個用了AMFX  格式的channels  也要求有一個同樣格式的endpoint。你要在你BlazeDS  WEB應用WEB-INF/flex directory的 services-config.xml 文件中配置你的endpoints。

 

channels

       channels 是客戶端的對象,它把FLEX組件和 BlazeDS  server端的勾通封裝起來。channels和與之在blaze Server上對應的endpoint通信。你可以在services-config.xml 文件中配置channel的各個屬性和對應的endpoint。

如下channel 定義代碼:

 <channels>

   ...

  <channel-definition id="samples-amf"

type="mx.messaging.channels.AMFChannel">

<endpoint url="http://servername:8400/myapp/messagebroker/amf" type="flex.messaging.endpoints.AMFEndpoint"/>

</channel-definition>

</channels>

 

你就創建了一個AMFChannel與服務器端的AMFEndpoint的通信channel。

其中endpoint URL 是endpoint暴露給外邊可以訪問的地址。channel用它和endpoint交互。這個url必須是唯一的,在所有服務器上暴露的endpoints中,這個url值指向MessageBrokerServlet。

 

怎樣給flex component分配channel

   flex components 用ChannelSet設定channel,它裏面可以包括多個channel,去和server 交互。你可以選擇自動和手動分配channel 給flex 組件。假如你在編譯你的MXML文件時,把你的compiler 配置上 -services 選項讓它指向services-config.xml 文件,那麼你的組件(如RemoteObject, HTTPService等)就自動分配一個配置好的channel實例, 這個channel是根據你在services-config.xml 文件中定義好的包括destination。第二種情況如果你不想在你的compiler 中設置什麼-services

選項,你可以在 MXML or ActionScript中手動創建你的channel,然後把它分配給你的flex components。如果你也不想自己配置channel,Application-level 默認channels這時就會被用到。

  如下代碼自己手動定義channel

  in mxml:

   <RemoteObject id="ro" destination="Dest">

 

<mx:ChannelSet>

<mx:channels>

<mx:AMFChannel id="myAmf"     uri="http://myserver:2000/myapp/messagebroker/amf"/>

</mx:channels>

</mx:ChannelSet>

 

</RemoteObject>

in  ActionScript:

   private function run():void {

   ro = new RemoteObject();

var cs:ChannelSet = new ChannelSet();

  cs.addChannel(new AMFChannel("myAmf", "http://servname:2000  /eqa/messagebroker/amf"));

ro.destination = "Dest";

ro.channelSet = cs;

}

注意當你在客戶端分配設置了channel 並指定了channel type 和 endpoint URL,但並沒有

指定handles 請求的endpoint class。所以這時你還要在services-config.xml中定義你用的這個channel,指明它的url 用到的endpoint class。

 

把channel和endpoint分配給destination

       我們用channel和endpoint目的就是把我們的從client傳過來的東西送到目的地destination。說到給destination分channel我們可以分以下幾中情況。

       1,在所有services中的大多數destinations用同樣的channels。我們可能定義應用級別(application-level)默認channels在services-config.xml文件中。如

<services-config ...>

...

<default-channels>

<channel ref="my-http"/>

<channel ref="my-amf"/>

</default-channels>

...

</services-config >

這樣所有沒定義channel的destinations就會用上面定義的默認channel,當然destinations和services都可以重載默認 channel,通過指明自己的channel。

      2.在一個service中的大多數destinations用同樣的channels。我們可以定義服務級別(service-level )默認channels在services-config.xml文件中。如

  <service ...>

     ...

  <default-channels>

  <channel ref="my-http"/>

  <channel ref="my-amf"/>

 </default-channels>

...

</service>

這樣以來,在一個service中沒有指定channel的destination就會用到本服務內的默認channels.

      3.一個destination 定義自己的channel.代碼如下:

      <destination id="sampleVerbose">

       <channels>

       <channel ref="my-secure-amf"/>

       </channels> ...

      </destination>

 

BlazeDs 基於消息的framework

      blazeds 是利用基於消息的框架完成數據在客戶端和服務器之間來回傳遞的。實際上blazeds用二種最重要的數據交換模式。第一種就是請求響應模式。PRC就是用的這種模式,這個我前幾天自己摸個CRUD例子出來。推銷一下:http://download.csdn.net/source/1527797(這個例子要求你自己要寫個Uint bean,另外要把你的Flex編譯器設置-services 選項並指向services-config.xml文件)。

      第二種模式就是發佈訂閱模式。在這模式下,服務器把published的信息發送給一系列訂閱了這些信息的customer。舉例說Messaging Service就是用這種方式把data發給對data感興趣的client,當然Messaging Service也用 request-response pattern去與client玩。



From:

http://blog.csdn.net/wannshan/article/details/4393494

發佈了37 篇原創文章 · 獲贊 1 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章