...
You can map a URI to the servlet and then use the relative part of the URI as the topic or queue name. e.g. you could HTTP POST to
Code Block |
http://www.acme.com/queue/orders/input
|
...
Similarly you could perform a HTTP DELETE GET on the above URL to read from the same queue. In this case we will map the MessageServlet from ActiveMQ to the URI
Code Block |
http://www.acme.com/queue
|
...
For example, you can use wget to consume messages, like this:
Code Block |
wget --user admin --password admin --save-cookies cookies.txt --load-cookies cookies.txt --keep-session-cookies http://localhost:8161/api/message/TEST1?type=queue
|
Also, if you plan to have multiple consumer using REST, it's advisable to set prefetch size to 1 so all consumers have an equal chance of getting the message. You can do that by passing a special parameter to the MessageServlet
Code Block |
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>org.apache.activemq.web.MessageServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>destinationOptions</param-name>
<param-value>consumer.prefetchSize=1</param-value>
</init-param>
</servlet>
|
...
Since 5.2.0 you can use clientId parameter to avoid storing actual JMS consumer in the request session. When using this approach, you don't need to keep sessions alive between requests, you just need to use the same clientId every time.
Code Block |
wget --user admin --password admin http://localhost:8161/api/message/test?type=queue&clientId=consumerA
|
...
As of ActiveMQ 5.4.0, you can use selectors when consuming using REST protocol. To do that, just specify the appropriate header with selector. To define a selector for the consumer, you have to provide it in an appropriate HTTP header. By default selector header name is selector, so the following example
Code Block |
wget --user admin --password admin --save-cookies cookies.txt --load-cookies cookies.txt --keep-session-cookies --header="selector: test=2" http://localhost:8161/api/message/test?type=queue
|
...
You can change the name of the selector header using the org.apache.activemq.selectorName Servlet context property in WEB-INF/web.xml, such as
Code Block |
<context-param>
<param-name>org.apache.activemq.selectorName</param-name>
<param-value>activemq-selector</param-value>
</context-param>
|
...
By default messages are sent to the consumers with text/xml content type. Your REST-based application may expect JSON response instead of XML one. In that case, you can configure the servlet to send responses back by adding something like this
Code Block |
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>org.apache.activemq.web.MessageServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>defaultContentType</param-name>
<param-value>application/json</param-value>
</init-param>
</servlet>
|
...
For more information on Jolokia protocol, see its reference manual. An API like this makes it easy to script monitoring and management operations agains against the broker., see also How can I monitor ActiveMQ?