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.
MessageSender.java
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();
msg.setClientId(“Java-Based-Producer-For-Messaging”);
msg.setTimestamp(new Date().getTime());
//you can create a unique id
msg.setMessageId(“Java-Based-Producer-For-Messaging-ID”);
//destination to which the message is to be sent
msg.setDestination(“TestingDestination”);
//set message body
msg.setBody(messageBody != null?messageBody:”");
//set message header
msg.setHeader(“sender”, “From the server”);
//send message to destination
MessageBroker.getMessageBroker(null).routeMessageToService(msg, null);
}
sendmessage.jsp
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.
MessageFromServerSample.mxml
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 http://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
messaging-config.xml
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.
<destination id=”TestingDestination”>
</destination>
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


August 21, 2008 at 9:47 am
Hi,
I had set the default channel and destination as below inside the messaging config:
But it still show me [MessagingError message='Destination 'TestingDestination' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']
How can I to solve this problem??
Thank you.
August 21, 2008 at 9:49 am
August 22, 2008 at 2:01 am
Hi again,
I solved the problems and it’s works fine. It’s server problem. Because I’m using JBoss.
Thank you.
August 23, 2008 at 5:23 am
Hi C.Y,
Great to know you got it working
August 25, 2008 at 6:57 pm
I’m using glassfish and am getting the same destination has no channels defined message. What did you have to change on the server to get it working???
August 27, 2008 at 1:41 am
Hi – I’m getting a NullPointer when I do MessageBroker.getMessageBroker(null) in the Java code which is the producer. I set up the destination and set up the default channel as my-polling-mf. Am I missing anything? (Note I have Tomcat in the backend w/ no JMS provider).
August 27, 2008 at 6:58 pm
flex-to-flex producer-consumer works, but java (tomcat) to flex is not working (I instantiated an instance of MessageBrokerServlet – so I’m not getting the aforementioned null pointer exception). java seems to be sending messages but flex is not getting it. is there any special code/api to call in flex to register for events/messages i.e. channels, etc that I’m missing? any help would be greatly appreciated, thx in advance!
September 1, 2008 at 5:50 am
Hi Nathan,
You can try cleaning your project in Flex builder and also make sure you have the messaging destination configured appropriately on your server.
Hope this helps.
September 1, 2008 at 5:51 am
Hi PV,
I got the sample working with Tomcat and you don’t need a JMS provider. I can try to help you if you can send the files
Hope this helps.
September 5, 2008 at 2:56 pm
Hi,
I am trying to send messages from Java to Flex. I am getting this error when I run my application ‘The remoting server can only process messages of type ‘RemotingMessage’
please let me know if you like more info.
thank you
September 5, 2008 at 8:17 pm
I Fixed the previous error. I am just wondering if I receive messages as they occur on the server,
Thank you in advance,
Moussa
October 31, 2008 at 3:15 pm
Hi,
thank you for this MessageSending demonstration. But there is still a question left. I tried the same thing with the pushToClients() method of the MessageService class. That didnt work. All I got was a “no destination ‘chat’ exists in service flex.messaging.services.MessageService”. Can you explain why?
Thank you
regards
Sebastian
November 11, 2008 at 12:30 pm
Hi,
Flex to Flex is working fine….But I am facing issues in sending message from jsp….the message is sent without any error…but it is not appearing in flex client…if we use two flex clients, it is working fine…I think I am facing the same issue by PV
November 13, 2008 at 11:33 am
Hi,
i am getting this error :
[MessagingError message='Destination 'TestingDestination' either does not exist or the destination has no channels defined (and the application does not define any default channels.)']
i am using Tomcat 5.5
how can i overcome this problem…
Thanks in Advance
Chandra
November 19, 2008 at 11:08 am
Hi,
i am finding difficult to create and run a blazeds application. i am trying to use Flex Builder 3 and blazeds.war and pre-installed tomact 5.5.
wrote a simple chat example and whenever consumer tries to subcribe, i am getting following exception :
Error :
There was an unhandled failure on the server.
java.lang.NullPointException
please help me, what is this error and how i can overcome this.
and also please mention the steps i need to follow to create a BlazeDS
enabled application with FlexBuilder3 and pre-installed Tomcat.
Thanks in Advance
Chandra
November 25, 2008 at 3:03 pm
Hi Sebastin and Jinoy,
Can you please send the code so that I can see what is going wrong.
November 25, 2008 at 3:05 pm
Hi Chandra,
No destination found error comes when the destination is not properly configured. If you have configured your destination properly, then try cleaning your project in Flex Builder.
For the null pointer exception: which version of BlazeDS are you using?
Hope this helps.
December 1, 2008 at 4:40 pm
How would you do a reverse of that, Getting BlazeDS to send a message to a javaclient?
December 2, 2008 at 8:49 am
Hi Sujit,
i am using
Implementation-Title: BlazeDS – BlazeDS Application
Implementation-Version: 3.0.0.544
Implementation-Vendor: Adobe Systems Inc.
please let me know which version is compatible with Flex Builder 3 and Pre-Installed Tomcat 5.5
Thanks,
Chandra
December 4, 2008 at 3:09 pm
Hi andspot,
Please try Java AMF client. Please find more details below.
http://opensource.adobe.com/wiki/display/blazeds/Java+AMF+Client
Hope this helps.
December 22, 2008 at 6:54 am
Hi sujith,
Firstly you must be appreciated for the good work. All your posts are so easy to understand. Next, coming to the issues I have. I was successful in running all your examples on messaging on a local server in my system. But I am in a fix when doing it on remote server. I dont understand what fields to be given in Flex Builder while configuring the project i.e rootname floder etc.(hope u got it, the 3 feilds). The overall view of my problem is: I have already a java project into which I want to add blazeds to make use of the messaging service. So I would be thankful if you could help out regarding adding blazeds to an already existing j2ee project. Moreover, the project needs to be deployed on a remote server to run the application, So how to configure the various settings etc. Any help in this direction is greatly appreciated.
Regards,
Harsha Galla
December 23, 2008 at 6:25 am
Hi Harsha,
To add BlazeDS to an existing J2EE project you have to:
1. Copy the jar files from lib folder to your project lib folder
2. Change the web.xml to add URL mapping to MessageBrokerSevlet
3. Add a folder called Flex under WEb-INF folder and have services-config.xml and other configuration files.
You can get all the above resources from an existing BlazeDS based project.
Regarding having BlazeDS on a remote server i wrote a cookbook entry, please visit the URL below for details on how to configure the same.
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&postid=12209&loc=en_US&productid=2
Hope this helps.
December 23, 2008 at 11:33 am
Hi Sujith,
I got it to work myself, though after a lot of struggle.
Since it was deployment in SAP NWDS it was difficult to figure it out in the direct approach as given by you since SAP’s NWDS differs a lot when compared to other server deployments.
One observation I made which probably was the key was:
My web application when deployed on server runs in context root “Prefetch”. So when I build my application in Flex Builder in the configuration I should give “\Prefetch”
Moreover I have one more issue.
I have tried your messaging example also. It was working fine, but I did some changes like, as and when the consumer recieves a message an alert shows the message. Somehow Alert was not working but the chat text was being passed between the two browser windows. What could be the reason.
My idea was to publish messages to subscribers when some changes on the server occur so that as and when the consumer gets a message a httprequest is invoked to update the data in his window. Is this feasible??
Anyways cheers to you for the good work.Probably anyone wants to know abt deploying blazeds on NWDS i can help out as there is not a single document covering this.
January 12, 2009 at 2:59 pm
Hi Sujith,
Did you fix pv and Jinoy’s problem ?
I have the same problem: Flex to Flex is working fine, but the message sent from jsp does not pop-up in the flex client…
Thanks.
Claude
January 12, 2009 at 3:55 pm
Hi Sujith,
I eventually fixed the problem. I had put the java class in blazeds\WEB-INF\classes instead of samples (where is the directory of my flex app) \WEB-INF\classes.
Now everything is working fine
Claude
January 13, 2009 at 5:17 am
hi Claude,
good to know that u got it working
January 13, 2009 at 7:27 pm
Hi Sujit
First of all thanks a lot for your great articles. This is an excellent resource for all flex programmers.
I have a question related to pushing asynchronous message from server to client. I want to create a timer in my java class to periodically send data to the client. I followed your example and it worked fine. However when the client goes away i.e. when I close the browser instances, the timer continues to run forever. Is there a way to know from my java classes that no client exists for that subscription and then I can stop the timer?
Thanks
Abhijit
January 20, 2009 at 1:28 pm
Hi Abhijit,
The destination will be of the type MessageDestination, so invoke getSubscriptionManager() on the destination object. SubscriptionManager has a method getSubscriberIds(Message message, boolean evalSelector). You can try this and check if there are any valid subscribers.
Hope this helps.
January 22, 2009 at 9:03 am
Hi Harsha,
I am facing issues when trying this on SAP. Can you please share the document you have created for deploying the flex app on SAP.
Thanks
Kiran.
February 5, 2009 at 1:42 am
Hi Sujit!! getSubscriberIds() works fine. Thanks a lot!!
–Abhijit
February 28, 2009 at 6:26 am
HI Harsha
we are facing some issues to make blazeDS work in NW
and it seems you made it work
Cna you help us please
you said
“My web application when deployed on server runs in context root “Prefetch”. So when I build my application in Flex Builder in the configuration I should give “\Prefetch”
can you elobarate, where you give \Prefetch
Thanks Alot
Kasem
May 6, 2009 at 7:05 pm
Hi
I am getting a error
Servlet MessageBrokerServlet is currently unavailable
What are the causes of error and how can I fix this
I myself tried few fixes
like checking the config and web.xml file
May 18, 2009 at 4:29 pm
Hi Vinay,
Please check your web server stdout for error messages.
Hope this helps.
May 28, 2009 at 11:00 pm
Hi,
Your sample works as long as you are in the same war file. I am currently looking into the integration of flex in WebSphere Portal. This means having several war files. I would like to use the MessageBroker in portlets in order to send messages that other flex portlet can use. Is there a way to remotely invoke the MessageBroker, so from a java class in a different war file?
Thanks,
Dennis
June 15, 2009 at 5:51 pm
Hi Dennis,
Is it possible for all SWF in your portlets subscribe to a single messaging destination ?
July 6, 2009 at 2:26 pm
Hi Sujit,
Great post! Thanks for making it so easy.
I have a question. even when the messaging-config xml declaration has utf-8, why is the application not showing other language messages in the correct format?
waiting for your reply
Thanks
Priya
July 7, 2009 at 9:15 pm
Hi Sujit,
Greate post!
I am new to Flex and Flex dataservices. I have to write an application where flash clients have to update themselves when they receive messages (real-time) from a J2EE server (Glassfish) through BlazeDS.
I tried to implement this via the mx:producer/mx:consumer paradigm using the code you decribed in your tutorial above.
When I ran it in the browser (Internet Explorer 6), the flash client(s) did not receive the messages that I send either via a flash producer nor via a servlet/jsp from the J2EE server. However, if I press in the browser to do a refresh, from that moment on, all the messages that are send, are received by the flash application in the browser.
Why do I have to do a refresh? Does this look like a coding/logic error?
Any help is very much appreciated.
Petran.
July 20, 2009 at 9:15 pm
Hi Priya,
I don’t the declaration in messaging-config.xml has anything to do with the message formats. Please try sharing code to reproduce problem faced. Try sending code to sujitreddy.g@gmail.com
Hope this helps.
July 20, 2009 at 9:18 pm
Hi Petran,
Not sure, please try sharing code to reproduce this. Send code to sujitreddy.g@gmail.com
August 13, 2009 at 2:09 am
Hi Sujit,
thank you for your help!
im stay the same problem that Petran,
I need refresh on page in IE but Firefox dont need;
thank you!
please help me juliobcosta@yahoo.com.br
August 21, 2009 at 11:31 pm
Hello Sujit,
Thanks for the great tutorial. I have a problem. We have started developing the application 6 months ago and we did not plan for Blazeds. My flex code runs as a non – server side application in Jboss server. But it communicates with the Spring server using HTTP calls.
Now we want Blazeds in our app. Is it possible to use Blazeds with the NonServer side app? (We still have WEB-INF) etc., complete J2EE environment.
Thanks in advance.
August 28, 2009 at 7:12 pm
Hi Petran/Julio,
Can you please check if this not a cache problem.
August 28, 2009 at 7:59 pm
Hi sai,
Yes, of course. Please check the articles below
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showDetails&postId=13810&productId=2&loc=en_US
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showDetails&postId=12209&productId=2&loc=en_US
Hope this helps.
August 31, 2009 at 1:37 am
Hello Sujit,
Thanks for the reply, This works after 2 days of struggle. Thanks a lot!!!
November 8, 2009 at 10:04 pm
while using remoteobject am getting following faultString…why it’s showing like ???
[MessagingError message='Destination loginServiceImpl' eight does not exist
or the destination has no channels defined(and the application does not defined any default channels.)']