Integrating with ActiveMQ
For using ActiveMQ queues and topics with Jetty, you have two choices:
- Traditional server-side jms api calls
ActiveMQ with Ajax and Jetty
Here's an example of sending a message to a queue:
Here's an example of setting up to receive messages from a queue. All you need do is to add a listener with a unique id, the name of the queue or topic from which you want to receive messages, and the name of a function which will be called back when a message is available.
Here's the plumbing you need to take advantage of this very convenient feature:
- Include these lines in your html:
- Include thse lines in your
web.xml, changing the url of the broker as appropriate for your installation:
- Include the
activemq-web.jarand all of its dependencies in your webapp, or alternatively, copy them to the $JETTY-HOME/lib/ext directory if you want them to be available for all of your webapps. Here's a maven2 dependency clause that you might find useful:
You will also need to make sure that jcl104-over-slf4j-1.0.1.jar and an slf4j log implementation jar are available at runtime, as ActiveMQ uses commons logging. You can move these jars out of $JETTY-HOME/lib/jsp-2.0 and into $JETTY-HOME/lib to make them generally available on the classpath.
This ActiveMQ Ajax mechanism is based on Jetty's Continuations mechanism and thus in fact works most efficiently when deployed on Jetty, however the same webapp will also run unaltered on other containers.
ActiveMQ with JNDI lookups from Servlets
You can use ActiveMQ queues and topics in the traditional manner from Servlet code with Jetty. Firstly, if you are not familiar with how JNDI resources are configured with Jetty, take a look at the JNDI Feature page.
Now lets look at the steps you need to perform to be able to access queues and topics:
- Copy all of the activemq jars and their dependent jars into
- Make sure that you have jcl104-over-slf4j-1.0.1.jar and an slf4j log implementation jar available on the classpath. These jars are available in
$JETTY-HOME/lib/jsp-2.0, but if you are not using jsp-2.0, you'll need to move them out of there and either up one level into
$JETTY-HOME/libor down into
- Edit the
$JETTY-HOME/etc/jetty-plus.xmlfile and define your connection factories and any queues or topics that you want to be available in a JNDI lookup from any webapp. If you want to make any of these resources scoped to a single webapp, then put the setup instead into a jetty-env.xml file and put it in your webapps
WEB-INF/directory. Here's an example of a QueueConnectionFactory, change the broker url to be appropriate for your setup:
Here's an example of a Queue:
- Edit your
WEB-INF/web.xmlfile for your webapp to link up to the resources you defined in the step above. Following our example ConnectionFactory and Queue, we'd add:
- Code your servlet to use JNDI lookups to access and operate on these resources. For example, here's the code snippets to put a message onto a queue (these full example is MyQueueServlet.java):
- Now deploy your webapp to
- Run it with: