try
{
queueConnection.start();
drainQueue();
QueueSession session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue) context.lookup(TEST_QUEUE);
QueueSender sender = session.createSender(queue);
QueueReceiver receiver = session.createReceiver(queue);
// Send a message that expires in 5 seconds
TextMessage message = session.createTextMessage("5 Second Expiration");
sender.send(message, DeliveryMode.PERSISTENT, 4, 5*1000);
// Send a message that has not expired
message = session.createTextMessage("OK");
sender.send(message);
// Sleep 6 seconds
Thread.sleep(6*1000);
// Try to receive the OK message
message = (TextMessage) receiver.receiveNoWait();
assertEquals("OK", message.getText());
// Should be no more
assertTrue("Didn't expect anymore messages", receiver.receiveNoWait() == null);
// Send a message that expires in 10 seconds
message = session.createTextMessage("10 Second Expiration");
sender.send(message, DeliveryMode.PERSISTENT, 4, 10*1000);
// Send a message that has not expired
message = session.createTextMessage("OK");
sender.send(message);
// Sleep 1 seconds
Thread.sleep(1*1000);
// Try to receive the messages
message = (TextMessage) receiver.receiveNoWait();
assertEquals("10 Second Expiration", message.getText());
message = (TextMessage) receiver.receiveNoWait();
assertEquals("OK", message.getText());
// Should be no more
assertTrue("Didn't expect anymore messages", receiver.receiveNoWait() == null);
// Test that JMSExpiration has no affect
message = session.createTextMessage("5 Second Expiration");
message.setJMSExpiration(System.currentTimeMillis() + 5*1000);
sender.send(message, DeliveryMode.PERSISTENT, 4, 0);
// Send a message that has not expired
message = session.createTextMessage("OK");
sender.send(message);
// Sleep 6 seconds
Thread.sleep(6*1000);
// Try to receive the OK message
message = (TextMessage) receiver.receiveNoWait();