This example shows you how to send a message to an MDB and then roll back the transaction forcing delivery of the message to a DLQ.
The example will send deploy a simple MDB and demonstrate sending a message, MDB consuming it, and then the standalone client consuming it from the DLQ and printing out the special DLQ properties "_HQ_ORIG_ADDRESS" and "_HQ_ORIG_QUEUE".
The example leverages the JBoss Arquillian framework to run a WildFly instance and deploy the MDB.
download WildFly 8.0.0.Final from here and install.
set the JBOSS_HOME property to point to the WildFly install directory
type mvn verify
from the example directory to run
jndi.properties
file in the directory config
final Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); initialContext = new InitialContext(env);
Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/jms/RemoteConnectionFactory");
connection = cf.createConnection("guest", "password");
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage("This is a text message");
producer.send(message);
TextMessage tm = (TextMessage)message;
String text = textMessage.getText(); System.out.println("message " + text + " received");
ctx.setRollbackOnly();
destination = (Destination) initialContext.lookup("jms/queues/dlq");
MessageConsumer consumer = session.createConsumer(destination); connection.start();
message = (TextMessage) consumer.receive(3000);
System.out.println("Original address: " + message.getStringProperty("_HQ_ORIG_ADDRESS")); System.out.println("Original queue: " + message.getStringProperty("_HQ_ORIG_QUEUE"));
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objectsfinally { if (initialContext != null) { initialContext.close(); } if (connection != null) { connection.close(); } }