Blazeds messaging service提供了2种message filter的方式:
1. add subtopic information。flex "producer" component在call "send" method之前,要设置"producer"的subtopic的值。另一方面,flex "consumer"component需要设置"subtopic" property来定义需要接收属于哪个sub topic的message。该方式最适合分group的message
2. flex "producer" component在call "send" method之前, add information to message headers。另一方面,flex "consumer"component需要设置"selector" property来定义需要接收的message的criteria (例如selector="prop1 > 4" 表示只接收在message header里的'prop1' value大于4的message)。该方法提供更灵活和强大的message filter criteria。
注意:上面2种方式选其中一种来用,不能同时使用2种。
Use subtopic for message filter
flex "Producer" component可以设置要发送的消息的category,"Consumer" component可以设置只接收某种category的message。
注意:若你的blazeds message destintion是JMS,则不能使用subtopic方式,而应该后面介绍的Selector方式。
在Producer component里,设置它的subtopic property来定义该producer发送的消息的category。该值的format是:
mainToken[.secondaryToken][.additionalToken][...]
token之间用"."里间隔。(如果你想使用其他符号来间隔,那么就要修改该destination在"messaging-config.xml" file里<subtopic-separator> property的设置,by default是使用"."来间隔)
例,你可以定义subtopic为"chat" or "chat.fds" or "chat.fds.newton".
在Consumer component里,设置它的subtopic property来定义应该接收哪些category的消息。该值的format和上面producer的subtopic一样,唯一不同的是,comsumer的subtopic可以使用wildcard character "*"。
例1:如果设置comsumer的subtopic="foo.*",那么它可以接收category为"foo.bar" and "foo.baz" and "foo.bar.aaa" and "foo.bar.bbb.ccc"的message。
例2:如果“*”放在中间,subtopic="foo.*.baz",那么它可以接收category为"foo.bar.baz" and "foo.aaa.baz",但不接收 "foo.bar.cookie" message。
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private function useSubtopic():void {
var message:AsyncMessage = new AsyncMessage();
producer.subtopic = "chat.fds.newton";
producer.send(message);
}
private function messageHandler(event:MessageEvent):void {
ta.text += event.message.body + "\n";
}
private function logon():void {
consumer.subtopic = "chat.fds.*";
consumer.subscribe();
}
]]>
</mx:Script>
<mx:Producer id="producer" destination="chat"/>
<mx:Consumer id="consumer" destination="chat" message="messageHandler(event);"/>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>
还有一步要做:在WEB-INF/flex/messaging-config.xml里设置你的message destination support subtopic。
<destination id="ChatTopic">
<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-streaming-amf"/>
</channels>
</destination>
Use selector for message filter
基本原理是:
一方面,flex "producer" component在call "send" method之前, 使用add extra information to message headers。
另一方面,flex "consumer"component需要设置"selector" property来定义需要接收的message的criteria。
例子:发送附加信息"prop1"的值为5的message,接收附加信息"prop1“的值大于4的message。
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">>
<mx:Script>
<![CDATA[
import mx.messaging.*;
import mx.messaging.messages.*;
import mx.messaging.events.*;
private function init():void {
consumer.subscribe();
}
private function sendMessage():void {
var message:AsyncMessage = new AsyncMessage();
message.headers = new Array();
//Use the AsyncMessage.headers property of the message to specify the message headers.
//message header是一个array,你可以添加多个key-value pair
//Note: header里的element key不能以"JMS" or "DS"作为前缀!!
//注意:headers里的element的值只能是number or String!!!
//在该例里,不能设置其值为string,如 message.headers["prop1"] = "5";
//这是因为在后面consumer component里设置selector="prop1 > 4",使用的是number,而不是string
//若要selector使用string,应该为selector="prop1 = '5'" (没试过,我猜的)
message.headers["prop1"] = 5;
message.body = input.text;
producer.send(message);
}
private function messageHandler(event:MessageEvent):void {
ta.text += event.message.body + "\n";
}
]]>
</mx:Script>
<mx:Producer id="producer" destination="chat"/>
<!-- selector属性值是一个SQL conditional expression-->
<mx:Consumer id="consumer" destination="chat" selector="prop1 > 4" message="messageHandler(event);"/>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>