Sending messages from Java to BlazeDS destinations using MessageBroker

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:

public void sendMessageToClients(String messageBody)


AsyncMessage msg = new AsyncMessage();


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

//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

MessageBroker.getMessageBroker(null).routeMessageToService(msg, null);



Download the JSP from this URL:

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

Download this MXML file from this URL:


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”>


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 🙂

58 Responses to Sending messages from Java to BlazeDS destinations using MessageBroker

  1. C.Y says:

    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.

  2. C.Y says:

    Hi again,
    I solved the problems and it’s works fine. It’s server problem. Because I’m using JBoss.
    Thank you.

  3. Sujit Reddy G says:

    Hi C.Y,

    Great to know you got it working 🙂

  4. Nathan Miller says:

    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???

  5. pv says:

    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).

  6. pv says:

    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!

  7. Sujit Reddy G says:

    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.

  8. Sujit Reddy G says:

    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.

  9. Mikey says:


    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

  10. Mikey says:

    I Fixed the previous error. I am just wondering if I receive messages as they occur on the server,

    Thank you in advance,


  11. Sebastian says:


    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”. Can you explain why?

    Thank you



  12. Jinoy says:

    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

  13. Chandra says:

    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

  14. Chandra says:

    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.

    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

  15. Sujit Reddy G says:

    Hi Sebastin and Jinoy,

    Can you please send the code so that I can see what is going wrong.

  16. Sujit Reddy G says:

    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.

  17. andspot says:

    How would you do a reverse of that, Getting BlazeDS to send a message to a javaclient?

  18. Chandra says:

    Hi Sujit,
    i am using
    Implementation-Title: BlazeDS – BlazeDS Application
    Implementation-Vendor: Adobe Systems Inc.

    please let me know which version is compatible with Flex Builder 3 and Pre-Installed Tomcat 5.5


  19. Sujit Reddy G says:

    Hi andspot,

    Please try Java AMF client. Please find more details below.

    Hope this helps.

  20. Harsha Galla says:

    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.

    Harsha Galla

  21. Sujit Reddy G says:

    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.

    Hope this helps. 🙂

  22. Harsha Galla says:

    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.

  23. Claude says:

    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…



  24. Claude says:

    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 😉

  25. Sujit Reddy G says:

    hi Claude,

    good to know that u got it working 🙂

  26. abhi says:

    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?


  27. Sujit Reddy G says:

    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. 🙂

  28. kiran says:

    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.


  29. abhi says:

    Hi Sujit!! getSubscriberIds() works fine. Thanks a lot!!


  30. Kasem Abotel says:

    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


  31. vinzy says:


    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

  32. Hi Vinay,

    Please check your web server stdout for error messages.

    Hope this helps.

  33. Dennis says:


    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?


  34. Hi Dennis,

    Is it possible for all SWF in your portlets subscribe to a single messaging destination ?

  35. Priya says:

    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

  36. Petran Bisschops says:

    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.


  37. 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

    Hope this helps.

  38. Hi Petran,

    Not sure, please try sharing code to reproduce this. Send code to

  39. Julio Costa says:

    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

  40. Sai says:

    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.

  41. Hi Petran/Julio,

    Can you please check if this not a cache problem.

  42. Sai says:

    Hello Sujit,

    Thanks for the reply, This works after 2 days of struggle. Thanks a lot!!!

  43. vasu says:

    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.)’]

  44. Somoza says:

    Hello Sujit

    I have a jBPM process running in jBoss and I have also a flex project running in jBoss. The jBPM process invokes methods of the java code located in the flex project. They are usually database querys. Now what I want is to send a message from the jBPM process to flex in order to show an alert window. Here comes my problem, your example works without problem the class and the jsp file (very clear, great job). But at the time I call from the jbpm process to the class I get java.lang.NullPointerException error. I have tested with the java class method sendMessageToClients, in the jBPM process either in the flex project folder, but with the same result the error.

    Am I doing something wrong? What am I missing?

    thanks in advance.

  45. Sportscar says:

    Hi Sujit,
    Thank for your tutorial, I try to send a java object (value object) to the flex client. like traderDesktop. the question is: when the client receive the message. all the member uninitialized (for example:number field is NaN, Date field is Null ). I implements Serializable and add serialVersionUID field in my Object. any other point I forgot?

  46. Kilian says:


    I also get a null pointer when I try to instantiate MessageBroker by doing MessageBroker.getMessageBroker(null). Flex to Flex messaging works fine. I also tried setting the init-param messageBrokerId in web.xml and calling getMessageBroker using the specified id, but this also doesn’t work.
    Any ideas how to solve this problem?

  47. sao says:

    Hey kilian did you figure out the problem ? i’m getting the same problem while trying to get the handle to the message broker.

  48. Sai says:

    Hello sujit,

    thansk for the great article. I’m running the blazeds, it is working fine on the dataPush, but the only problem is, I have to refresh my browser to get the subscription as a consumer. it is not a feasible solution, how do i get rid of this? I want the client to get handle to the stream in first try.


  49. ATEJEDA says:

    I was doing a lot of tests with messaging system of blazeDs, it seams that the getMessageBroker(null) it only works under a thread.

    In a extended thread class, the run method have to send the message or the run method have to call the self class method to send the message, also, the send message must have a delay (700ms aprox, i don’t know yet why have to do this).

    I have doing a lot of research about this, this only works under a thread class and the message must be sended from the run method or a self method called by the run method.

  50. Somoza says:

    Hey atejeda!

    nice clue, I was thinking something like that but could you explain with an example?

    I don’t understand

    “I have doing a lot of research about this, this only works under a thread class and the message must be sended from the run method or a self method called by the run method.”

    Do I have to create the thread to put it running?


  51. Alberto acevedo says:


    Excellent article.

    There is a memory leak with the Asyncmessage submitted the Adobe JIra web site:

    When sending the message with:

    MessageBroker.getMessageBroker(null).routeMessageToService(msg, null);

    The messages stay in the heap when blazeds is under a heavy load. When I say heavy load I mean 40 messages per second each having a body loaded with 2 MB of data as objects. Have you encountered this problem? If yes, do you have a workaround to fix the issue?



  52. peter says:

    i’ve seen a few comment that they get NULL returned from this line of code:


    However, i haven’t seen any specific mention of remedy. i am running my code in Eclipse 3.5.2. I have imported the flex-messaging-core.jar (as well as flex-messaging-common.jar because i was getting a ClassDefNotFoundException without it). My code compiles and runs fine up to this line. It returns NULL when i request the MessageBroker.

    Do you have any suggestions about how to remedy this? I am running BlazeDS 4 Turnkey, the server works. I know this for a fact because i’ve used it for other things.

    Any help is appreciated!

  53. Hi Peter,

    If the null is returned that means there are no MessageBroker instances created. MessageBroker instances are created when the BlazeDS boots, which will happen when the server starts up. Please try running this code on the server, should work.

    Hope this helps.

  54. Rupen says:

    Hello Sujit,

    I am facing the error MessagingError message=’Destination ‘DestinationName’ either does not exist or the destination has no channels defined and i am sure its not a path or destination problem .. Coz while am creating Air application its working but if am creating WEB application it fails with this error .. Any help will be greatly appreciated ..


  55. peter says:

    thanks for the reply. i got it working.

    it turned out i wasn’t running the code in the server. i ended up installing the Eclipse Tomcat plugin, wiring it up to my Tomcat install, then running the code from within Eclipse with Tomcat running. once i did that it all worked great.
    thanks again!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: