title: How to send jms message from java app to jetty webapp
body: <p>I have Jetty hightide 8.1.8.v20121106 running and have deployed a tiny webapp using jquery/javascript to listen onto a JMS topic and when a message is received it will display it on the page. I have a java app that will connect to a JMS topic and publish messages, I want those messages published by the Java app to be consumed by the webapp on Jetty.</p>
  
  <p>Jetty is not connecting to tcp://localhost:61616 when the javascript webapp publishes a message or listens for a message. The logs show Jetty connecting to vm://localhost all the time. Is there any way to make Jetty use my ActiveMQ server instead of its own embedded one? Help is appreciated.</p>
  
  <p>Config files below:</p>
  
  <p><strong>jetty-plus.xml</strong></p>
  
  <pre><code>&lt;New id="cf" class="org.eclipse.jetty.plus.jndi.Resource"&gt;
      &lt;Arg&gt;jms/connectionFactory&lt;/Arg&gt;
      &lt;Arg&gt;
            &lt;New class="org.apache.activemq.ActiveMQConnectionFactory"&gt;
                  &lt;Arg&gt;tcp://localhost:61616&lt;/Arg&gt;
            &lt;/New&gt;
      &lt;/Arg&gt;
  &lt;/New&gt;
  
  &lt;New id="interestingTopic"  class="org.eclipse.jetty.plus.jndi.Resource"&gt;
      &lt;Arg&gt;jms/interestingTopic&lt;/Arg&gt;
      &lt;Arg&gt;
            &lt;New class="org.apache.activemq.command.ActiveMQTopic"&gt;
                  &lt;Arg&gt;my.test&lt;/Arg&gt;
            &lt;/New&gt;
      &lt;/Arg&gt;
  &lt;/New&gt;
  </code></pre>
  
  <p><strong>web.xml</strong> for webapp</p>
  
  <pre><code>&lt;web-app&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;org.apache.activemq.brokerURL&lt;/param-name&gt;
          &lt;param-value&gt;tcp://localhost:61616&lt;/param-value&gt;
          &lt;description&gt;The URL of the Message Broker to connect to&lt;/description&gt;
      &lt;/context-param&gt;
  
      &lt;context-param&gt;
          &lt;param-name&gt;org.apache.activemq.embeddedBroker&lt;/param-name&gt;
          &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;description&gt;Whether we should include an embedded broker or not&lt;/description&gt;
      &lt;/context-param&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;session&lt;/filter-name&gt;
          &lt;filter-class&gt;org.apache.activemq.web.SessionFilter&lt;/filter-class&gt;
          &lt;!-- &lt;filter-class&gt;org.eclipse.jetty.continuation.ContinuationFilter&lt;/filter-class&gt; --&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;session&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;listener&gt;
                  &lt;listener-class&gt;org.apache.activemq.web.SessionListener&lt;/listener-class&gt;
          &lt;/listener&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;AjaxServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.apache.activemq.web.AjaxServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;MessageServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.apache.activemq.web.MessageServlet&lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;AjaxServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/amq/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;MessageServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/message/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;!-- Default page to serve --&gt;
      &lt;welcome-file-list&gt;
          &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
      &lt;/welcome-file-list&gt;
  
      &lt;resource-env-ref&gt;
                  &lt;resource-env-ref-name&gt;jms/interestingTopic&lt;/resource-env-ref-name&gt;
                  &lt;resource-env-ref-type&gt;javax.jms.Topic&lt;/resource-env-ref-type&gt;
          &lt;/resource-env-ref&gt;
  
      &lt;resource-ref&gt;
              &lt;description&gt;Connection Factory&lt;/description&gt;
          &lt;res-ref-name&gt;jms/connectionFactory&lt;/res-ref-name&gt;
          &lt;res-type&gt;javax.jms.ConnectionFactory&lt;/res-type&gt;
          &lt;res-auth&gt;Container&lt;/res-auth&gt;
      &lt;/resource-ref&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p><strong>My Java script code for listening on the topic</strong></p>
  
  <pre><code>window.onload = function() {
      org.activemq.Amq.init({ uri: 'amq', logging: true, timeout: 45, clientId:('geo_1') });
  };
  
  var amq = org.activemq.Amq;
  var msgTopic = 'topic://my.test';
  var clientId = 'geo_1';
  
  function addTopicListener() {
      var myHandler = { 
              rcvMessage: function(message) {
                  //&lt;message type="zmg"&gt;test test&lt;/message&gt;
                  var type = message.getAttribute('type');
  
                  switch (type) {
                      case 'zmsg':
                      {
                          var text = message.childNodes[0].data;
                          document.getElementById('zmsg_container').innerHTML += ' | '+text;
                          break;
                      }
                  }
              }
      };
  
      amq.addListener('myHandler', msgTopic, myHandler.rcvMessage);
  };
  
  function delTopicListener() {
      amq.removeListener('myHandler', msgTopic);
  }
  </code></pre>
  
  <p><strong>And lastly the code in my Java app for publishing</strong></p>
  
  <pre><code>private void init() throws NamingException {
          Properties props = new Properties();
          props.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
          props.setProperty(Context.PROVIDER_URL,"tcp://localhost:61616");
          props.setProperty("topic.test", "my.test");
          ctx = new InitialContext(props);
  
          System.out.println("JMS Context Initialized");
      }
  
      private void connect() throws NamingException, JMSException {
          connectionFactory = (TopicConnectionFactory)ctx.lookup("ConnectionFactory");
          conn = connectionFactory.createTopicConnection();
          myTopic = (Topic)ctx.lookup("test");
          session = conn.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
          publisher = session.createPublisher(myTopic);
  
          System.out.println("JMS Connection started");
      }
  </code></pre>