This example shows you how to send a message to an MDB configured to use Bean Managed Transactions
The example will send deploy a simple MDB and demonstrate sending a message and the MDB consuming it
The example leverages the JBoss Arquillian framework to run an AS 7 instance and deploy the MDB.
download AS 7.1.1.final from here and install.
set the JBOSS_HOME property to point to AS7 install directory
To run the example simply type mvn test
from the example directory
jndi.properties
file in the directory config
initialContext = new InitialContext();
Queue queue = (Queue) initialContext.lookup("/queue/testQueue");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage("This is a text message");
messageProducer.send(message);
TextMessage tm = (TextMessage)message;
String text = tm.getText(); System.out.println("message " + text + " received");
UserTransaction tx = ctx.getUserTransaction(); if(tx != null) { tx.begin(); System.out.println("we're in the middle of a transaction: " + tx); tx.commit(); }
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}