日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Flex通过Blazeds利用Remoteservice与后台java消息推送

發(fā)布時間:2023/12/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Flex通过Blazeds利用Remoteservice与后台java消息推送 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Flex通過Blazeds利用Remoteservice與后臺java消息推送

準備工作:Myeclipse中先建立一個Web project工程,然后導入Blazeds的文件,再轉換為Flex項目類型。 前言:Flex 通過開源的BlazeDS消息服務來支持訂閱及發(fā)布消息。這個消息服務管理著Flex客戶端可以訂閱或發(fā)布的目標地址。Flex提供了 Producer和Consumer這兩個組件,讓你用來向目標地址發(fā)送或訂閱消息。如果要訂閱消息,你就使用Consumer類的 subscribe()方法。當有消息發(fā)送到你訂閱了的目標地址時,Consumer上就會觸發(fā)message事件。 消息傳遞的目標地址是在Flex應用根下一個叫messaging-config.xml中配置的。一個目標地址配置的關鍵元素是在客戶端和服務器建立交換數(shù)據(jù)的通道。使用BlazeDS,消息傳遞的目標地址通常使用流通道或者輪詢通道。 1,使用流通道,服務器響應會一直保持開放狀態(tài),直到通道連接關閉,這樣可以讓服務器持續(xù)向客戶端發(fā)送變化的數(shù)據(jù)。 2,如果數(shù)據(jù)沒有立刻準備好(長輪詢),就可以通過一個簡單的時間間隔或者服務器等待時間來配置輪詢通道。 修改兩個配置文件services-config.xml,messaging-config.xml services-config.xml加入以下代碼:
  • <channel-definition?id="my-streaming-amf"?class="mx.messaging.channels.StreamingAMFChannel">? ?
  • <endpoint?url="http://{server.name}:{server.port}/{context.root}/messagebroker/streamingamf"?class="flex.messaging.endpoints.StreamingAMFEndpoint"/>? ?
  • ????<properties>?
  • ??????????<idle-timeout-minutes>0</idle-timeout-minutes>?
  • ??????????<max-streaming-clients>10</max-streaming-clients>?
  • <server-to-client-heartbeat-millis>5000 ?
  • </server-to-client-heartbeat-millis>?
  • ??????<user-agent-settings>?
  • ???????<user-agent?match-on="MSIE"?kickstart-bytes="2048"?max-streaming-connections-per-session="1"/>?
  • ???????<user-agent?match-on="Firefox"?kickstart-bytes="2048"?max-streaming-connections-per-session="1"/>?
  • ???????</user-agent-settings>?
  • ?????</properties>?
  • </channel-definition>???
  • messaging-config.xml加入以下代碼
  • <destination?id="message-data-feed">?
  • ???????<properties>?
  • ????????<server>?
  • ???????????<allow-subtopics>true</allow-subtopics>?
  • ???????????<subtopic-separator>.</subtopic-separator>?
  • ????????</server>?
  • ??????</properties>?
  • ??????<channels>?
  • ???????????<channel?ref="my-polling-amf"?/>?
  • ???????????<channel?ref="my-streaming-amf"?/>?
  • ??????</channels>?
  • </destination>?
  • 注:這里的id就是目標地址,也就是在flex代碼中需要訂閱消息的Consumer的屬性destination所要設置的值,以及發(fā)布消息java后臺代碼AsyncMessage的setDestination()所要傳遞的參數(shù),必須保證這三個地方名稱一致。正是通過這一個目標地址,信息發(fā)布者就會將信息發(fā)布到該目標地址,然后所有已經(jīng)設置訂閱了該目標地址的flex就會觸發(fā)MessageEvent.MESSAGE事件,來獲取發(fā)布的消息。 Flex前端代碼:
  • <?xml?version="1.0"?encoding="utf-8"?>?
  • <s:Application?xmlns:fx="http://ns.adobe.com/mxml/2009"? ?
  • ??????????????xmlns:s="library://ns.adobe.com/flex/spark"? ?
  • ??????????????xmlns:mx="library://ns.adobe.com/flex/mx"? ?
  • minWidth="955"?minHeight="600">?
  • ????<fx:Script>?
  • ???????<![CDATA[ ?
  • ???????????import?mx.messaging.Channel; ?
  • ???????????import?mx.messaging.ChannelSet; ?
  • ???????????import?mx.messaging.Consumer; ?
  • ???????????import?mx.messaging.events.MessageEvent; ?
  • ???????????import?mx.rpc.events.ResultEvent;? ?
  • ???????????private?var?myConsumer:Consumer?=?new?Consumer();? ?
  • ??????? ?
  • ???????????//直接利用Remote來進行遠程的消息調用 ?
  • ???????????//開始訂閱消息 ?
  • ???????????protected?function?rbt_clickHandler(event:MouseEvent):void ?
  • ???????????{ ?
  • ??????????????//?TODO?Auto-generated?method?stub ?
  • ??????????????//利用遠程調用來觸發(fā)開始工作 ?
  • ??????????????subMessage.startSendMessage("start"); ?
  • ??????????????//準備開始訂閱消息 ?
  • ??????? myConsumer.destination?=?"?message?-data-feed";? ?
  • //這里也要與后臺的主題名稱必須相同 ?
  • ?????? ?myConsumer.subtopic?=?"tick";? ?
  • ?????? ?myConsumer.channelSet?=?new?ChannelSet(["my-streaming-amf"]);? ?
  • ?????? ?myConsumer.addEventListener(MessageEvent.MESSAGE,?remote_messageHandler);? ?
  • ?????? ?myConsumer.subscribe();? ?
  • ?????? ?} ?
  • //獲取訂閱的消息,以文本來顯示顯示 ?
  • private?function?remote_messageHandler(event:MessageEvent):void? ?
  • {? ?
  • ??????? var?mess:String?=?event.message.body?as?String;? ?
  • ??????? demot.appendText("\n"+?mess); ?
  • }? ?
  • //退訂該消息 ?
  • protected?function?cbr_clickHandler(event:MouseEvent):void ?
  • { ?
  • ??????? subMessage.stopSendMessage("stop"); ?
  • ??????? myConsumer.unsubscribe(false); ?
  • } ?
  • protected?function?subMessage_resultHandler(event:ResultEvent):void ?
  • {}
  • ]]>?
  • </fx:Script>?
  • <fx:Declarations>?
  • ???? <!—用來啟動消息發(fā)布?-->?
  • <mx:RemoteObject?id="subMessage"?destination="RemoteMessage"? ?
  • ????result="subMessage_resultHandler(event)">?</mx:RemoteObject>?
  • </fx:Declarations>?
  • ????<s:TextArea?x="445"?y="42"?width="257"?id="demot"/>?
  • ????<s:Button?x="445"?y="210"?label="訂閱消息Remote"?id="rbt"?click="rbt_clickHandler(event)"/>?
  • ????<s:Button?x="597"?y="207"?label="退訂消息R"?id="cbr"?click="cbr_clickHandler(event)"/>?
  • </s:Application>?
  • ? Java后臺代碼:
  • package?com.whut.daemon; ?
  • import?flex.messaging.MessageBroker; ?
  • import?flex.messaging.messages.AsyncMessage; ?
  • import?flex.messaging.util.UUIDUtils; ?
  • public?class?DaemonMessage?{ ?
  • ????private?static?FeedThread?thread; ?
  • ????//開始傳遞消息 ?
  • ????public?void?startSendMessage(String?flags) ?
  • ????{ ?
  • ???????if?(thread?==?null)? ?
  • ???????{ ?
  • ???????????thread?=?new?FeedThread(); ?
  • ???????????thread.start(); ?
  • ???????} ?
  • ????} ?
  • ????//停止消息發(fā)布 ?
  • ????public?void?stopSendMessage(String?flags) ?
  • ????{ ?
  • ???????thread.running=false; ?
  • ???????thread=null; ?
  • ????} ?
  • ????public?static?class?FeedThread?extends?Thread? ?
  • ????{ ?
  • ???????public?boolean?running?=?true; ?
  • ???????public?void?run()?{ ?
  • ???????????MessageBroker?msgBroker?=?MessageBroker.getMessageBroker(null); ?
  • ???????????String?clientID?=?UUIDUtils.createUUID(); ?
  • ???????????System.out.println("clientID="+clientID); ?
  • ???????????while?(running)?{ ?
  • ??????????????//異步消息 ?
  • ??????????????AsyncMessage?msg?=?new?AsyncMessage(); ?
  • ??????????????msg.setDestination("tick-data-feed111"); ?
  • ??????????????msg.setHeader("DSSubtopic",?"tick"); ?
  • ??????????????msg.setClientId(clientID); ?
  • ??????????????msg.setMessageId(UUIDUtils.createUUID()); ?
  • ??????????????msg.setTimestamp(System.currentTimeMillis()); ?
  • ??????????????msg.setBody("hello"); ?
  • ??????????????msgBroker.routeMessageToService(msg,?null); ?
  • ??????????????try?{ ?
  • ??????????????????Thread.sleep(500); ?
  • ??????????????}?catch?(InterruptedException?e)?{}}}}} ?
  • remoting-config.xml加入以下代碼:
  • <destination?id="RemoteMessage">?
  • ?<properties>?
  • ??????<source>com.whut.daemon.DaemonMessage</source>?
  • ??</properties>?
  • </destination>?
  • ?

    ?

    轉載于:https://blog.51cto.com/computerdragon/1143326

    總結

    以上是生活随笔為你收集整理的Flex通过Blazeds利用Remoteservice与后台java消息推送的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。