*/
public void doTestAsynchRecover() throws JMSException, InterruptedException {
final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
final String errorMessage[] = new String[]{null};
final Latch doneLatch = new Latch();
MessageConsumer consumer = session.createConsumer(dest);
MessageProducer producer = session.createProducer(dest);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.send(session.createTextMessage("First"));
producer.send(session.createTextMessage("Second"));
consumer.setMessageListener(new MessageListener(){
int counter;
public void onMessage(Message msg) {
counter++;
try {
TextMessage message = (TextMessage)msg;
switch( counter ) {
case 1:
assertEquals("First", message.getText());
assertFalse(message.getJMSRedelivered());
message.acknowledge();
break;
case 2:
assertEquals("Second", message.getText());
assertFalse(message.getJMSRedelivered());
session.recover();
break;
case 3:
assertEquals("Second", message.getText());
assertTrue(message.getJMSRedelivered());
message.acknowledge();
doneLatch.release();
break;
default:
errorMessage[0]="Got too many messages: "+counter;
doneLatch.release();
}
} catch (Throwable e) {
e.printStackTrace();
errorMessage[0]="Got exception: "+e;
doneLatch.release();
}
}
});
connection.start();
if( doneLatch.attempt(1000*5) ) {
if( errorMessage[0]!=null ) {
fail(errorMessage[0]);
}
} else {
fail("Timeout waiting for async message delivery to complete.");