Home  >  

Pushing Data Around With Blaze/LCDS

Author photo
AddThis Social Bookmark Button
Here's a quick tip for working with real time data in Flex applications (in particular Blaze Data Services and LiveCycle Data Services). LCDS isn't new by any means, but not everyone uses it on a daily basis. Here are a few ways that you can push real-time data between the server and client applications.

First of all, you can send and receive messages using Producer and Consumer objects

With your messaging channels configured, Sending data back and forth between two clients is easy. You send data using a Producer object. Producers are used to data from Flex client instances into the LCDs/BlazeDS messaging system. You just need to create an AsynchMessage object and send it to the messaging system using the send() function. The message data can either be a simple type (string, number), or it can be a complex value-object. You'll just ned to register classes accordingly to have them handled correctly. Below is a basic example of sending a message across the real-time messaging system.

var messsage:AsyncMessage = new AsyncMessage( myMessage );
myProducer.send( message ) 
This message will be received by all clients that are subscribed to the same messaging system.

Receiving a message from the LCDS system is just as easy. The difference is that you use a Consumer object to consume data from the messaging system. With the consumer configured to point at the messaging channel, you just need to add a listener for MessageEvent.MESSAGE events on the consumer object. These will be dispatched any time that a message is received on the consumer.

myConsumer.addEventListener( MessageEvent.MESSAGE, onMessage );
And you can handle them accordingly on the client side... Whether it is updating a view, displaying an alert, or showing some other kind of interaction.

private function onMessage( event : MessageEvent ):void 
{	
 	//do something now that you have received a message
}
Now, this is where things get really interesting... Messages don't always have to originate from a Flex client to be received by other Flex clients. Messages can originate in a java process and be pushed to subscribed clients. The following code will enable you to send a message originating in the java server tier to those subscribed client machines.

//create a new message
AsyncMessage message = new AsyncMessage();

//should be a unique id that identifies the message origin (commonly a GUID)
message.setClientId( myClientId );  

//set the destination -- this should match the 
//messaging destination id in messaging-config.xml
message.setDestination( "myDestination" );

//set a unique message id and timestamp
message.setMessageId( UUIDUtils.createUUID(false) );
message.setTimestamp( System.currentTimeMillis() );

//set the content of the message - can be an object or a string
message.setBody( messageContent );  

//send the message to subscribed clients
MessageBroker.getMessageBroker(null).routeMessageToService(msg, null);
This kind of setup can be used in a variety of ways... broadcast messages server processes, having a service invocation dispatch data synchronization or status messages, having a rpc service stream data back to a client, among many other scenarios. This technique is extremely useful.

___________________________________
Andrew Trice
Principal Architect
Cynergy Systems
http://www.cynergysystems.com

Read more from Andrew Trice. Andrew Trice's Atom feed

Comments

6 Comments

Joel said:

Hi Andrew,

Can BlazeDS/LCDS converse with JMS via other J2EE applications in the same container? Like say I have a REST api that a user updates from a device/site, could that action notify my AMF clients that an update has occurred?

Dane said:

Nice summary. Do you know if the code to send messages originating on the server also be done in ColdFusion in a similar manner?

Andrew Trice said:

Joel, Yes, you can configure LCDS to communicate with JMS channels. I haven't played with those much, but I do know that it is possible. However, it also depends what you mean by "AMF clients". If you are using AMF as a polling mechansim in LCDS or Blaze DS, then yes. However if you are only using AMF remoting/serialization (no messaging), then no you can't.

Dane, Yes, you have to use the CF Event Gateway Adapter to send messages from CF to LCDS/BlazeDS. I haven't tried this in a few years... but if I remember correctly, there are a few gotchas if you are trying to do subtopic or selector based filtering of messages sent using the CF event gateway

Oussama said:

Nice work.
I work actually on a data push application using flex and java.
I've tested many examples to use messaging service in BlazeDS.
Every time a null pointer exception is occuried when I want to instanciate a "MessageBroker" to route message to destination as you have written in the article.
I don't know if I should specify an adapter to have a working sample??

Andrew Trice said:

Make sure you have your messaging destination and messaging channels defined properly. If you have more than one destination, you might need to access the destination by name/id, instead of just calling getMessageBroker with a null parameter.

Oussama said:

In fact I think that there is a problem with MessageBrokerServlet.
I got an aphache error message like this:
INFO: La servlet MessageBrokerServlet est marqué comme indisponible
4 mai 2009 10:00:18 org.apache.catalina.core.StandardContext loadOnStartup
GRAVE: La servlet /BlazeDS_GoogleChart a généré une exception "load()"
javax.servlet.UnavailableException: com/espertech/esper/client/UpdateListener
at flex.messaging.MessageBrokerServlet.init(MessageBrokerServlet.java:170)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:981)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4045)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4351)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:566)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

Leave a comment


Tag Cloud

Question of the Week: Open Source Flex Projects

What would you say are the 5 most prominent open source projects in the Flex world?

Answer

Latest Features

Recommended for You

@InsideRIA on Twitter

Archives

  • Or, visit our complete archive.  

About This Site

Welcome to the premiere community site for all things RIA sponsored by O'Reilly Media and Adobe Systems Incorporated.