If a JMS provider should fail, you can use message persistence to ensure that messages are not lost. Using the persistent delivery mode (the default) instructs the JMS provider to persist messages to a stable store (a database) as part of the client's send operation.
By default, OpenJMS writes to a file (openjms.db
which is found in the bin directory of the OpenJMS installation).
Configuring a database for message persistence requires the following steps:
- Add the JDBC driver to the CLASSPATH
- Edit the OpenJMS configuration file and specify connection properties
- Create the tables OpenJMS needs for persisting messages
Oracle8i, Sybase ASE, MySQL, and HSQLDB have been formally tested for message persistence. However, OpenJMS should work with any JDBC 2.0 compliant database.
I used IBM's DB2 Universal Database to test things out by first modifying the setenv.bat
batch file (located in the bin
directory of the OpenJMS installation) to point the CLASSPATH entry to the DB2 JDBC driver:
set CLASSPATH=C:\Program Files\IBM\SQLLIB\java\db2java.zip
You'll need to create the database before you can configure it. In DB2, issue the following command to the DB2 Command Line Processor:
db2 => create db openjms
Next, edit the OpenJMS configuration file (opensjms.xml
, located in the config
directory of the OpenJMS installation) and modify it to specify the DB2 database connection properties:
Depending on the database you are using, you might be able to create the tables which OpenJMS needs by using the db2tool
application (located in the OpenJMS installation's bin directory) which ships with OpenJMS. This is done with the command:
dbtool.bat -create -config
To create the tables for DB2, I had to issue the following command from the DB2 Command Window after changing to the config\db
of the OpenJMS installation directory and logging into DB2 as the user specified in the openjms.xml
db2 -tvf create_db2.sql
directory contains scripts for most popular databases.
The figure below shows the DB2 Control Center peeking into the openjms
database, in which the OpenJMS tables have been created.
|Figure 4: The OpenJMS tables created in DB2.|
After successfully configuring your persistence database with the necessary tables, OpenJMS will persist messages there, ensuring no message is lost during an OpenJMS provider failure.
This article has shown you some of the more advanced JMS topics, including message selectors, durable subscriptions, and message persistence. You can use message selectors to filter a client's consumption of messages. Durable subscriptions eliminate the need for a subscriber to be running when a message is published. Message persistence ensures that messages are not lost in case the JMS provider in your setup fails.