案例學習BlazeDS+Spring之十一:Simple Data Push

來源:互聯網
上載者:User

Simple Data Push

這個簡單的資料推送服務demo示範了如何使用Message Service,將資料從服務端推送到用戶端。在服務端,一個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" />,將Message Service暴露給用戶端。

3、simpleFeedStarter

simplepush項目只是一個接收資料的應用。啟動/停止/發布Feed是通過一JAVA組件來實現的。simpleFeedStarter 是實現此Message Service的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的啟動/停止在自訂的線程中執行。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.