Retrive embedded ActiveMQ messages
How can you get messages out of an embedded ActiveMQ kaha db?
Well this question came up this week in conversation at work after my colleagues had spent a fair few hours trying to achieve this goal. I always prefer the simplest option possible and thought, well why not just copy the kaha db into a fresh ActiveMQ installation and boot up the admin console? Unable to give this a go on Friday, I’ve just found the time to try it.
First of all I need a message on an embedded broker kaha db and since I use Mule a lot, this little demo will use Mule to put a fake message on a queue.
First we need a mule config file to take an inbound vm message and place it on an ActiveMQ queue.
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.3/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/3.3/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/3.3/mule-vm.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/3.3/mule-jms.xsd">
<jms:activemq-connector name="jmsConnector"
brokerURL="vm://localhost?broker.persistent=true&create=true"
persistentDelivery="true" />
<vm:connector name="abc" />
<flow name="dropMessageOnAMQ">
<vm:inbound-endpoint address="vm://amq" exchange-pattern="one-way" connector-ref="abc" />
<jms:outbound-endpoint queue="testQ" connector-ref="jmsConnector" exchange-pattern="one-way" />
</flow>
</mule>
Next a very simple test class which puts a message on the vm
package uk.co.vsf.utilities;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.module.client.MuleClient;
import org.mule.tck.FunctionalTestCase;
public class ActiveMQQueueLoaderMule extends FunctionalTestCase
{
public void testAddMsgToQ() throws Exception
{
MuleClient client = new MuleClient(muleContext);
MuleMessage message = client.send("vm://amq?connector=abc", "hello me", null);
Thread.sleep(1000);
}
@Override
protected String getConfigResources()
{
return "mule-cfg.xml";
}
}
Run the test and a kaha db file will be created.

Copy the directory KahaDB into a fresh ActiveMQ installation, e.g.

Boot up ActiveMQ

Go into the Queues tab

Go into the message(s) in question and you’ll be able to see the data and use it if necessary

Please note this is not recommended for production use – it’s here for reference
The above does rely on persistence being specified otherwise the messages would be cleared when the application stops.
Where possible, a standalone ActiveMQ installation should be used separate from your application so you can access the queues, move messages around and have full fail over. Embedded is great for testing things during development but has limitations.
Please enable the Disqus feature in order to add comments