Package com.netflix.astyanax.recipes.queue

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


                .put("strategy_class", "SimpleStrategy")
                .build());

        final CountingQueueStats stats = new CountingQueueStats();

        final ShardedDistributedMessageQueue queue = new ShardedDistributedMessageQueue.Builder()
                .withColumnFamily(SCHEDULER_NAME_CF_NAME)
                .withQueueName("TestQueue")
                .withKeyspace(keyspace)
                .withConsistencyLevel(CONSISTENCY_LEVEL)
                .withStats(stats)
                .withTimeBuckets(2, 30, TimeUnit.SECONDS)
                .withShardCount(2)
                .withPollInterval(100L, TimeUnit.MILLISECONDS)
                .build();

        queue.createStorage();

    }
View Full Code Here


    // This tests for a known bug that has yet to be fixed
    public void testRepeatingMessage() throws Exception {
        final CountingQueueStats stats = new CountingQueueStats();

        // Create a simple queue
        final ShardedDistributedMessageQueue queue = new ShardedDistributedMessageQueue.Builder()
                .withColumnFamily(SCHEDULER_NAME_CF_NAME)
                .withQueueName("RepeatingMessageQueue" + qNameSfx)
                .withKeyspace(keyspace)
                .withConsistencyLevel(CONSISTENCY_LEVEL)
                .withStats(stats)
                .withShardCount(1)
                .withPollInterval(100L, TimeUnit.MILLISECONDS)
                .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);

View Full Code Here

    @Test
    public void testNoKeyQueue() throws Exception {
        final CountingQueueStats stats = new CountingQueueStats();

        final ShardedDistributedMessageQueue scheduler = new ShardedDistributedMessageQueue.Builder()
                .withColumnFamily(SCHEDULER_NAME_CF_NAME)
                .withQueueName("TestNoKeyQueue" + 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();

            // Add a message
View Full Code Here

    @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()
View Full Code Here

    @Test
    public void testQueueBusyLock() throws Exception {

        final CountingQueueStats stats = new CountingQueueStats();

        final ShardedDistributedMessageQueue scheduler = new ShardedDistributedMessageQueue.Builder()
                .withColumnFamily(SCHEDULER_NAME_CF_NAME)
                .withQueueName("TestQueueBusyLock" + qNameSfx)
                .withKeyspace(keyspace)
                .withConsistencyLevel(CONSISTENCY_LEVEL)
                .withStats(stats)
                .withShardCount(1)
                .withPollInterval(100L, TimeUnit.MILLISECONDS)
                .withShardLockManager(slm)
                .build();
        scheduler.deleteQueue();
        scheduler.createQueue();

        MessageProducer producer = scheduler.createProducer();

        // Add a batch of messages and peek
        List<Message> messages = Lists.newArrayList();

        for (int i = 0; i < 5; i++) {
            messages.add(new Message().addParameter("body", "" + i));
        }

        producer.sendMessages(messages);
        long queuedCount = scheduler.getMessageCount();
        final AtomicInteger count = new AtomicInteger();

        // Lock the shard. This should throw a few BusyLockExceptions
        String shard = scheduler.getShardStats().keySet().iterator().next();
        ShardLock l = null;
        if(slm!=null) {
            l = slm.acquireLock(shard);
        }
View Full Code Here

TOP

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

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.