09 March 2015

ActiveMQ, transactions and several consumers

Bambitroll @ 16:57
Here is a lesson learnt regarding using ActiveMQ with several consumers for one queue and transactions enabled.

What we wanted to achieve was to have the following setup for each queue:
  • several consumers to spread the load and avoid that one message gets stuck if a route gets into trouble or takes a long time to complete
  • each consumer should get only one message at a time so no message gets stuck in the local client buffer
  • the camel route should be transacted so if something goes wrong the message is not lost but gets delivered to another consumer
We are using camel and the camel-activemq component.
So defining several consumers is rather easy.
We used prefetch=1 to make sure that only one message at a time is delivered to a route .
And we used a transaction manager for our JmsConfiguration bean.
So my config looked like something like that

<bean id="jmsConfig" class="org.apache.camel.component.jms.JmsConfiguration">
        <property name="connectionFactory" ref="pooledConnectionFactory" />
        <property name="transacted" value="true" /> 
        <property name="transactionManager" ref="jmsTransactionManager" />
        <property name="cacheLevelName" value="CACHE_CONSUMER" />
</bean>

So far nothing special.

What I then noticed was that sometimes a consumer could get 2 messages assigned, which puzzled me since I specified prefetch=1!
One being processed and waiting for an ACK to finish the transaction, and the other one just waiting to be taken and not being taken by another consumer even if one is available.
And this was bad because sometimes my route took several minutes to completed, effectively preventing the 2nd message to get through right away.

The culprit was CACHE_CONSUMER. This pre-assigns the next message to the consumer to speed things up.

So I changed this to CACHE_NONE and now everything behaves as expected!

27 February 2015

Run Hawtio locally and connect to Jolokia/ActiveMQ 5.9.1 and above remotely

Bambitroll @ 15:33

Get the Hawtio app from http://hawt.io
Start it like this:

java -jar -Dhawtio.offline=true -Dhawtio.proxyWhitelist=* hawtio-app-2.0.2.jar

Then go to http://localhost:8080/hawtio (actually the tab should open by itself in your default browser after you ran the command above)




Choose Connect->Remote, then Add Connection and use port 8161 and path /api/jolokia




And voila :)


Remember to adjust 2 settings by clicking on the little user icon in the top right corner (in Preferences):
- the ActiveMQ username/password in order to send/consume messages
- the max collection size for Jolokia in order to get all the objects back from JMX

From ActiveMQ 5.9.1 and up, the jolokia agent is installed by default so you should not need to do anything special with your ActiveMQ installation.

29 January 2015

Installing Hawtio in ActiveMQ 5.9.1

Bambitroll @ 11:07

Hawtio is not included by default in ActiveMQ 5.9.1 as it was in 5.9.0.

But by following the instructions from these 2 pages, it is fairly simple to add it manually:
http://sensatic.net/activemq/activemq-and-hawtio.html
http://activemq.2283324.n4.nabble.com/Hawto-log-in-td4673552.html

The main info from the second page is this missing info from the first one:
-Djava.security.auth.login.config=$ACTIVEMQ_CONF/login.config

Also make sure that you update the usernames/password in the following files in $ACTIVEMQ_HOME/conf:
- credentials.properties
- users.properties
- groups.properties
- login.config

Make sure you also add this to $ACTIVEMQ_HOME/bin/activemq, somewhere around line 160:
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS -Dhawtio.realm=activemq -Dhawtio.role=admins -Dhawtio.rolePrincipalClasses=org.apache.activemq.jaas.GroupPrincipal -Djava.security.auth.login.config=$ACTIVEMQ_CONF/login.config"
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS -Dhawtio.offline=true -Dhawtio.config.cloneOnStartup=false -Dhawtio.config.pullOnStartup=false"


The second set of sysetm properties is to prevent hawtio to try to fetch data from github which is does by default. If you are behind a firewall, this can cause hawtio to hang for over 2 minutes.

2 more things:
- if you have some special proxy settings, you might want to use 127.0.1.1 instead of 127.0.0.1 to reach your local hawtio
- the hawtio tree structure on the left side gets all screwed up each time a refresh occurs. I am not sure if this is a bug in hawtio but it is very annoying.

More links:
http://hawt.io/getstarted/
http://hawt.io/configuration/index.html#Configuration_Properties


Good luck!