Connection cnx = cf.createConnection();
Session prodSession = cnx.createSession(false, Session.AUTO_ACKNOWLEDGE);
Session consSession = cnx.createSession(true, 0);
MessageProducer producer = prodSession.createProducer(null);
MessageConsumer consumer = consSession.createConsumer(queue1);
cnx.start();
// Producing messages with a very short time to live: 20 ms.
System.out.println("Sends Message1 with a very short time to live");
TextMessage msg = prodSession.createTextMessage("Message1");
producer.send(queue1, msg, DeliveryMode.NON_PERSISTENT, Message.DEFAULT_PRIORITY, 20);
// Waiting for the message to be expired.
System.out.println("Waits for the message to be expired");
Thread.sleep(100);
msg = (TextMessage) consumer.receiveNoWait();
System.out.println("receives: " + msg);
// Producing "undeliverable" messages
System.out.println("Send Message2");
msg = prodSession.createTextMessage("Message2");
producer.send(queue1, msg);
msg = (TextMessage) consumer.receive();
System.out.println("Receives: " + msg.getText() + " then deny it!");
consSession.rollback();
msg = (TextMessage) consumer.receive();
System.out.println("Receives: " + msg.getText() + " then deny it!");
consSession.rollback();
// Producing "forbidden" messages
System.out.println("Send Message3");
msg = prodSession.createTextMessage("Message3");
try {
producer.send(queue2, msg);
} catch (JMSException exc) {
System.out.println(exc.getMessage());
}
// // Producing a message to a deleted destination