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">
        <constructor-arg ref="defaultMessageTemplate" />
        <constructor-arg value="stocklist.xml" />
        <flex:remoting-destination />
</bean>

4、MarketFeed.java

该类中XML中读取股票信息xml,通过在FeedThread线程中向消息服务的目标发送数据。使用template来创建服务market-feed目标发送的AsyncMessage,通过setHeader来设置消息的主题,stock实体设置在AsyncMessager的Body中。

template.send(new AsyncMessageCreator() {

public AsyncMessage createMessage() {
                    AsyncMessage msg = template.createMessageForDestination("market-feed");
                    msg.setHeader("DSSubtopic", stock.getSymbol());
                    System.out.println(stock.getSymbol() + " " + stock.getLast());
                    msg.setBody(stock);
                    return msg;
                }

三、小结:

本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

来自:http://blog.sina.com.cn/s/blog_67532f7c0100oza6.html

案例学习BlazeDS+Spring之十二:Traderdesktop相关推荐

  1. 案例学习BlazeDS+Spring之十:Chat(

    Chat: 该DEMO展示的是BlazeDS的消息服务,是一个使用发布/订阅者模式的简单聊天室. 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex- ...

  2. 案例学习BlazeDS+Spring之二Spring BlazeDS Integration 101

    Spring BlazeDS Integration 101 这展示BlazeDS+Spring最基本的DEMO. 一.运行DEMO: 1.运行应用程序,URL:http://localhost:84 ...

  3. 案例学习BlazeDS+Spring之一(

    BlazeDS4的一个亮点就是与Spring的集成,这大大简化了与FLASH与JEE的集成开发.通过理解BlazeDS4附带的那些精湛的小DEMO,可以很快速的掌握这门技术.虽然案例学习这种方式不太利 ...

  4. 案例学习BlazeDS+Spring之十一:Simple Data Push

    Simple Data Push 这个简单的数据推送服务demo演示了如何使用消息服务,将数据从服务端推送到客户端.在服务端,一个JAVA组件发布一个模拟真实的值给订阅了此消息目标的FLEX客户端.这 ...

  5. 案例学习BlazeDS+Spring之三InSync01查找联系人

    InSync01:查找联系人 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex-testdrive/insync01/index.html: 2.单 ...

  6. 案例学习BlazeDS+Spring之五InSync03强类型

    InSync03:强类型 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex-testdrive/insync03/index.html: 2.单击S ...

  7. 案例学习BlazeDS+Spring之九Company Manager

    Company Manager 该DEMO与InSync是类似的,提供公司信息的CRUD操作.但CompanyManager使用注释来进行定义.也展示了对象关联(CompanyDAO类与Industr ...

  8. 案例学习BlazeDS+Spring之四InSync02使用RemoteObject事件

    InSync02:使用RemoteObject事件 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex-testdrive/insync02/inde ...

  9. 案例学习BlazeDS+Spring之六InSync04打开多个联系人

    InSync04:打开多个联系人 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex-testdrive/insync04/index.html: 2 ...

最新文章

  1. slackware安装mysql_slackware系统安装二
  2. 信息安全系统设计基础第一周学习总结
  3. 跟小段一起学Solaris(20)---ipFilter防火墙
  4. .Net Framework SDK下的命令汇总
  5. Unity3d读取.csv文件
  6. Centos 6.9 编译安装gcc 4.8.5
  7. 俄亥俄州立大学计算机科学转学成功,俄亥俄州立大学转学要求
  8. java无法启动安装程序,Windows Fix中无法启动Java更新安装程序错误
  9. MySQL的ALTER命令
  10. 中原地区第一款开源产品SmartAdmin和《Smart前后台代码规范》正式发布
  11. 故障排除经验——笔记本电脑重启后键盘失灵的解决办法(亲测可用)
  12. android 清除通知栏,android startForeground去除通知栏
  13. Mybatis Plus 实现批量插入
  14. Gartner 魔力象限:安全信息和事件管理 2020
  15. 学校学生工科学生接私活_理工科学生在美国以外的最佳城市
  16. (转)申请企业级IDP、真机调试、游戏接入GameCenter 指南、游戏接入OpenFeint指南;...
  17. 三星sec.android.soagent,【图片】(原贴删)原不用ROOT禁用系统软件的方法,加详细教程。【note4吧】_百度贴吧...
  18. 正交矩阵的列向量组是标准正交向量组
  19. 《WDF USB驱动开发指南》-- USB 设备硬件结构
  20. 福州区网吧管理计费解决方案(转)

热门文章

  1. azure云数据库_Azure SQL数据库地理复制
  2. 查询sql执行计划_使用SQL执行计划进行查询性能调整
  3. 利用T-SQL处理SQL Server数据库表中的重复行
  4. 【java】简介(一)
  5. 强烈建议大家把 CBCentralManager.h 里面的代理方法全部看一遍(就是翻译一遍,别偷懒)。@file CBCentralManager.h...
  6. 数据库系统中事务的ACID原则
  7. 如何把一个数据库的数据copy到另外一个数据库
  8. C#2.0 泛型初探 (特性一览)
  9. JAXB注解 java 关于xml的注解,自动生成xml文件 - @XML***
  10. day055056Django之多表操作,多表查询