Traderdesktop:
这是一个更为复杂的数据推送demo,它演示了如何使用主题来有选择的订阅指定消息。在这种情况下,用户可以仅订阅指定的股票更新消息。在服务端,Java组件发送模拟的市场数据到消息的destination。
一、运行DEMO:
1、通过http://localhost:8400/spring-flex-testdrive/feedstarter/index.html来启动服务端的Feed,发送模拟的股票信息。
2、打开客户端程序:http://localhost:8400/spring-flex-testdrive/traderdesktop/index.html
3、通过http://localhost:8400/spring-flex-testdrive/feedstarter/index.html来停止服务端的Feed,停止发送模拟的股票信息。
二、理解代码:
1、traderdesktop.mxml:
为了实现主题订阅,在此DEMO中使用MultiTopicConsumer类来订阅目标。订阅方法使用consumer.addSubscription(symbol);取消订阅使用consumer.removeSubscription(symbol);对于接受到的消息,使用强类型AS对象转换。如:var changedStock:Stock = event.message.body as Stock;
该程序其它代码主要用于显示这些动态数据。
2、flex-servlet.xml
设置消息服务的allow-subtopics为true,可以允许订阅者订阅指定消息。
<flex:message-destination id="market-feed" allow-subtopics="true" subtopic-separator="." />
3、用于管理MarketFeed的Spring bean。
<bean id="marketFeedStarter" class="org.springframework.flex.samples.marketfeed.MarketFeed">
</bean>
4、MarketFeed.java
该类中XML中读取股票信息xml,通过在FeedThread线程中向消息服务的目标发送数据。使用template来创建服务market-feed目标发送的AsyncMessage,通过setHeader来设置消息的主题,stock实体设置在AsyncMessager的Body中。
template.send(new AsyncMessageCreator() {
三、小结:
本demo较simple data push的demo 更进一步。通过MultiTopicConsumer 来订阅指定主题。在消息服务的flex-servlet.xml配置中,设置消息服务目标的allow-subtopics="true"。在负责发送数据的bean中,使用MessageTemplate的send方法来发送数据。在此注意AsyncMessageCreator的用法,通过设置消息的头部,指定消息所属的主题。
附:MultiTopicConsumer 与MultiTopicProducer
“与 Consumer 类似,MultiTopicConsumer 使用单个 clientId 订阅目标,并将消息传递到单个事件处理函数。与 Consumer 不同,它使您可以在单个消息处理程序中同时注册一组 subtopics 和选择器表达式的 subscriptions。Consumer 具有 subtopic 和 selector 属性,而此组件则包含 addSubscription(subtopic, selector) 方法,可用于将新订阅添加到现有 subscriptions 集中。或者,您可以使用 SubscriptionInfo 实例列表填充 subscriptions 属性,这些实例定义对此目标的 subscriptions。
与常规 Consumer 类似,MultiTopicConsumer 发送生成 MessageAckEvent 或 MessageFaultEvent 的订阅和取消订阅消息,生成内容取决于操作是否成功。订阅后,MultiTopicConsumer 便会为它接收到的每条消息分派 MessageEvent。
”
“MultiTopicProducer 将消息发送到具有零个或多个 subtopics 的目标。它与常规 Producer 类似,但它可以将消息定向到任何订阅大量 subtopics 之一的使用者。如果使用者是 MultiTopicConsumer,并且该使用者已订阅了生产者使用的 subtopics 列表中的多个 subtopic,则使用者将仅能收到消息一次。
MultiTopicProducer 将为其发送的每条消息分派 MessageAckEvent 或 MessageFaultEvent,具体取决于出站消息的发送和处理是否成功。”
public function addSubscription(subtopic:String = null, selector:String = null, maxFrequency:uint = 0):void
这是添加新订阅的简便方法。它只创建新 SubscriptionInfo 对象并将其添加到 subscriptions 属性。要调用此方法,您需要为新订阅提供 subtopic 以及选择器字符串。如果 subtopic 为 null,则订阅将应用于制造者中未设置 subtopic 的消息。如果选择器字符串为 null,则已发送的与 subtopic 字符串匹配的所有消息都将由此使用者接收。
参数
subtopic:String (default = null) — 新订阅的 subtopic。
selector:String (default = null) — 新订阅的选择器。
maxFrequency:uint (default = 0) — Consumer 希望每秒钟收到订阅消息的最大数量。请注意,此值将覆盖 Consumer 范围的 maxFrequency。
public function removeSubscription(subtopic:String = null, selector:String = null):void
此方法可删除 subtopic 和选择器指定的订阅。
subtopic:String (default = null) — 订阅的 subtopic。
selector:String (default = null) — 订阅的选择器。
org.springframework.flex.messaging
Interface AsyncMessageCreator
public interface AsyncMessageCreator
Factory interface for the creation of AMF messages to be sent to a MessageDestination
flex.messaging.messages.AsyncMessage createMessage()
Create a new AsyncMessage for sending to a MessageDestination
Returns:
the created message