Package com.netflix.astyanax.recipes.queue

Examples of com.netflix.astyanax.recipes.queue.MessageConsumer


                .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);
        }
    }
View Full Code Here


        String key = "MyEvent";
        String key2 = "MyEvent2";

        MessageProducer producer = scheduler.createProducer();
        MessageConsumer consumer = scheduler.createConsumer();

        {
            final Message m = new Message();

            // Add a message
View Full Code Here

        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) {
                LOG.info(msg.getParameters().toString());
            }
View Full Code Here

TOP

Related Classes of com.netflix.astyanax.recipes.queue.MessageConsumer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.