Wednesday, November 5, 2014

[Active-MQ] Setting up AMQ with MySQL database.



1) Create MySQL database.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
4 rows in set (0.00 sec)
mysql> create database activemq;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| activemq |
| mysql |
| performance_schema |
| phpmyadmin |
+--------------------+
5 rows in set (0.00 sec)
mysql> use activemq;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql>
view raw mysql hosted with ❤ by GitHub
2) Download activeMQ and extract it.
3) Edit /conf/activemq.xml
4) Add following configuration.

<beans ...>
<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?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<broker ...>
<!--
Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag).
For more information, see:
http://activemq.apache.org/persistence.html
-->
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/-->
<jdbcPersistenceAdapter dataSource="#mysql-ds"/>
</persistenceAdapter>
</broker>
</beans>
view raw activemq.xml hosted with ❤ by GitHub
5) Copy mysql jdbc driver (mysql-connector-java-5.1.25-bin.jar) in the directory "activemq_home/lib/optional"

6) Start ActiveMQ server using $ ./activemq start ( To stop the server use ./activemq stop )

7) Log in to activeMQ management console using http://localhost:8161/ with admin:admin credentials.

8) Create a queue and send a message to the queue with persistence enabled.

9) You can see the message in database.

mysql>
mysql> show tables;
+--------------------+
| Tables_in_activemq |
+--------------------+
| ACTIVEMQ_ACKS |
| ACTIVEMQ_LOCK |
| ACTIVEMQ_MSGS |
+--------------------+
3 rows in set (0.00 sec)
mysql> select * from ACTIVEMQ_MSGS;
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
| ID | CONTAINER | MSGID_PROD | MSGID_SEQ | EXPIRATION | MSG | PRIORITY | XID |
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
| 1 | queue://TestQueue | ID:hasitha-laptop-48562-1415139260206-3:1:1:1 | 1 | 0 | � { )ID:hasitha-laptop-48562-1415139260206-3:1  d TestQueue n{ )ID:hasitha-laptop-48562-1415139260206-3:1       I|��  
Hello world. I|�� | 0 | NULL |
+----+-------------------+-----------------------------------------------+-----------+------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+------+
1 row in set (0.00 sec)
mysql>


No comments:

Post a Comment