Red Hat AMQ 6
As of February 2025, Red Hat is no longer supporting Red Hat AMQ 6. If you are using AMQ 6, please upgrade: Migrating to AMQ 7.Este conteúdo não está disponível no idioma selecionado.
6.5. Tutorial: JDBC Persistence
Overview Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
This tutorial provides complete instructions for installing a JDBC persistence layer into the JBoss A-MQ broker, using the MySQL database to store the broker's data. This example uses a plain JDBC persistence adapter and uses the default database schema.
This tutorial assumes you are using a standalone JBoss A-MQ container, which is the condition of the container immediately after the product is installed. It does not cover the case of a Fabric container.
Prerequisites Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
Before following the instructions for this tutorial, make sure that your system satisfies the following prerequisites:
- You have already installed a MySQL database server (following the instructions in the MySQL Installation Guide, including the post installation set-up and testing).
- The MySQL database server is already running.
- You have root access to the MySQL database server (that is, you have access to the
rootuser account in MySQL, which you can use to administer the database). - You have access to the Internet (so that you can install the MySQL JDBC driver bundle and the Apache Commons data source bundle, both of which must be downloaded from the Maven Central repository).
Steps to configure JDBC persistence with MySQL Copiar o linkLink copiado para a área de transferência!
Copiar o linkLink copiado para a área de transferência!
To configure a standalone JBoss A-MQ broker to use JDBC persistence with MySQL, perform the following steps:
- Log into the MySQL database using the
mysqlclient shell. Enter the following command to log on as therootuser:mysql -u root -p
mysql -u root -pCopy to Clipboard Copied! Toggle word wrap Toggle overflow You will be prompted to enter therootuser password (alternatively, if the root user has no associated password, you can omit the-poption). - Add the new user account,
amq, to MySQL with password,amqPass, by entering the following command at themysqlshell prompt:mysql> CREATE USER 'amq'@'localhost' IDENTIFIED BY 'amqPass';
mysql> CREATE USER 'amq'@'localhost' IDENTIFIED BY 'amqPass';Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you would rather create theamquser without any password, you can omit theIDENTIFIED BYclause, as follows:mysql> CREATE USER 'amq'@'localhost';
mysql> CREATE USER 'amq'@'localhost';Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteThis example assumes you are invoking themysqlshell from the same host where the MySQL database server is running. If you are logging on to the MySQL database remotely, however, you should replacelocalhostin the preceding command (and subsequent commands) by the name of the host where you are invoking themysqlshell. - Grant privileges to the
amquser, enabling it to access theactivemqdatabase instance (which has yet to be created). Enter the followingGRANTcommand at themysqlshell prompt:mysql> GRANT ALL PRIVILEGES ON activemq.* TO 'amq'@'localhost' WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON activemq.* TO 'amq'@'localhost' WITH GRANT OPTION;Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Create the
activemqdatabase instance, by entering the following command:mysql> CREATE DATABASE activemq;
mysql> CREATE DATABASE activemq;Copy to Clipboard Copied! Toggle word wrap Toggle overflow There is no need to create any database tables at this point. The broker's JDBC persistence will automatically create the necessary tables when it starts up for the first time. - Start the JBoss A-MQ standalone container, with its default (unchanged) configuration:
cd InstallDir/bin ./amq
cd InstallDir/bin ./amqCopy to Clipboard Copied! Toggle word wrap Toggle overflow - Install the MySQL JDBC driver into the container, as follows:
JBossA-MQ:karaf@root> osgi:install mvn:mysql/mysql-connector-java/5.1.27
JBossA-MQ:karaf@root> osgi:install mvn:mysql/mysql-connector-java/5.1.27Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Install the Apache Commons data source bundle, as follows:
JBossA-MQ:karaf@root> osgi:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.4_3
JBossA-MQ:karaf@root> osgi:install mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.commons-dbcp/1.4_3Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Stop the JBoss A-MQ container (for example, by entering the
shutdowncommand at the console). Now configure the broker to use JDBC persistence by editing theInstallDir/etc/activemq.xmlfile. Modify thebroker/persistenceAdapterelement and add a newbeanelement (for the MySQL data source) as follows:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Where the bean with the ID,mysql-ds, creates a data source instance for connecting to the MySQL database through the JDBC protocol. Note particularly the following property settings for this bean:- url
- Is used to specify a JDBC URL in the following format:
jdbc:mysql://Hostame/DBName[?Property=Value]
jdbc:mysql://Hostame/DBName[?Property=Value]Copy to Clipboard Copied! Toggle word wrap Toggle overflow WhereHostnameis the host where the MySQL database server is running;DBNameis the name of the database instance used to store the broker data (which isactivemq, in this example); and you can optionally set property values,Property=Value, after the?character. - password
- If you specified a password for the
amquser when you created it, specify the password here. Otherwise, if no password was defined, specify a blank string,"".
- Restart the JBoss A-MQ container, as follows:
./amq
./amqCopy to Clipboard Copied! Toggle word wrap Toggle overflow As the broker initializes, it automatically creates new tables in theactivemqdatabase instance to hold the broker data (this is the default behavior). - To verify that the requisite tables have been created in the
activemqdatabase instance, enter the following commands at themysqlclient shell:Copy to Clipboard Copied! Toggle word wrap Toggle overflow