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()
// .setKey(repeatingKey)
// .setKeepHistory(true)
// .setTaskClass(HelloWorldFunction.class.getCanonicalName())
// .setTrigger(new RepeatingTrigger.Builder()
// .withInterval(3, TimeUnit.SECONDS)
// .withRepeatCount(5)
// .build());
// final String messageId = producer.sendMessage(m);
//
// final AtomicLong counter = new AtomicLong(0);
//
// MessageQueueDispatcher dispatcher = new MessageQueueDispatcher.Builder()
// .withBatchSize(5)
// .withCallback(new Function<MessageContext, Boolean>() {
// long startTime = 0;
//
// @Override
// public synchronized Boolean apply(MessageContext message) {
// if (startTime == 0)
// startTime = System.currentTimeMillis();
//
// LOG.info("Callback : " + (System.currentTimeMillis() - startTime) + " " + message);
// counter.incrementAndGet();
// return true;
// }
// })
// .withMessageQueue(scheduler)
// .withThreadCount(2)
// .build();
//
// dispatcher.start();
//
// Thread.sleep(TimeUnit.MILLISECONDS.convert(20, TimeUnit.SECONDS));
//
// Collection<MessageHistory> history = scheduler.getKeyHistory(repeatingKey, null, null, 10);
// LOG.info(history);
//
// dispatcher.stop();
//
// Assert.assertEquals(5, counter.get());
// }
// Add a batch of messages and peek
{
List<Message> messages = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
messages.add(new Message().addParameter("body", "" + i));
}
producer.sendMessages(messages);
Collection<Message> all = consumer.peekMessages(Integer.MAX_VALUE);
Assert.assertEquals(10, all.size());
for (Message msg : all) {