March 25, 2010

Configuring REST for ActiveMQ

I recently decided I wanted to try out the REST interfaces for ActiveMQ. My assumption going into this was that it was implemented as a transport connector much like STOMP and OpenWire. Adding to my confusion was the fact that there is a transport connector called "http". If you check out the samples, you will see that the demo application uses the REST interface via a MessageServlet in the demo webapp. Likewise AjaxServlet implements the AJAX interface used by amq.js.

To use these things you will need to embed them in a servlet container. The default ActiveMQ configuration using Jetty:

 <import resource="jetty.xml"/> 


The Jetty bean config includes a demo webapp:

 <beans>  
<jetty xmlns="http://mortbay.com/schemas/jetty/1.0">
<connectors>
<nioConnector port="8161"/>
</connectors>
<handlers>
<webAppContext contextPath="/demo" resourceBase="${activemq.base}/webapps/demo" logUrlOnStart="true"/>
</handlers>
</jetty>
</beans>


Then in the WEB-INF/web.xml for your web application, you simply include the MessageServlet:

  <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>MessageServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>


As you can see it's pretty easy from there to customize where you map your MessageServlet, choose your port, etc. You'll note also that ActiveMQ includes a bunch of other useful administrative webapps in the Jetty instance.