« Installing and using Weborb.NET platform for Flex clients | Main | Understanding and improving Flex bindings »

Messaging from Java to Flex without JMS

With BlazeDS/LCDS you can send messages to and receive messages from a server side destination thanks to Messaging Services and their client-side API.

In your messaging configuration you can use a ActionScript Adapter:
<adapter-definition id="actionscript" class="flex.messaging.services.messaging.adapters.ActionScriptAdapter" default="true" />

or a JMS Adapter:
<adapter-definition id="jms" class="flex.messaging.services.messaging.adapters.JMSAdapter"/>

that integrates Flex messaging with Java Message Service destinations.

If you don't have JMS or you don't want to use it in your web application you can send messages from Java to Flex in a very simple way.

First of all you should enable the ActionScript adapter and create your destination without additional parameters:


<destination id="actionscriptMessaging">

</destination>

In your Flex application you have your Consumer:


<mx:Consumer id="consumer"
destination="actionscriptMessaging"
message="messageHandler(event);"
fault="faultHandler(event);"/>

that is subscribed to the previous destination so that it can receive messages:


consumer.subscribe();

On the Java side, inside the context of the web application in which LCDS/BlazeDS runs, you can use these lines of codes to send messages to your Flex Consumer:


AsyncMessage msg = new AsyncMessage();
msg.setTimestamp(new Date().getTime());

msg.setClientId("JavaMessageProducer");
msg.setMessageId("JavaMessageId");

//destination configured in messaging-config.xml
msg.setDestination("actionscriptMessaging");

msg.setBody("Message from Java!");

//you can set custom message headers
msg.setHeader("headerParam1", "value1");
msg.setHeader("headerParam2", "value2");

//send message to destination
MessageBroker.getMessageBroker(null).routeMessageToService(msg, null);

This is a solution suitable for uni-directional messaging, if you want to use a bi-directional messaging system it's convenient to use JMS.

TrackBack

TrackBack URL for this entry:
http://blog.comtaste.com/mt-tb.cgi/87

Comments (3)

Jelou:

Is it possible to do the same with webORB? is there any example?

thanks

kk:

That was very informative. Is there a way to do bi-directional without using JMS ??

Thanks
KK

Thanks! Works like a charm

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on March 13, 2009 11:07 AM.

The previous post in this blog was Installing and using Weborb.NET platform for Flex clients.

The next post in this blog is Understanding and improving Flex bindings.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type 3.33