How to send jms message from java application to jetty webapp
I have Jetty hightide 8.1.8.v20121106 running and deployed a tiny webapp using jquery / javascript to listen to the JMS theme and when the message is received it will display it on the page. I have a java application that will connect to a JMS topic and post messages. I want those posts posted by Java app to be consumed by webapp on Jetty.
Jetty is not connecting to tcp: // localhost: 61616 when javascript webapp is posting message or listening for message. The logs show that Jetty is constantly connecting to vm: // localhost. Is there a way to get Jetty to use my ActiveMQ server instead of its built-in one? Help is appreciated.
The config files are below:
berth-plus.xml
<New id="cf" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jms/connectionFactory</Arg>
<Arg>
<New class="org.apache.activemq.ActiveMQConnectionFactory">
<Arg>tcp://localhost:61616</Arg>
</New>
</Arg>
</New>
<New id="interestingTopic" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jms/interestingTopic</Arg>
<Arg>
<New class="org.apache.activemq.command.ActiveMQTopic">
<Arg>my.test</Arg>
</New>
</Arg>
</New>
web.xml for webapp
<web-app>
<context-param>
<param-name>org.apache.activemq.brokerURL</param-name>
<param-value>tcp://localhost:61616</param-value>
<description>The URL of the Message Broker to connect to</description>
</context-param>
<context-param>
<param-name>org.apache.activemq.embeddedBroker</param-name>
<param-value>true</param-value>
<description>Whether we should include an embedded broker or not</description>
</context-param>
<filter>
<filter-name>session</filter-name>
<filter-class>org.apache.activemq.web.SessionFilter</filter-class>
<!-- <filter-class>org.eclipse.jetty.continuation.ContinuationFilter</filter-class> -->
</filter>
<filter-mapping>
<filter-name>session</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.apache.activemq.web.SessionListener</listener-class>
</listener>
<servlet>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>org.apache.activemq.web.AjaxServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>org.apache.activemq.web.MessageServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/amq/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>MessageServlet</servlet-name>
<url-pattern>/message/*</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<resource-env-ref>
<resource-env-ref-name>jms/interestingTopic</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Topic</resource-env-ref-type>
</resource-env-ref>
<resource-ref>
<description>Connection Factory</description>
<res-ref-name>jms/connectionFactory</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
My Java script code to listen on a topic
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) {
//<message type="zmg">test test</message>
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);
}
And finally the code in my Java app for posting
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");
}
source to share
After literally cleaning up the internet and browsing many websites about configuring Jetty and ActiveMQ; I finally came across a website that discusses jetty clustering with activemq and with jetty use the activemq web console ( http://java.dzone.com/articles/activemq-configuring-multiple ). This site really helped.
So I just missed the extra XML config section in Jetty. With the XML configuration I have above, I had to add the following:
<New id="url" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jmx/url</Arg>
<Arg>
<New class="java.lang.String">
<Arg>service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi,service:jmx:rmi:///jndi/rmi://localhost:1299/jmxrmi</Arg>
</New>
</Arg>
</New>
After that I started Jetty and ActiveMQ, and enough the haven connected to ActiveMQ rather than creating its own vm: // localhost connection as it did previously by default.
Thanks to those who helped, it allowed me to focus on my problem.
source to share
This line in your Java code will create an instance of an ActiveMQ instance in the local VM (it's a bit of a double-edged sword ...):
props.setProperty(Context.PROVIDER_URL,"vm://localhost?broker.persistent=false");
If you have another ActiveMQ broker running on myamqhost then change this line to something like this (assuming the broker is listening on port 61616):
props.setProperty(Context.PROVIDER_URL,"tcp://myamqhost:61616");
See ActiveMQ JNDI Support .
You can also look at the ActiveMQ Ajax Client .
source to share