We discussed BlazeDS/LCDS messaging samples in which we produce messages from a Flex application and consume that message from another or same Flex application. BlazeDS/LCDS also allows us to publish messages to messaging destinations from a Java class on the server.
As BlazeDS/LCDS allows us to publish messages from a Java class on server, we can build a JSP/Servlet which will invoke this Java class and publish messages to BlazeDS/LCDS messaging destination. We can establish messaging between and Flex and Non-Flex client.
I created a simple sample in which I am sending messages from a JSP page to a Flex client. This is so simple. All you need to do is to create a Java class with one method in it and a JSP which will invoke this Java class. You will also create Flex application which will subscribe to the destination to which we will publish messages from our Java class.
Let’s start coding:)
First we will create the Java class which will publish the message.
We have only one method in this class called sendMessageToClients. In this method we will create a message of the type AsyncMessage and publish that message using MessageBroker. AsyncMessage and MessageBroker class are available with BlazeDS in the flex-messaging-core.jar. You should be adding this jar to your project libraries to compile this class. Download the Java file compile it and deploy it in WEB-INF/classes folder of your web application.
Download this Java file from this URL: http://sujitreddy.g.googlepages.com/MessageSender.java
public void sendMessageToClients(String messageBody)
AsyncMessage msg = new AsyncMessage();
//you can create a unique id
//destination to which the message is to be sent
//set message body
msg.setBody(messageBody != null?messageBody:””);
//set message header
msg.setHeader(“sender”, “From the server”);
//send message to destination
Download the JSP from this URL: http://sujitreddy.g.googlepages.com/sendmessage.jsp
In this JSP file we will just invoke the Java class created above.
In this MXML file we will be subscribing to the destination and also act as a producer for the same destination. You can find more details on messaging in Flex at this URL https://sujitreddyg.wordpress.com/2008/01/17/messaging-using-flex-and-blaze-ds/
Download this MXML file from this URL: http://sujitreddy.g.googlepages.com/MessageFromServerSample.mxml
This is file in which we will configure our destination to which we are publishing messages and subscribing to get messages. Just add the XML tag below as a child to <service> tag in messaging-config.xml file in your BlazeDS configured web application under Web-INF/flex folder. Please make sure you have default channel set in the messaging-config.xml.
That is all you need to do. Run the Flex application and then try some text and see if you are receiving the messages as the Flex application is acting as both producer and consumer for the same destination. If you are receiving the messages send by you, which means the messaging destination is configured properly. Now just launch the sendmessage.jsp page from the browser, you should be able to see the message sent from the Java class in the Flex client.
That’s it:) Adobe Rocks 🙂