Tuesday, December 18, 2007

ActiveMQ and REST

Here is a little example, how you can use ActiveMQ's REST interface with HTTP POST and GET methods.

Getting started...

Download ActiveMQ by following this link. Here we use ActiveMQ 5.0.0 Release.
You can start ActiveMQ by running activemq.bat (on windows) or ./activemq (on linux) in a shell. The start scripts are in the bin folder.


The logger output shows that the web server 'Jetty' is started with the web applications admin (ActiveMQ's Web Console) and demo.
To start the Web Console, type this url into your browser:

http://localhost:8161/admin

Now click on 'Queues'. You should see this:



You can use the demo web application to test the REST interface, or you can use my simple sample html-page, which can be downloaded here.
Open my sample page in a new browser. You should see this:



Click on 'POST' to send the message to a queue named 'myQueue' using ActiveMQ's REST interface. After that, look at the Web Console to see if the message arrived (refresh it):


As you can see, there's now a new queue 'myQueue' with one message in it, as expected.
Now go back to our sample page and click on 'GET' to consume the message. Your browser should look like this:



The queue 'myQueue' should have no message, you can check this with the Web Console.

NOTE: There is a bug in ActiveMQ. If you have more than 1 message in a queue, then every time the REST GET method is called on this queue, there are 2 messages consumed. I posted this as a bug AMQ-1480 and also the patch that fixes it some time ago, but it's not been committed yet.
If you can't apply the patch, you can download the fixed jar-file here. Just put it into 'path_to_activemq/lib/' and overwrite the old 'activemq-web-5.0.0.jar'


Customizing...

If you want to customize ActiveMQ (e.g. if you don't want to use the demo web application) you can do following:
  • Download the archive myApp.zip and extract it to 'path_to_activemq/webapps/' (compare the web.xml to the web.xml of the demo web application; theres only a change in the servlet-mapping)
  • Edit 'path_to_activemq/conf/activemq.xml' (jetty-tag):


  • (Re)start ActiveMQ
  • The new sample page (and with more features) can be found here