This example shows you how to send a message to an MDB and deliver it within a local transaction
The example will send deploy a simple MDB and demonstrate sending a message and the MDB consuming it, throwing an exception and the message being re delivered.
Please refer to JBoss Messaging Quickstart guide to install it in JBoss AS 5
To deploy and start the server, simply type ant deploy
from the example directory
To run the example, simply type ant
from the example directory
To remove the example profile, simply type ant undeploy
from the example directory
** make sure that JBOSS_HOME is set to the Jboss installation 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;
System.out.println("message " + text + " received");
if (!textMessage.getJMSRedelivered())
{
//Step 11. On first delivery get the transaction, take a look, and throw an exception
Transaction tx = tm.getTransaction();
if (tx != null)
{
System.out.println("something is wrong, there should be no global transaction: " + tx);
}
else
{
System.out.println("there is no global transaction, altho the messge delivery is using a local transaction");
System.out.println("lets throw an exception and see what happens");
throw new RuntimeException("DOH!");
}
}
System.out.println("The message was redelivered since the message delivery used a local transaction");
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();
}
}