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

8 comments:

Unknown said...

how can we do this without using the demo app? i mean if i add the servlet part ot activemq.xml and define a HTTP connector and then try to access it from a browser directly... will it work?

It did not work for me. Any idea where the issue is?

-andy

DBLWizard said...

Howdy,

It appears the links you in this blog are now broken. At least the one to the HTML page and the "fixed jar file" are. Can you update them?

Thanks

David

ritu said...

Sounds Interesting !!! can you Please update the Links

truth, love and a little malice said...

HI the links appear to be broken, can you repost or upload them somewhere else.

TIA

Unknown said...

hello all, did anybody here get lucky to produce the same scenario

Unknown said...

I am facing the same thing, can you please share your app.
or any reproducible example

Unknown said...

When ever I click on a link it is leading me to another irrelevant page. can anyone plz send me the proper links

Unknown said...

When ever I click on a link it is leading me to another irrelevant page. can anyone plz send me the proper links