I am building a site that uses a simple AJAX Servlet to talk JMS (ActiveMQ) and when a message arrives from the topic to update the site.
I have Javascript that creates an XMLHttpRequest for data. The Servlet processes the Get Request and sends back JSON. However I have no idea how to connect my Servlet into my ActiveMQ Message Broker. It just sends back dummy data right now.
I am thinking the Servelt should implement the messagelistener. Then onMessage send data to the JavaScript page. But I’m not sure how to do this.
The problem with having a servlet implement MessageListener is that servlets are synchronous and MessageListeners are asynchronous. Instead you should create some other object to act as the MessageListener and update some state somewhere (possibly a database or a JMX MBean or a Stateful Session EJB) when messages come in. Then the servlet can query that state to see if there’s data to report back to the client, and your web page can periodically ping the servlet to ask for fresh data.