Case study: FIG + spring 12: traderdesktop

Source: Internet
Author: User

Traderdesktop:

This is a more complex data push demo. It demonstrates how to use a topic to subscribe to a specified message. In this case, you can only subscribe to the specified stock update message. On the server side, the Java component sends the simulated market data to the destination of the message.

I. Run the demo:

1. Use http: // localhost: 8400/spring-Flex-testdrive/feedstarter/index.html to start the server feed and send the simulated stock information.

2. Open the client program: http: // localhost: 8400/spring-Flex-testdrive/traderdesktop/index.html

3. Stop the server feed through http: // localhost: 8400/spring-Flex-testdrive/feedstarter/index.html and stop sending the simulated stock information.

Ii. Understand the Code:

1. traderdesktop. mxml:

To implement topic subscription, the multitopicconsumer class is used in this demo to subscribe to the target. The subscription method uses consumer. addsubloud (Symbol); cancels the subscription using consumer. removesubloud (Symbol); and converts received messages using a strongly-typed as object. For example, VAR changedstock: stock = event. Message. Body as stock;

Other code of this program is mainly used to display the dynamic data.

2. flex-servlet.xml

Set allow-subtopics of message service to true, which allows the subscriber to subscribe to a specified message.

<Flex: Message-destination ID = "market-feed" allow-subtopics = "true" subtopic-separator = "."/>

3. Spring Bean used to manage marketfeed.

<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

This class reads the stock information XML in XML, and sends data to the Target message service in the feedthread. Use template to create the asyncmessage sent by the service market-feed target, set the message topic through setheader, and set the stock object in the body of asyncmessager.

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;
}

 

Iii. Summary:

This demo is more advanced than the demo of simple data push. You can use multitopicconsumer to subscribe to a specified topic. In the message service's flex-servlet.xml configuration, set allow-subtopics = "true" for the message service target ". In the bean responsible for sending data, use the messagetemplate send method to send data. Pay attention to the usage of asyncmessagecreator. Specify the topic of the message by setting the message header.

 

Appendix: multitopicconsumer and multitopicproducer

"Similar to consumer, multitopicconsumer subscribes to a target using a single clientid and transmits the message to a single event handler. Unlike consumer, it enables you to register subscriptions for a group of subtopics and Selector expressions in a single message processing program. Consumer has the subtopic and Selector attributes, and this component contains the addsubscription (subtopic, selector) method, which can be used to add a new subscription to an existing subscriptions set. Alternatively, you can use
The subscriptioninfo instance list is filled with the subscriptions attribute, which defines the subscriptions for this target.

Similar to conventional consumer, when multitopicconsumer sends messages that generate messageackevent or messagefaultevent, the generated content depends on whether the operation is successful. After the subscription, multitopicconsumer will dispatch messageevent for each message it receives.

"

"Multitopicproducer sends messages to targets with zero or multiple subtopics. It is similar to conventional producer, but it can direct messages to any user who subscribes to a large number of subtopics. If the user is multitopicconsumer and has subscribed to multiple subtopics in The subtopics list used by the producer, the user will only receive the message once.

Multitopicproducer will assign messageackevent or messagefaultevent to each message it sends, depending on whether the outbound message is sent and processed successfully ."

 

Public Function addsubency (subtopic: String = NULL, selector: String = NULL, maxfrequency: uint = 0): void

This is a simple way to add a new subscription. It only creates a new subscriptioninfo object and adds it to the subscriptions attribute. To call this method, you must provide the subtopic and Selector string for the New subscription. If the subtopic is null, the subscription applies to messages without subtopic set by the producer. If the selector string is null, all messages that match the subtopic string are received by the user.

Parameters

Subtopic: string (default = NULL)-New subtopic.

Selector: string (default = NULL)-Selector of The New subscription.

Maxfrequency: uint (default = 0)-Maximum number of messages subscribed to by consumer per second. Note that this value will overwrite maxfrequency in the consumer range.

 

Public Function removesubscription (subtopic: String = NULL, selector: String = NULL): void
This method deletes the subscription specified by the subtopic and selector.
Subtopic: string (default = NULL)-subtopic subscribed.
Selector: string (default = NULL)-Selector of the subscription.

 

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

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.