Case study: blazeds + spring 11: simple data push

Source: Internet
Author: User

Simple data push

This simple data PUSH Service Demo demonstrates how to use message service to push data from the server to the client. On the server side, a Java component publishes a simulated value to the flex client that subscribes to the Message target. This function is common in stock applications.

I. Run the demo:

1. Run feed starter application to start "simple feed" and the server starts to publish data values.
2. Run the client program http: // localhost: 8400/spring-Flex-testdrive/simplepush/index.html.
3. Click "subscribe" to display the pushed value in the text field. You can click "Unsubscribe" to cancel the subscription to this destnation.

Ii. Understand the Code:

1. simplepush. mxml

This program subscribes to the server through the consumer. After receiving the message, the value is displayed in the text box.

<Mx: Consumer id = "consumer" Destination = "simple-feed" channelset = "{CS }"
Message = "messagehandler (event. Message)"/>
Subscribe () and unsubscribe () of the consumer to subscribe to messages and cancel the subscribe messages.

Private function messagehandler (message: iMessage): void
{
Pushedvalue. Text = "" + message. Body;
}

2. flex-servlet.xml

Use this configuration <flex: Message-destination ID = "simple-feed"/> to expose the message service to the client.

3. simplefeedstarter

The simplepush project is only an application that receives data. Starting/stopping/publishing a feed is implemented through a Java component. Simplefeedstarter is the bean that implements this message service. Spring bean can be defined directly in the flex-servlet.xml and the <flex: remoting-Destination/> label needs to be used in the <bean/> label.

<Bean id = "simplefeedstarter" class = "org. springframework. Flex. samples. simplefeed. simplefeed">
<Constructor-Arg ref = "defaultmessagetemplate"/>
<Flex: remoting-Destination/>
</Bean>

Simplefeedstarter bean constructor injects the default message template:

<Bean id = "defaultmessagetemplate" class = "org. springframework. Flex. messaging. messagetemplate"/>

4. simplefeed. Java

The simplefeed class uses the messagetemplate class to publish messages to subscribers. The member variable template saves references to the messagetemplate class injected from the constructor.

The simplefeed class runs a thread to generate data. The simplefeed class has an internal class feedthread, which is a subclass of 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 = maid * 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 ){
}

}
}
}

You can start and stop the feedthread in simplefeed by manipulating the feedthread thread.

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 is responsible for starting and stopping simplefeedstarter. Feedstarter calls a remote object on the server through remoteobject.

Iii. Summary:

The simplepush program only subscribes to and unsubscribes to messages through the subscribe () and unsubscribe () of the consumer, and processes received messages when the server pushes data. The important thing is the simplefeed class. simplefeedstarter bean uses the messagetemplate object injected by spring to send messages to the target simple-feed of blazeds, in this case, all the consumers subscribed to the simple-feed target will receive this message.

Feedstarter start-> simplefeedstarter bean-> simplefeedstarter. Start-> feedthread. Start-> feedthread. Run-> messagetemplate send-> simple-feed message-destination-> simplefeedstarter messagehandler.

Start/stop simplefeed execution in a custom thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.