.withShardLockManager(slm)
.build();
queue.createQueue();
MessageProducer producer = queue.createProducer();
MessageConsumer consumer = queue.createConsumer();
// Enqueue a recurring message
final String key = "RepeatingMessageWithTimeout";
final Message message = new Message()
.setUniqueKey(key)
.setTimeout(1, TimeUnit.SECONDS)
.setTrigger(new RepeatingTrigger.Builder().withInterval(1, TimeUnit.SECONDS).build());
producer.sendMessage(message);
// Make sure it's unique by trying to submit again
try {
producer.sendMessage(message);
Assert.fail();
} catch (KeyExistsException e) {
LOG.info("Key already exists");
}
// Confirm that the message is there
Assert.assertEquals(1, queue.getMessageCount());
printMessages("Pending messages after insert ORIG message", queue.peekMessagesByKey(key));
// Consume the message
LOG.info("*** Reading first message ***");
final List<MessageContext> m1 = consumer.readMessages(10);
printMessages("Consuming the ORIG message", m1);
Assert.assertEquals(1, m1.size());
printMessages("Pending messages after consume ORIG " + key, queue.peekMessagesByKey(key));
// Exceed the timeout
Thread.sleep(2000);
// Consume the timeout event
LOG.info("*** Reading timeout message ***");
final List<MessageContext> m2 = consumer.readMessages(10);
printMessages("Consuming the TIMEOUT message", m2);
Assert.assertEquals(1, m2.size());
printMessages("Pending messages after consume TIMEOUT " + key, queue.peekMessagesByKey(key));
// Assert.assertEquals(2, m2a.size());
LOG.info("*** Acking both messages ***");
consumer.ackMessages(m1);
consumer.ackMessages(m2);
printMessages("Pending messages after both acks " + key, queue.peekMessagesByKey(key));
// Assert.assertEquals(2, m2a.size());
// Consume anything that is in the queue
final List<MessageContext> m3 = consumer.readMessages(10);
printMessages("Consuming messages", m3);
Assert.assertEquals(1, m3.size());
printMessages("Pending messages after 2nd consume " + key, queue.peekMessagesByKey(key));
consumer.ackMessages(m3);
Thread.sleep(2000);
final List<MessageContext> m4 = consumer.readMessages(10);
printMessages("Consuming messages", m4);
Assert.assertEquals(1, m4.size());
// There should be only one message
// Assert.assertEquals(1, queue.getMessageCount());
for (int i = 0; i < 10; i++) {
final List<MessageContext> m5 = consumer.readMessages(10);
Assert.assertEquals(1, m5.size());
long systemtime = System.currentTimeMillis();
MessageContext m = Iterables.getFirst(m5, null);
LOG.info("MessageTime: " + (systemtime - m.getMessage().getTrigger().getTriggerTime()));
consumer.ackMessages(m5);
}
}