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.wso2.siddhi.debs2017.input.DebsBenchmarkSystem.java

License:Open Source License

private void registerConsumerFor(RabbitQueue queue) throws IOException {
    Channel channel = queue.getChannel();
    channel.basicConsume(queue.getName(), false, new DefaultConsumer(channel) {
        @Override//from w ww  .java 2 s . co  m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            getChannel().basicAck(envelope.getDeliveryTag(), false);
            DebsBenchmarkSystem.this.handleDelivery(body);
        }
    });
}

From source file:org.wso2.siddhi.debs2017.input.rabbitmq.RabbitMQConsumer.java

License:Open Source License

/**
 *  Consumes the sample data/*from  w  w w . j ava 2  s .  com*/
 *
 */
public void consume(String queue, String host, String host1, int port1, String host2, int port2, String host3,
        int port3, int executorSize) {
    TASK_QUEUE_NAME = queue;
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(host);
    final Connection connection;
    final Channel channel;
    final Consumer consumer;
    try {
        connection = factory.newConnection(EXECUTORS);
        channel = connection.createChannel();
        consumer = new EventDispatcher(channel, host1, port1, host2, port2, host3, port3, executorSize);
        boolean autoAck = true; // acknowledgment is covered below
        channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (TimeoutException e) {
        e.printStackTrace();
    }
}

From source file:ox.softeng.burst.service.message.RabbitMessageService.java

@Override
public void run() {
    for (int c = 0; c < consumerCount; c++) {
        try {/*from w  w w. j a v  a 2  s .  co  m*/
            Channel channel = connection.createChannel();
            channel.basicQos(40);
            channel.exchangeDeclare(exchange, "topic", true);
            Consumer consumer = new MessageConsumer(channel, entityManagerFactory);
            channel.basicConsume(queue, true, consumer);
        } catch (IOException e) {
            logger.error("Error running consumer for queue '" + queue + "'", e);
        } catch (Exception e) {
            logger.error("Unhandled exception: " + e.getMessage(), e);
        }
    }
}

From source file:ox.softeng.burst.services.RabbitService.java

License:Open Source License

public void run() {
    try {// ww  w . ja  v  a 2s. c  om
        Channel channel = connection.createChannel();
        channel.basicQos(40);
        channel.exchangeDeclare(rabbitMQExchange, "topic", true);
        Consumer consumer = createConsumer(channel);
        logger.warn("Waiting for messages");
        channel.basicConsume(rabbitMQQueue, true, consumer);
    } catch (IOException e) {
        logger.error("Error running consumer for queue '" + rabbitMQQueue + "'", e);
    } catch (Exception e) {
        logger.error("Unhandled exception: " + e.getMessage(), e);
    }
}

From source file:pl.nask.hsn2.bus.rabbitmq.endpoint.RbtConsumeEndPoint.java

License:Open Source License

/**
 * This method creates single consumer./*from   w  w  w. j  a  va 2 s  .  co  m*/
 * 
 * @throws BusException If there is a problem with creating consumer.
 */
private void createConsumer() throws BusException {
    Channel channel = null;
    try {
        channel = connection.createChannel();
    } catch (IOException e) {
        LOGGER.error("Error creating channel.");
        throw new BusException("Can't create channel.", e);
    }

    Consumer consumer = new RbtDefaultConsumer(autoack, channel, responseHandler) {
        @Override
        public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) {
            try {
                close();
            } catch (BusException ex) {
                LOGGER.error("Error during shutdown customer.", ex);
            }
            super.handleShutdownSignal(consumerTag, sig);
        }
    };

    try {
        channel.basicConsume(consumerQueueName, this.autoack, consumer);
        this.channels.add(channel);
    } catch (IOException e) {
        LOGGER.error("Error creating consumer.");
        throw new BusException("Can't create consumer.", e);
    }
}

From source file:pl.nask.hsn2.DataStoreActiveCleaner.java

License:Open Source License

/**
 * Initialize RabbitMQ connection./*from www. ja v  a  2  s. c  o  m*/
 *
 * @return RabbitMQ consumer.
 * @throws IOException
 *             When there's some connection issues.
 */
private QueueingConsumer initRabbitMqConnection() throws IOException {
    ConnectionFactory connFactory = new ConnectionFactory();
    connFactory.setHost(rbtHostName);
    rbtConnection = connFactory.newConnection();
    Channel channel = rbtConnection.createChannel();
    channel.exchangeDeclare(rbtNotifyExchName, "fanout");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, rbtNotifyExchName, "");
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queueName, AUTO_ACK, consumer);
    return consumer;
}

From source file:play.modules.rabbitmq.consumer.RabbitMQConsumer.java

License:Apache License

/**
 * Creates the channel.//from w  w w  .ja  v a2s .  c o m
 * 
 * @param channel
 *            the channel
 * @param plugin
 *            the plugin
 * @return the channel
 * @throws Exception
 *             the exception
 */
protected QueueingConsumer createConsumer(Channel channel, RabbitMQPlugin plugin) throws Exception {
    // Get Plugin
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(this.queue(), plugin.isAutoAck(), consumer);

    // Log Debug
    Logger.info("RabbitMQ Consumer - Channel: %s, Consumer: %s " + channel, consumer);

    // Return Channel
    return consumer;
}

From source file:pro.foundev.examples.spark_streaming.java.interactive.querybasedconsumer.QueryConsumer.java

License:Apache License

public void run() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
    final Session session = cluster.connect();
    session.execute(String.format("CREATE TABLE IF NOT EXISTS tester.warningsrdd (ssn text, "
            + "batchStartTime bigint, id uuid, amount decimal, rule text, PRIMARY KEY(batchStartTime, id))"));
    final PreparedStatement preparedStatement = session
            .prepare("SELECT * FROM tester.warningsrdd where batchStartTime = ?");
    try {/*from   w  w  w  .ja va2  s  . com*/
        Connection connection = factory.newConnection();
        final Channel channel = connection.createChannel();
        final String queue = channel.queueDeclare().getQueue();
        channel.queueBind(queue, EXCHANGE_NAME, "");

        final Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                String message = new String(body);
                long batchStartTime = Long.parseLong(message);
                System.out.println("Writing batch with start time of " + new Date(batchStartTime));
                ResultSet warningsResultSet = session.execute(preparedStatement.bind(batchStartTime));
                int count = 0;
                for (Row warning : warningsResultSet) {
                    count += 1;
                    BigDecimal decimal = warning.getDecimal("amount");
                    UUID id = warning.getUUID("id");
                    String ssn = warning.getString("ssn");
                    String rule = warning.getString("rule");
                    Warning warningObj = new Warning();
                    warningObj.setAmount(decimal);
                    warningObj.setId(id);
                    warningObj.setSsn(ssn);
                    warningObj.setRule(rule);
                    notifyUI(warningObj);
                }
                System.out.println("Batch with start time of " + new Date(batchStartTime) + " Complete with "
                        + count + " items.");
            }
        };
        channel.basicConsume(queue, true, consumer);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:pro.foundev.examples.spark_streaming.java.interactive.smartconsumer.DeduplicatingRabbitMQConsumer.java

License:Apache License

public void run() {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    try {/*w w w . ja v  a 2 s .  c  o m*/
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(EXCHANGE_NAME, true, consumer);
        Set<String> messages = new HashSet<>();
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.start();
        while (true) {
            QueueingConsumer.Delivery delivery = consumer.nextDelivery();
            String message = new String(delivery.getBody());
            messages.add(message);

            if (stopwatch.elapsed(TimeUnit.MILLISECONDS) > 1000l) {
                System.out.println("it should be safe to submit messages now");
                for (String m : messages) {
                    //notifying user interface
                    System.out.println(m);
                }
                stopwatch.stop();
                stopwatch.reset();
                messages.clear();
            }
            if (messages.size() > 100000000) {
                System.out.println("save off to file system and clear before we lose everything");
                messages.clear();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:pt.ua.ies.ControllerView.java

@PostConstruct
public void init() {
    System.out.println("init");
    recive = "";/*ww  w .j a v a2 s. c om*/
    reciveBeforeRefresh = recive;
    try {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

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

        channel.exchangeDeclare(DisplayMessage.EXCHANGE_NAME, "fanout");
        String queueName = channel.queueDeclare().getQueue();
        channel.queueBind(queueName, DisplayMessage.EXCHANGE_NAME, "");

        MessageConsumer m = new MessageConsumer(channel, this);

        channel.basicConsume(queueName, true, m);
    } catch (Exception ex) {
        Logger.getLogger(ControllerView.class.getName()).log(Level.SEVERE, null, ex);
    }
}