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

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

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

[{'answer_id': 64883, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 0, 'selected': False, 'text': '<p>You probably need to get a JMS connection from JNDI, like this:</p>\n\n<pre><code>Properties props = new Properties();\nprops.setProperty(Context.INITIAL_CONTEXT_FACTORY,\n    "org.apache.activemq.jndi.ActiveMQInitialContextFactory");\nprops.setProperty(Context.PROVIDER_URL,\n    "tcp://hostname:61616");\njavax.naming.Context ctx = new InitialContext(props);\n// lookup the connection factory\njavax.jms.TopicConnectionFactory factory = \n    (javax.jms.TopicConnectionFactory)ctx.lookup("ConnectionFactory");\n// create a new TopicConnection for pub/sub messaging\njavax.jms.TopicConnection conn = factory.getTopicConnection();\n// lookup an existing topic\njavax.jms.Topic mytopic = (javax.jms.Topic)ctx.lookup("MyTopic");\n// create a new TopicSession for the client\njavax.jms.TopicSession session = \n    conn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);\n// create a new subscriber to receive messages\njavax.jms.TopicSubscriber subscriber = session.createSubscriber(mytopic);\n</code></pre>\n'}, {'answer_id': 65144, 'author': 'jodonnell', 'author_id': 4223, 'author_profile': 'https://Stackoverflow.com/users/4223', 'pm_score': 3, 'selected': True, 'text': "<p>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.</p>\n"}, {'answer_id': 72352, 'author': 'James Strachan', 'author_id': 2068211, 'author_profile': 'https://Stackoverflow.com/users/2068211', 'pm_score': 1, 'selected': False, 'text': '<p>Have you tried reading the answers for <a href="https://stackoverflow.com/questions/66840/integration-of-javascript-and-jms">this question</a> which links to the <a href="http://activemq.apache.org/ajax.html" rel="nofollow noreferrer">ActiveMQ Ajax support</a>. Basically ActiveMQ has native support for Ajax so you can use its JavaScript library to directly subscribe from an ActiveMQ topic. </p>\n\n<p>Also see the <a href="http://activemq.apache.org/web-samples.html" rel="nofollow noreferrer">ActiveMQ web samples</a> which show how to do things like real time chat or real time stock portfolio screens using Ajax with ActiveMQ</p>\n'}, {'answer_id': 2361633, 'author': 'Henryk Konsek', 'author_id': 80711, 'author_profile': 'https://Stackoverflow.com/users/80711', 'pm_score': 2, 'selected': False, 'text': '<p>As James Strachan says - <a href="http://activemq.apache.org/ajax.html" rel="nofollow noreferrer">http://activemq.apache.org/ajax.html</a> is an ideal out-of-the-box solution for your problem.</p>\n\n<p>If you still want to create such solution manually you can just create JMS connection in your Ajax servlet (connection per request). Consider using Spring JMS template for that reason ( <a href="http://static.springsource.org/spring/docs/2.5.x/reference/jms.html" rel="nofollow noreferrer">http://static.springsource.org/spring/docs/2.5.x/reference/jms.html</a> ). Then just receive the message in the Servlet doGet/doPost method. Consider low timeout value for receiving in that case. Such solution will work for the Queues and durable Topics.</p>\n\n<p>For non-durable Topics consider external message listener. Spring <code>MessageListenerContainer</code> is an excellent tool for that purpose:</p>\n\n<pre><code>&lt;bean class="org.springframework.jms.listener.DefaultMessageListenerContainer\n   &lt;property name="connectionFactory" ref="jmsFactory"/&gt;\n   &lt;property name="destination" ref="myTopic" /&gt;\n   &lt;property name="messageListener" ref="lastTenUpdatesCache" /&gt;\n&lt;/bean&gt;\n</code></pre>\n\n<p>Bean <code>lastTenUpdatesCache</code> will be a singleton bean implementing <code>MesssageListener</code>. This bean would be responsible for caching last ten messages (just putting it into a java.util list). It will be injected into your Ajax servlet so in your doGet/doPost method you can ask it about last 10 messages sent to the topic.</p>\n'}]
['https://Stackoverflow.com/questions/64843', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1992/']