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

46 篇文章 0 订阅
 

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的启动/停止在自定义的线程中执行。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值