Simple Data Push

这个简单的数据推送服务demo演示了如何使用消息服务,将数据从服务端推送到客户端。在服务端,一个JAVA组件发布一个模拟真实的值给订阅了此消息目标的FLEX客户端。这种功能常见到股票应用中。

一、运行DEMO:

1、运行Feed Starter application启动“Simple Feed”,服务端开始发布数据值。
2、运行客户端程序:http://localhost:8400/spring-flex-testdrive/simplepush/index.html。
3、单击“Subscribe”按钮,被推送的值显示在文本字段中。你可以单击“unsubscribe”按钮取消对这个destnation的订阅。

二、理解代码:

1、simplepush.mxml

该程序通过Consumer向服务端订阅消息,在接收到消息后,将值显示在文本框中。

<mx:Consumer id="consumer" destination="simple-feed" channelSet="{cs}"
                     message="messageHandler(event.message)"/>
通过consumer的subscribe()和unsubscribe()来订阅消息和取消订阅消息。

private function messageHandler(message:IMessage):void
{
      pushedValue.text = ""+ message.body;    
}

2、flex-servlet.xml

通过此配置<flex:message-destination id="simple-feed" />,将消息服务暴露给客户端。

3、simpleFeedStarter

simplepush项目只是一个接收数据的应用。启动/停止/发布Feed是通过一JAVA组件来实现的。simpleFeedStarter 是实现此消息服务的bean。可以在flex-servlet.xml直接定义Spring bean,需要在<bean/>标签里使用<flex:remoting-destination />标签。

<bean id="simpleFeedStarter" class="org.springframework.flex.samples.simplefeed.SimpleFeed">
        <constructor-arg ref="defaultMessageTemplate" />
        <flex:remoting-destination />
</bean>

simpleFeedStarter bean的构造器注入了默认的消息模版:

<bean id="defaultMessageTemplate" class="org.springframework.flex.messaging.MessageTemplate" />

4、SimpleFeed.java

在SimpleFeed类通过MessageTemplate类来向订阅者发布消息,成员变量template保存了从构造器中注入的MessageTemplate类引用。

SimpleFeed类运行一线程来生成数据,SimpleFeed类有一个内部类FeedThread,是Thread的子类。

public static class FeedThread extends Thread {

public boolean running = false;

private final MessageTemplate template;

public FeedThread(MessageTemplate template) {
            this.template = template;
        }

@Override
        public void run() {
            this.running = true;
            Random random = new Random();
            double initialValue = 35;
            double currentValue = 35;
            double maxChange = initialValue * 0.005;

while (this.running) {
                double change = maxChange - random.nextDouble() * maxChange * 2;
                double newValue = currentValue + change;

if (currentValue < initialValue + initialValue * 0.15 && currentValue > initialValue - initialValue * 0.15) {
                    currentValue = newValue;
                } else {
                    currentValue -= change;
                }

this.template.send("simple-feed", new Double(currentValue));

System.out.println("" + currentValue);

try {
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                }

}
        }
    }

SimpleFeed中启动和停止的方法就是操纵FeedThread 线程。

public void start() {
        if (thread == null) {
            thread = new FeedThread(this.template);
            thread.start();
        }
    }

public void stop() {
        thread.running = false;
        thread = null;
    }

5、feedstarter.mxml

feedstarter负责simpleFeedStarter的启动和停止。feedstarter通过RemoteObject调用服务端上的远程对象。

三、小结:

simplepush程序只是通过consumer的subscribe()和unsubscribe()来订阅消息和取消订阅消息,以及在服务器推送数据时,处理接受到的消息。重要的是SimpleFeed类,simpleFeedStarter bean通过使用Spring注入的MessageTemplate 对象,通过该对象向BlazeDS的目标simple-feed发送消息,此时订阅了simple-feed目标的所有consumer都将收到此消息。

feedstarter start –>simpleFeedStarter bean->simpleFeedStarter.start->FeedThread.start –>FeedThread.run->MessageTemplate send->simple-feed  message-destination –>simpleFeedStarter messageHandler。

SimpleFeed的启动/停止在自定义的线程中执行。

案例学习BlazeDS+Spring之十一:Simple Data Push相关推荐

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

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

  2. 案例学习BlazeDS+Spring之十二:Traderdesktop

    Traderdesktop: 这是一个更为复杂的数据推送demo,它演示了如何使用主题来有选择的订阅指定消息.在这种情况下,用户可以仅订阅指定的股票更新消息.在服务端,Java组件发送模拟的市场数据到 ...

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

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

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

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

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

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

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

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

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

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

  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. SpringBoot项目在IntelliJ IDEA中实现热部署 1
  2. JNI中参数的传递与操作
  3. IOS 浏览器端overflow:scroll overflow:auto元素无法滑动bug解决方法整理
  4. hadoop记录topk
  5. 后疫情时代,初创企业生存之策
  6. c语言用指针访问简单变量,关于C语言指针,个人认为最经典、最简单的一个应用...
  7. html mysql查询_mysql查询
  8. rest sso 和_SSO企业单点登录系统——CAS REST认证方式
  9. Sybase的安装、配置及使用(五)
  10. excel文件撤销工作表保护
  11. 中文拼音排序的两种方法
  12. IOS设置时间不兼容解决方案
  13. java实现打字母小游戏
  14. 《嫌疑人X的献身》读书笔记
  15. 通过网络地址下载图片示例
  16. 噢!查重原来是这样实现的啊!
  17. ibm服务器面板报警指示灯含意
  18. KDJ 技术指标实战技巧
  19. Python使用QQ邮箱发送验证码
  20. sqli-labs1-10关闯关心得与思路

热门文章

  1. react sql格式化_为SQL Server数据库损坏做准备; 初步React与分析
  2. tde数据库加密_启用了基于透明数据加密(TDE)的地理复制的Azure SQL数据库
  3. goroutine 修改全局变量无效问题
  4. USACO Training3.3亚瑟王的宫殿【搜索】By cellur925
  5. HDU 2068 Choose the best route
  6. h2database源码浅析:TransactionMap、MVMap、MVStore
  7. 程序中,序列化与反序列化
  8. flashSlider——基于Jquery的图片展示插件
  9. VC/MFC 下 递归遍历目录下的所有子目录及文件
  10. 阿里云服务器对外开放tomcat端口访问