Active MQ with Message Persistence (MySQL DB)

Communication between Producer and Consumer using Active MQ also allows to persist the data in the database. This post will explain how MySQL Database can be used for message persistence with ActiveMQ. By default Active MQ supports the message persistence through KahaDB which is a file based persistence of data and is explained in another blog post.

PERSISTING QUEUE MESSAGES USING MYSQL DB

Configuration Changes in activemq.xml

A. Add the following outside of the <broker>….</broker> tag

<bean id=”mysql-ds” class=”org.apache.commons.dbcp2.BasicDataSource” destroy-method=”close”>

<property name=”driverClassName” value=”com.mysql.jdbc.Driver“/>

<property name=”url” value=”jdbc:mysql://localhost:3306/activemq?relaxAutoCommit=true”/>

<property name=”username” value=”****”/>

<property name=”password” value=”****”/>

<property name=”poolPreparedStatements” value=”true”/>

</bean>

where 1. activemq: refers to the database that you will have to create.

2. com.mysql.jdbc.Driver is the driver obtained from the jar file: mysql-connector-java-5.1.39-bin.jar. Do note this jar file should be kept at the location: apache-activemq-5.13.4/lib/optional/

3. User name and password are the credentials for accessing the mysql database.

B. Add the following inside of the <broker>….</broker> tag

<persistenceAdapter>

<jdbcPersistenceAdapter dataDirectory=”${activemq.base}/data” dataSource=”#mysql-ds”/>

</persistenceAdapter>

C. Set class path as follows:

1) export AMQ_INSTALL = path to apache-activemq-x.xx.x

2) export CLASSPATH = $AMQ_INSTALL/lib

D. Send messages from Producer to the Queue:

Try to run the consumer, the activemq database will now have three tables:-

ACTIVEMQ_MSGS

ACTIVEMQ_LOCK

ACTIVEMQ_ACKS

where: ACTIVEMQ_MSGS table stores the messages under the column called MSGS along with meta data information.

Note:

If there are two queues (network of brokers) and 2nd queue has to persist the messages in the database then the above steps should be done in the activemq.xml configuration of 2nd queue and then start both the queues along with producer and consumer respectively.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s