Flex通过Blazeds利用Remoteservice与后台java消息推送
准备工作:Myeclipse中先建立一个Web project工程,然后导入Blazeds的文件,再转换为Flex项目类型。
前言:Flex 通过开源的BlazeDS消息服务来支持订阅及发布消息。这个消息服务管理着Flex客户端可以订阅或发布的目标地址。Flex提供了 Producer和Consumer这两个组件,让你用来向目标地址发送或订阅消息。如果要订阅消息,你就使用Consumer类的 subscribe()方法。当有消息发送到你订阅了的目标地址时,Consumer上就会触发message事件。
消息传递的目标地址是在Flex应用根下一个叫messaging-config.xml中配置的。一个目标地址配置的关键元素是在客户端和服务器建立交换数据的通道。使用BlazeDS,消息传递的目标地址通常使用流通道或者轮询通道。
1,使用流通道,服务器响应会一直保持开放状态,直到通道连接关闭,这样可以让服务器持续向客户端发送变化的数据。
2,如果数据没有立刻准备好(长轮询),就可以通过一个简单的时间间隔或者服务器等待时间来配置轮询通道。
修改两个配置文件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所要设置的值,以及发布消息java后台代码AsyncMessage的setDestination()所要传递的参数,必须保证这三个地方名称一致。正是通过这一个目标地址,信息发布者就会将信息发布到该目标地址,然后所有已经设置订阅了该目标地址的flex就会触发MessageEvent.MESSAGE事件,来获取发布的消息。
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
- //利用远程调用来触发开始工作
- 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>
- <!—用来启动消息发布 -->
- <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();
- }
- }
- //停止消息发布
- 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>
本文转自 zhao_xiao_long 51CTO博客,原文链接:http://blog.51cto.com/computerdragon/1143326