@Test
public void testQueue() throws Exception {
final CountingQueueStats stats = new CountingQueueStats();
final ShardedDistributedMessageQueue scheduler = new ShardedDistributedMessageQueue.Builder()
.withColumnFamily(SCHEDULER_NAME_CF_NAME)
.withQueueName("TestQueue" + qNameSfx)
.withKeyspace(keyspace)
.withConsistencyLevel(CONSISTENCY_LEVEL)
.withStats(stats)
.withShardCount(1)
.withPollInterval(100L, TimeUnit.MILLISECONDS)
.withShardLockManager(slm)
.build();
scheduler.createQueue();
String key = "MyEvent";
String key2 = "MyEvent2";
MessageProducer producer = scheduler.createProducer();
MessageConsumer consumer = scheduler.createConsumer();
{
final Message m = new Message().setKey(key);
// Add a message
LOG.info(m.toString());
String messageId = producer.sendMessage(m);
LOG.info("MessageId: " + messageId);
Assert.assertEquals(1, scheduler.getMessageCount());
// Read it by the messageId
final Message m1rm = scheduler.peekMessage(messageId);
LOG.info("m1rm: " + m1rm);
Assert.assertNotNull(m1rm);
// Read it by the key
final Message m1rk = scheduler.peekMessageByKey(key);
LOG.info("m1rk:" + m1rk);
Assert.assertNotNull(m1rk);
// Delete the message
scheduler.deleteMessageByKey(key);
// Read and verify that it is gone
final Message m1rkd = scheduler.peekMessageByKey(key);
Assert.assertNull(m1rkd);
// Read and verify that it is gone
final Message m1rmd = scheduler.peekMessage(messageId);
Assert.assertNull(m1rmd);
}
{
// Send another message
final Message m = new Message().setUniqueKey(key);
LOG.info("m2: " + m);
final String messageId2 = producer.sendMessage(m);
LOG.info("MessageId2: " + messageId2);
try {
final Message m2 = new Message().setUniqueKey(key);
producer.sendMessage(m2);
Assert.fail("Message should already exists");
} catch (MessageQueueException e) {
LOG.info("Failed to insert duplicate key", e);
}
try {
List<Message> messages = Lists.newArrayList(
new Message().setUniqueKey(key),
new Message().setUniqueKey(key2));
SendMessageResponse result = producer.sendMessages(messages);
Assert.assertEquals(1, result.getMessages().size());
Assert.assertEquals(1, result.getNotUnique().size());
} catch (MessageQueueException e) {
Assert.fail(e.getMessage());
}
Map<String, Integer> counts = scheduler.getShardCounts();
LOG.info(counts.toString());
Assert.assertEquals(2, scheduler.getMessageCount());
// Delete the message
scheduler.deleteMessageByKey(key2);
// Read the message
final Collection<MessageContext> lm2 = consumer.readMessages(10, 10, TimeUnit.SECONDS);
LOG.info("Read message: " + lm2);
Assert.assertEquals(1, lm2.size());
LOG.info(lm2.toString());
Assert.assertEquals(1, scheduler.getMessageCount());
consumer.ackMessages(lm2);
Assert.assertEquals(0, scheduler.getMessageCount());
}
{
final Message m = new Message()
.setKey("Key12345")
.setTrigger(new RepeatingTrigger.Builder()
.withInterval(3, TimeUnit.SECONDS)
.withRepeatCount(10)
.build());
final String messageId3 = producer.sendMessage(m);
Assert.assertNotNull(messageId3);
final Message m3rm = scheduler.peekMessage(messageId3);
Assert.assertNotNull(m3rm);
LOG.info(m3rm.toString());
Assert.assertEquals(1, scheduler.getMessageCount());
scheduler.deleteMessage(messageId3);
Assert.assertEquals(0, scheduler.getMessageCount());
}
// {
// final String repeatingKey = "RepeatingMessage";
// final Message m = new Message()