Flex消息例子(Producer和Consumer)
????? 不多说,直接上代码,关于Flex消息的信息详见Flex消息服务的文章
?
1.前台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"creationComplete="application1_creationCompleteHandler(event)"><fx:Declarations><mx:RemoteObject id="ro" destination="neAlarmProducer"endpoint="http://ip:8080/appname/messagebroker/amf"showBusyCursor="true"/></fx:Declarations><fx:Script><![CDATA[import mx.controls.Alert;import mx.events.FlexEvent;import mx.messaging.ChannelSet;import mx.messaging.Consumer;import mx.messaging.channels.StreamingAMFChannel;import mx.messaging.events.ChannelEvent;import mx.messaging.events.ChannelFaultEvent;import mx.messaging.events.MessageEvent;import mx.messaging.events.MessageFaultEvent;import mx.rpc.remoting.RemoteObject;private var consumer:Consumer=new Consumer();protected function application1_creationCompleteHandler(event:FlexEvent):void{var myStreamingAMF:StreamingAMFChannel=new StreamingAMFChannel();myStreamingAMF.uri="http://ip:8080/appname/messagebroker/streamingamf";var channelSet:ChannelSet=new ChannelSet();channelSet.addChannel(myStreamingAMF);consumer.destination="testDataPush";consumer.subtopic="3d";consumer.channelSet=channelSet;consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);consumer.addEventListener(ChannelFaultEvent.FAULT, fault);consumer.addEventListener(MessageFaultEvent.FAULT, fault2);consumer.subscribe();//订阅}private function fault(e:ChannelFaultEvent):void{trace(e.faultDetail, e.faultString);}private function fault2(e:MessageFaultEvent):void{trace(e.faultDetail, e.faultString);}private function messageHandler(event:MessageEvent):void{trace("-----messageHandler-");ta.text = ta.text + "获取的告警: " + event.message.body.description + " \n";}private function messageResult(event:MessageEvent):void{trace("-----messageResult-");}private function connectHandler(event:ChannelEvent):void{trace("----connectHandler-");}private function disconnectHandler(event:ChannelEvent):void{trace("----disconnectHandler-");}protected function button1_clickHandler(event:MouseEvent):void{var ro:RemoteObject=new RemoteObject();ro.endpoint="http://ip:8080/appname/messagebroker/streamingamf";ro.destination="testPush";ro.test();}]]></fx:Script><s:Button label="test" click="button1_clickHandler(event)"/><s:TextArea id="ta" x="10" y="42"/></s:Application>
?
?
2.message-config.xml中加入如下代码
<destination id="testDataPush"> <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>
?
3.service-config.xml中如果没有此通道,则加入此通道
<channel-definition id="my-streaming-amf" name="code"><!-- 定义 MessageBrokerFactoryBean 工厂 Bean 用于创建 MessageBroker,并为您的 Service Bean 提供绑定服务 --><bean id="_messageBroker" value="/WEB-INF/flex/services-config.xml" /> </bean><!-- 引入 HandlerMapping 通过 MessageBrokerHandlerAdapter 将所有的 Servlet 请求 URL 映射到由 Spring 容器管理的 MessageBroker --><bean /> <bean id="testPush" ref="defaultMessageTemplate"></property></bean><bean name="code">public class TestDataPush { private MessageTemplate messageTemplate; private AsyncMessage asyncMessage; public TestDataPush() { super(); String clientID = UUIDUtils.createUUID(); asyncMessage = new AsyncMessage(); asyncMessage.setDestination("testDataPush"); asyncMessage.setHeader("DSSubtopic", "3d"); asyncMessage.setClientId(clientID); asyncMessage.setMessageId(UUIDUtils.createUUID()); } public void test() { System.out.println("test-------------------"); asyncMessage.setTimestamp(System.currentTimeMillis()); asyncMessage.setBody("test"); messageTemplate.getMessageBroker().routeMessageToService(asyncMessage, null); } public MessageTemplate getMessageTemplate() { return messageTemplate; } public void setMessageTemplate(MessageTemplate messageTemplate) { this.messageTemplate = messageTemplate; } }
?