August 20, 2013

Configure ActiveMQ 5.8.0 to use MySQL as Persistence Mechanism

Introduction

In this blog I will show you how to change, the default persistence mechanism in Apache ActiveMQ 5.8.0 from KahaDB to instead use a RDBMS. And here I will use MySQL 5, but ActiveMQ supports most of the major vendor of RDBMS.

Install MySQL JDBC Driver

Download driver from maven central repo:
http://search.maven.org/#artifactdetails%7Cmysql%7Cmysql-connector-java%7C5.1.19%7Cjar.

Copy to $ACTIVEMQ_HOME/lib/optional/.

Create ActiveMQ database

Log into mysql and create ActiveMQ database.

$ mysql -u root -p
Enter password: 

mysql> CREATE SCHEMA `activemq` DEFAULT CHARACTER SET utf8 COLLATE utf8_swedish_ci;

Configure MySQL as persistence storage

Open the ActiveMQ configuration, $ACTIVEMQ_HOME/conf/activemq.xml, and add mysql datasource, comment/remove the default KahaDB and finally add the RDBMS persistence adapter.

<beans ...>
    ...
    </bean>

    <!-- MySql DataSource Sample Setup -->
    <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/activemq"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
        <property name="maxActive" value="200"/>
        <property name="poolPreparedStatements" value="true"/>
    </bean>

    <broker ...>
        ...

        <!-- Comment out the default storage adapter
        <persistenceAdapter>
            <kahaDB directory="${activemq.data}/kahadb"/>
        </persistenceAdapter>
        -->

        <persistenceAdapter>
            <jdbcPersistenceAdapter dataSource="#mysql-ds" />
        </persistenceAdapter> 

        ...
    </broker>
    ...
</beans>

Test

Now we are ready to test the installation. Restart activemq and check the activemq log for errors, $ACTIVEMQ_HOME/data/activemq.log.

You can also open the activemq web console, to verify that things are working:
URL: http://localhost:8161/admin/
Username: admin
Password: admin

Now lets really test the installation from the activemq web console you can send message. From the web console click Send and choose to send a text message to a new queue.

Afterwards we can check that the message is sent to the queue, by clicking on Queue and our new queue foo.bar. And there see your text message.

You could also verify that the message is really persistent in MySQL by querying your database.

$mysql> SELECT ID, CONTAINER, MSGID_PROD, MSGID_SEQ FROM activemq.ACTIVEMQ_MSGS;

No comments: