Example usage for com.rabbitmq.client Channel basicConsume

List of usage examples for com.rabbitmq.client Channel basicConsume

Introduction

In this page you can find the example usage for com.rabbitmq.client Channel basicConsume.

Prototype

String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;

Source Link

Document

Start a non-nolocal, non-exclusive consumer, with a server-generated consumerTag.

Usage

From source file:org.apache.druid.firehose.rabbitmq.RabbitMQFirehoseFactory.java

License:Apache License

@Override
public Firehose connect(final InputRowParser<ByteBuffer> firehoseParser, File temporaryDirectory)
        throws IOException {
    ConnectionOptions lyraOptions = new ConnectionOptions(this.connectionFactory);
    Config lyraConfig = new Config().withRecoveryPolicy(new RetryPolicy().withMaxRetries(config.getMaxRetries())
            .withRetryInterval(Duration.seconds(config.getRetryIntervalSeconds()))
            .withMaxDuration(Duration.seconds(config.getMaxDurationSeconds())));

    String queue = config.getQueue();
    String exchange = config.getExchange();
    String routingKey = config.getRoutingKey();

    boolean durable = config.isDurable();
    boolean exclusive = config.isExclusive();
    boolean autoDelete = config.isAutoDelete();

    final Connection connection = Connections.create(lyraOptions, lyraConfig);

    connection.addShutdownListener(new ShutdownListener() {
        @Override//  w ww  . j a v a2s.  co  m
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Connection closed!");
        }
    });

    final Channel channel = connection.createChannel();
    channel.queueDeclare(queue, durable, exclusive, autoDelete, null);
    channel.queueBind(queue, exchange, routingKey);
    channel.addShutdownListener(new ShutdownListener() {
        @Override
        public void shutdownCompleted(ShutdownSignalException cause) {
            log.warn(cause, "Channel closed!");
        }
    });

    // We create a QueueingConsumer that will not auto-acknowledge messages since that
    // happens on commit().
    final QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);

    return new Firehose() {
        /**
         * Storing the latest row as a member variable should be safe since this will only be run
         * by a single thread.
         */
        private InputRow nextRow;

        /**
         * Store the latest delivery tag to be able to commit (acknowledge) the message delivery up to
         * and including this tag. See commit() for more detail.
         */
        private long lastDeliveryTag;

        private Iterator<InputRow> nextIterator = Collections.emptyIterator();

        @Override
        public boolean hasMore() {
            nextRow = null;
            try {
                if (nextIterator.hasNext()) {
                    nextRow = nextIterator.next();
                    return true;
                }
                // Wait for the next delivery. This will block until something is available.
                final Delivery delivery = consumer.nextDelivery();
                if (delivery != null) {
                    lastDeliveryTag = delivery.getEnvelope().getDeliveryTag();
                    nextIterator = firehoseParser.parseBatch(ByteBuffer.wrap(delivery.getBody())).iterator();
                    if (nextIterator.hasNext()) {
                        nextRow = nextIterator.next();
                        // If delivery is non-null, we report that there is something more to process.
                        return true;
                    }
                }
            } catch (InterruptedException e) {
                // A little unclear on how we should handle this.

                // At any rate, we're in an unknown state now so let's log something and return false.
                log.wtf(e, "Got interrupted while waiting for next delivery. Doubt this should ever happen.");
            }

            // This means that delivery is null or we caught the exception above so we report that we have
            // nothing more to process.
            return false;
        }

        @Nullable
        @Override
        public InputRow nextRow() {
            if (nextRow == null) {
                //Just making sure.
                log.wtf("I have nothing in delivery. Method hasMore() should have returned false.");
                return null;
            }

            return nextRow;
        }

        @Override
        public Runnable commit() {
            // This method will be called from the same thread that calls the other methods of
            // this Firehose. However, the returned Runnable will be called by a different thread.
            //
            // It should be (thread) safe to copy the lastDeliveryTag like we do below and then
            // acknowledge values up to and including that value.
            return new Runnable() {
                // Store (copy) the last delivery tag to "become" thread safe.
                final long deliveryTag = lastDeliveryTag;

                @Override
                public void run() {
                    try {
                        log.info("Acknowledging delivery of messages up to tag: " + deliveryTag);

                        // Acknowledge all messages up to and including the stored delivery tag.
                        channel.basicAck(deliveryTag, true);
                    } catch (IOException e) {
                        log.error(e, "Unable to acknowledge message reception to message queue.");
                    }
                }
            };
        }

        @Override
        public void close() throws IOException {
            log.info("Closing connection to RabbitMQ");
            channel.close();
            connection.close();
        }
    };
}

From source file:org.apache.flume.amqp.AmqpConsumer.java

License:Apache License

@VisibleForTesting
protected void processDeliveriesForConsumer(Thread currentThread, QueueingConsumer consumer)
        throws InterruptedException, ChannelException, IOException {
    Channel channel = consumer.getChannel();
    String consumerTag = channel.basicConsume(queueName, autoAck, consumer);
    LOG.info("Starting new consumer. Server generated {} as consumerTag", consumerTag);

    List<QueueingConsumer.Delivery> batch = new ArrayList<QueueingConsumer.Delivery>(batchSize);

    while (!currentThread.isInterrupted()) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(DELIVERY_TIMEOUT);

        if (delivery == null && batch.size() > 0) {
            deliverBatch(channel, batch);

            batch.clear();/*w w  w .ja  v  a2  s.c  o m*/

        } else if (delivery != null) {
            batch.add(delivery);

            if (batch.size() == batchSize) {
                deliverBatch(channel, batch);

                batch.clear();
            }
        }
    }
}

From source file:org.apache.helix.recipes.rabbitmq.ConsumerThread.java

License:Apache License

@Override
public void run() {
    Connection connection = null;
    try {/*  www  . j  av a 2 s.  c om*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(_mqServer);
        connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        String queueName = channel.queueDeclare().getQueue();

        String bindingKey = _partition.toString();
        channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

        System.out.println(
                " [*] " + _consumerId + " Waiting for messages on " + bindingKey + ". To exit press CTRL+C");

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, consumer);

        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            String routingKey = delivery.getEnvelope().getRoutingKey();

            System.out.println(" [x] " + _consumerId + " Received '" + routingKey + "':'" + message + "'");
        }
    } catch (InterruptedException e) {
        System.err.println(" [-] " + _consumerId + " on " + _partition + " is interrupted ...");
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

From source file:org.apache.niolex.rabbit.rpc.RPCServer.java

License:Apache License

public static void main(String[] argv) {
    Connection connection = null;
    Channel channel = null;
    try {/*  www  . j  a  v  a 2s .c o m*/
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        connection = factory.newConnection();
        channel = connection.createChannel();

        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null);

        channel.basicQos(1);

        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(RPC_QUEUE_NAME, false, consumer);

        System.out.println(" [x] Awaiting RPC requests");

        while (true) {
            String response = null;

            QueueingConsumer.Delivery delivery = consumer.nextDelivery();

            BasicProperties props = delivery.getProperties();
            BasicProperties replyProps = new BasicProperties.Builder().correlationId(props.getCorrelationId())
                    .build();

            try {
                String message = new String(delivery.getBody(), "UTF-8");
                int n = Integer.parseInt(message);

                System.out.println(" [.] fib(" + message + ")");
                response = "" + fib(n);
            } catch (Exception e) {
                System.out.println(" [.] " + e.toString());
                response = "";
            } finally {
                channel.basicPublish("", props.getReplyTo(), replyProps, response.getBytes("UTF-8"));
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception ignore) {
            }
        }
    }
}

From source file:org.apache.synapse.tranport.amqp.AMQPTwoWayProducerClient.java

License:Apache License

private static void produceAndConsume(String message, Channel channel, String requestQueueName,
        String replyQueueName) throws IOException, InterruptedException {

    AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties().builder();
    String restCoID = Math.random() + "";
    builder.correlationId(restCoID);/*  w  ww.  j a va2 s  .co  m*/
    System.out.println("Request correlation Id : " + restCoID);
    builder.replyTo(replyQueueName);
    channel.basicPublish("", requestQueueName, builder.build(), message.getBytes());
    System.out.println("[x] sent to '" + requestQueueName + "' the message '\n" + message + "'");

    channel.queueDeclare(replyQueueName, false, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(replyQueueName, true, consumer);
    System.out.println("Waiting for message on queue '" + replyQueueName + "'");
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String replyMessage = new String(delivery.getBody());
        System.out.println("[x] received '" + replyMessage + "'");
        System.out.println("Response correlation Id : " + delivery.getProperties().getCorrelationId());
        break;

    }
}

From source file:org.ballerinalang.messaging.rabbitmq.nativeimpl.channel.listener.Start.java

License:Open Source License

/**
 * Receive messages from the RabbitMQ server.
 *
 * @param resource  Ballerina resource function.
 * @param channel   RabbitMQ Channel object.
 * @param queueName Name of the queue messages are consumed from.
 * @param autoAck   True if the server should consider messages acknowledged once delivered;
 *                  false if the server should expect explicit acknowledgements.
 *///from  www .  j  av  a2s . com
private void receiveMessages(Resource resource, Channel channel, String queueName, boolean autoAck) {
    try {
        channel.basicConsume(queueName, autoAck, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                dispatchMessage(resource, body, channel, envelope.getDeliveryTag(), autoAck);
            }
        });
    } catch (IOException exception) {
        throw new BallerinaException(exception);
    }
}

From source file:org.eclipse.ditto.services.connectivity.messaging.rabbitmq.RabbitMQClientActor.java

License:Open Source License

private void startConsumers(final Channel channel) {
    getSourcesOrEmptyList().forEach(source -> source.getAddresses().forEach(sourceAddress -> {
        for (int i = 0; i < source.getConsumerCount(); i++) {
            final String addressWithIndex = sourceAddress + "-" + i;
            final AuthorizationContext authorizationContext = source.getAuthorizationContext();
            final Enforcement enforcement = source.getEnforcement().orElse(null);
            final HeaderMapping headerMapping = source.getHeaderMapping().orElse(null);
            final ActorRef consumer = startChildActorConflictFree(CONSUMER_ACTOR_PREFIX + addressWithIndex,
                    RabbitMQConsumerActor.props(sourceAddress, getMessageMappingProcessorActor(),
                            authorizationContext, enforcement, headerMapping, connectionId()));
            consumerByAddressWithIndex.put(addressWithIndex, consumer);
            try {
                final String consumerTag = channel.basicConsume(sourceAddress, false,
                        new RabbitMQMessageConsumer(consumer, channel, sourceAddress));
                log.debug("Consuming queue <{}>, consumer tag is <{}>.", addressWithIndex, consumerTag);
                consumedTagsToAddresses.put(consumerTag, addressWithIndex);
            } catch (final IOException e) {
                connectionLogger.failure("Failed to consume queue {0}: {1}", addressWithIndex, e.getMessage());
                log.warning("Failed to consume queue <{}>: <{}>", addressWithIndex, e.getMessage());
            }/*from   ww w .  ja  v  a 2 s  .co m*/
        }
    }));
}

From source file:org.graylog2.messagehandlers.amqp.AMQPSubscriberThread.java

License:Open Source License

/**
 * Run the thread. Runs forever!/*from w w w.j a v a2s  .co m*/
 */
@Override
public void run() {
    while (true) {
        Connection connection = null;
        Channel channel = null;
        QueueingConsumer consumer = new QueueingConsumer(channel);

        try {
            connection = broker.getConnection();
            channel = connection.createChannel();
            channel.basicConsume(this.queue.getName(), false, consumer);

            LOG.info("Successfully connected to queue '" + this.queue.getName() + "'");
        } catch (Exception e) {
            LOG.error("AMQP queue '" + this.queue.getName()
                    + "': Could not connect to AMQP broker or channel (Make sure that "
                    + "the queue exists. Retrying in " + SLEEP_INTERVAL + " seconds. (" + e.getMessage() + ")");

            // Retry after waiting for SLEEP_INTERVAL seconds.
            try {
                Thread.sleep(SLEEP_INTERVAL * 1000);
            } catch (InterruptedException foo) {
            }
            continue;
        }

        while (true) {
            try {
                QueueingConsumer.Delivery delivery;
                try {
                    delivery = consumer.nextDelivery();
                } catch (InterruptedException ie) {
                    continue;
                }

                // Handle the message. (Store in MongoDB etc)
                try {
                    handleMessage(delivery.getBody());
                } catch (Exception e) {
                    LOG.error("Could not handle AMQP message: " + e.toString());
                }

                try {
                    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                } catch (IOException e) {
                    LOG.error("Could not ack AMQP message: " + e.toString());
                }
            } catch (Exception e) {
                // Error while receiving. i.e. when AMQP broker breaks down.
                LOG.error("AMQP queue '" + this.queue.getName()
                        + "': Error while subscribed (rebuilding connection " + "in " + SLEEP_INTERVAL
                        + " seconds. (" + e.getMessage() + ")");

                // Better close connection stuff it is still active.
                try {
                    channel.close();
                    connection.close();
                } catch (IOException ex) {
                    // I don't care.
                } catch (AlreadyClosedException ex) {
                    // I don't care.
                }

                // Retry after waiting for SLEEP_INTERVAL seconds.
                try {
                    Thread.sleep(SLEEP_INTERVAL * 1000);
                } catch (InterruptedException foo) {
                }
                break;
            }
        }
    }
}

From source file:org.hobbit.core.rabbit.EchoServer.java

License:Open Source License

@Override
public void run() {
    running = true;//from  w  w  w  .j  a v  a2  s  .c  om
    Connection connection = null;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(rabbitHost);
        connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare(queueName, false, false, true, null);

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                BasicProperties replyProps = new BasicProperties.Builder()
                        .correlationId(properties.getCorrelationId()).deliveryMode(2).build();
                channel.basicPublish("", properties.getReplyTo(), replyProps, body);
            }
        };
        channel.basicConsume(queueName, true, consumer);

        while (running) {
            Thread.sleep(3000);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:org.mazerunner.core.messaging.Worker.java

License:Apache License

public void doMain(String[] args) throws Exception {

    CmdLineParser parser = new CmdLineParser(this);

    // if you have a wider console, you could increase the value;
    // here 80 is also the default
    parser.setUsageWidth(80);//  w w  w . j a  v a  2  s.  c om

    try {
        // parse the arguments.
        parser.parseArgument(args);

        if (sparkMaster == "" || hadoopHdfs == "")
            throw new CmdLineException(parser, "Options required: --hadoop.hdfs <url>, --spark.master <url>");

        ConfigurationLoader.getInstance().setHadoopHdfsUri(hadoopHdfs);
        ConfigurationLoader.getInstance().setSparkHost(sparkMaster);
        ConfigurationLoader.getInstance().setAppName(sparkAppName);
        ConfigurationLoader.getInstance().setExecutorMemory(sparkExecutorMemory);
        ConfigurationLoader.getInstance().setDriverHost(driverHost);
        ConfigurationLoader.getInstance().setRabbitmqNodename(rabbitMqHost);

    } catch (CmdLineException e) {
        // if there's a problem in the command line,
        // you'll get this exception. this will report
        // an error message.
        System.err.println(e.getMessage());
        System.err.println("java -cp $CLASSPATH [<spark-config-options>] <main-class> [<mazerunner-args>]");
        // print the list of available options
        parser.printUsage(System.err);
        System.err.println();

        // print option sample. This is useful some time
        System.err.println("  Example: java -cp $CLASSPATH org.mazerunner.core.messaging.Worker"
                + parser.printExample(ALL));

        return;
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(ConfigurationLoader.getInstance().getRabbitmqNodename());
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

    channel.basicQos(20);

    // Initialize spark context
    GraphProcessor.initializeSparkContext();

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);

    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());

        System.out.println(" [x] Received '" + message + "'");

        // Deserialize message
        Gson gson = new Gson();
        ProcessorMessage processorMessage = gson.fromJson(message, ProcessorMessage.class);

        // Run PageRank
        GraphProcessor.processEdgeList(processorMessage);

        System.out.println(" [x] Done '" + message + "'");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}