Session producerSess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer producer = producerSess.createProducer(queue);
Session consumerSess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageConsumer consumer = consumerSess.createConsumer(queue);
conn.start();
final int NUM_MESSAGES = 20;
final int ACKED_MESSAGES = 11;
//Send some messages
for (int i = 0; i < NUM_MESSAGES; i++)
{
Message m = producerSess.createMessage();
producer.send(m);
}
assertRemainingMessages(NUM_MESSAGES);
log.trace("Sent messages");
int count = 0;
Message m = null;
for (int i = 0; i < NUM_MESSAGES; i++)
{
m = consumer.receive(200);
if (m == null)
{
break;
}
if (count == ACKED_MESSAGES -1)
{
m.acknowledge();
}
count++;
}
assertRemainingMessages(NUM_MESSAGES - ACKED_MESSAGES);
assertNotNull(m);
log.trace("Received " + count + " messages");
assertEquals(count, NUM_MESSAGES);
consumerSess.recover();
log.trace("Session recover called");
count = 0;
while (true)
{
m = consumer.receive(200);
if (m == null) break;
count++;
}
assertEquals(NUM_MESSAGES - ACKED_MESSAGES, count);