Producer Consumer实例
Producer Consumer实例
在上一篇文章(blazeds学习笔记四 Producer Consumer讲解)我们粗略了解一下在Flex里Message通讯中Producer组件和Consumer组 件的简单理论知识,这篇文章中让我们一起来看一个简单的实例,看看Producer组件和Consumer组件的具体使用!
上代码,大部分都在注释中解释的很清楚了!!!(如果哪里讲的不清楚或者有错误,欢迎与我们沟通交流!)
ChatRoom.mxml
<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” creationComplete=”initChatRoom()” layout=”absolute”>
<mx:Script>
<![CDATA[
//导入相应的类
import mx.controls.Alert;
import mx.messaging.messages.AsyncMessage;
import mx.messaging.events.MessageEvent;
import mx.messaging.events.MessageAckEvent;
import mx.messaging.events.MessageFaultEvent;
import mx.rpc.Fault;
import mx.messaging.messages.AcknowledgeMessage;
import mx.messaging.Consumer;
import mx.messaging.Producer;
//声明Producer和Consumer
private var producor:Producer;
private var consumer:Consumer;
//程序在creationComplete后自动调用
private function initChatRoom():void
{
initProducer();
initConsumer();
//为“发送”按钮的单击事件注册处理函数
sendBtn.addEventListener(MouseEvent.CLICK,sendBtnHandler);
}
//初始化一个Producer用来发送消息
private function initProducer():void
{
//实例化一个Producer用于发送消息
producor = new Producer();
//设置消息服务的目标,应与messaging-config.xml文件中的destination条目id相匹配
producor.destination = "chatRoom";
//为acknowledge事件注册监听函数,当收到已发送消息的确认结果时触发该事件
producor.addEventListener(MessageAckEvent.ACKNOWLEDGE,ackMessageHandler);
//为fault事件注册监听函数,当发生消息错误时调度fault事件
producor.addEventListener(MessageFaultEvent.FAULT,faultMessageHandler);
//使用的远程目标的子主题
producor.subtopic = "topic";
}
//初始化一个Consumer用来订阅、接受消息
private function initConsumer():void
{
//实例化一个Consumer用于订阅和接收消息
consumer = new Consumer();
//设置消息服务的目标,该值应与messaging-config.xml文件中的destination条目id相匹配。
consumer.destination = "chatRoom";
//为message事件注册监听函数,当Consumer接收到消息时调度
consumer.addEventListener(MessageEvent.MESSAGE,messageHandler);
//使用的远程目标的子主题
consumer.subtopic = "topic";
//订阅远程目标
consumer.subscribe();
}
//acknowledge事件处理方法,消息发送成功调用该方法
private function ackMessageHandler(event:MessageAckEvent):void
{
trace("消息发送成功...");
}
//fault事件处理方法,消息发送失败将调用该方法
private function faultMessageHandler(event:MessageFaultEvent):void
{
Alert.show("信息发送失败","提示");
}
//message事件处理方法
private function messageHandler(event:MessageEvent):void
{
output.text += event.message.headers.nickName + " 说: " + event.message.body.content + "\n";
}
//点击“发送”按钮执行该方法
private function sendBtnHandler(event:MouseEvent = null):void
{
//实例化一个AsyncMessage用于接收creatMessage()方法返回的AsyncMessage实例
var asyncMessage:AsyncMessage = creatMessage();
//用producor的send方法发送一条消息
producor.send(asyncMessage);
//将输入框清空
input.text = "";
//调用setChatRoomFocus()方法设置程序的焦点
setChatRoomFocus();
}
//创建一条消息并返回该消息
private function creatMessage():AsyncMessage
{
//声明并实例化一个AsyncMessage实例asyncMessage
var asyncMessage:AsyncMessage = new AsyncMessage();
//在asyncMessage的headers中添加信息
asyncMessage.headers.nickName = "nirvana";
//在asyncMessage的body中添加信息
asyncMessage.body.content = input.text;
//返回AsyncMessage实例asyncMessage
return asyncMessage;
}
//设置焦点
private function setChatRoomFocus():void
{
//设置application的焦点
application.focusManager.setFocus(input);
}
]]>
</mx:Script>
<mx:TextArea id=”output” x=”0″ y=”0″ width=”462″ height=”300″ editable=”false”/>
<mx:TextArea id=”input” x=”0″ y=”308″ width=”406″ height=”48″/>
<mx:Button id=”sendBtn” label=”发送” width=”48″ x=”414″ y=”308″ height=”48″/>
</mx:Application>
messaging-config.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<service id=”message-service” >
<adapters>
<adapter-definition id=”actionscript” default=”true” />
<!– <adapter-definition id=”jms”/> –>
</adapters>
<default-channels>
<channel ref=”my-polling-amf”/>
</default-channels>
<destination id=”chatRoom”>
<properties>
<network>
<subscription-timeout-minutes>0</subscription-timeout-minutes>
</network>
<server>
<message-time-to-live>0</message-time-to-live>
<allow-subtopics>true</allow-subtopics>
<subtopic-separator>.</subtopic-separator>
</server>
</properties>
<channels>
<channel ref=”my-amf”/>
</channels>
</destination>
</service>