What is BlazeDS Message Service?
The BlazeDS Message Service enabled with publish/subscribe mechanism and infrastructure to allow Flex application to publish and subscribe messages to a collection of messaging destinations with providing the building of real time data push and applications with collaboration. The following steps creates a simple Cricket score card application that helps in understanding the BlazeDS Message Service
also read:
Step 1: Create messaging destination
A messaging destination depicts a topic that tracks the conversation of producer and consumers.
Define destination for this application:
- In the Blazeds server application project, update the messaging-config.xml placed in the Flex folder with following entry.
configuring messaging-config.xml
<destination id="cricket"> <properties> <server> <message-time-to-live>0</message-time-to-live> <durable>true</durable> </server> </properties> </destination>
And restart Tomcat.
The destination is used to communicate the messaged thru the channel between the client and the server. BlazeDS enables to use either polling channel or streaming for a messaging destination.
What is Polling channel?
A polling channel normally may be configured using a polling interval. As it works in asynchronous mode, it can also wait for data to receive at the server-side. Every poll response concludes the request. Already connected HTTP connections can be used to send sequential poll requests reduce the overhead of polling.
What is streaming channel?
In case of a streaming channel,the server is enabled to send data in incremental basis to the client until the connection is closed. Streaming AMF(Action Message Format) or HTTP channel requires two active browser HTTP connections in order to send the data in both directions.
This articles works with the destination defined by using the default channel defined in the messaging-config.xml file. The client will connect to the message service by using the streaming AMF channel otherwise if there is any issue by using that channel, the client will connect to polling AMF channel.
Step 2: Configure the Flex project
Connect to the BlazeDS server thru the configuration below:
Step 3: Implement the producer for the Cricket Score card update:
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="consumer.subscribe()" width="540" height="356"> <fx:Declarations> <s:Producer id="producer" destination="cricket"/> <s:Consumer id="consumer" destination="cricket" message="receiveMessage(event)"/> </fx:Declarations> <fx:Script> <![CDATA[ import mx.messaging.events.MessageEvent; import mx.messaging.messages.AsyncMessage; import mx.messaging.messages.IMessage; private function submitMessage():void { var message:IMessage = new AsyncMessage(); message.body = new Date()+": "+msg.text; producer.send(message); msg.text = ""; } private function receiveMessage(event:MessageEvent):void { mytext.text += event.message.body.toString()+ "\n"; } ]]> </fx:Script> <s:Panel title="Cricket Score Update" chromeColor="#000000" backgroundColor="#FBF8F8" contentBackgroundColor="#FAFBFB" symbolColor="#FBF7F7" rollOverColor="#EFF3F8" borderColor="#FAF5F5" focusColor="#F7F8F9" top="0" left="0" right="0" horizontalCenter="0" verticalCenter="-71" bottom="142" color="#FCF9F9"> <s:TextArea id="mytext" width="100%" height="100%" contentBackgroundColor="#C0E5EE" color="#090808"/> </s:Panel> <s:Button label="Send" click="submitMessage()" x="0" y="227"/> <s:TextInput id="msg" width="100%" enter="submitMessage()" x="80" y="226"/> </s:Application>
Step 4: Create the Consumer for the Cricket Score Update
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="consumer.subscribe()"> <fx:Declarations> <s:Consumer id="consumer" destination="cricket" message="receiveMessage(event)"/> </fx:Declarations> <fx:Script> <![CDATA[ import mx.messaging.events.MessageEvent; private function receiveMessage(event:MessageEvent):void { mytext.text += event.message.body.toString()+ "\n"; } ]]> </fx:Script> <s:Panel title="Cricket Score Update" width="406" height="188" x="124" y="96" backgroundColor="#FFFFFF" contentBackgroundColor="#991010" chromeColor="#E84242" rollOverColor="#2F69C0"> <s:TextArea editable="false" id="mytext" width="100%" height="100%" contentBackgroundColor="#A1D2D3"/> </s:Panel> </s:Application>
On the Flex client side, the BlazeDS provides two classes Producer and Consumer from its Message Service API that can be used to publish and subscribe to a destination.
Subscribe() method of the Consumer can be used to subscribe to a destination.
The event of message is triggered on the Consumer object when a message is published to a subscribed destination.
The BlazeDS Message Service can also support and provideJava API to allow a server side component to publish or subscribe messages to a BlazeDS destination. Java Message Service (JMS) topics can also be mapped to work with Flex clients that publish and subscribe to JMS topics.
Step 5: Run the cricket producer application:
The message is received as after sending
Step 6: The message received by the consumer application is as below:
Conclusion
BlazeDS clients can leverage a message-based framework supported by BlazeDS. The message-based channels encapsulate the connection functionality between the Flex client and the BlazeDS server. These channels are collectively gathered into channel sets for channel discovery and channel reliability.