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:translators.ToOurJsonBank.java

public static void main(String[] args) throws Exception {

    RabbitConnection rabbitConnection = new RabbitConnection();
    Channel channel = rabbitConnection.makeConnection();
    channel.exchangeDeclare(ExchangeName.GLOBAL, "direct");
    String queueName = channel.queueDeclare().getQueue();
    channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.OUR_JSON_BANK);

    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override// w  ww .  ja v  a  2s .c o  m
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            System.out.println("Received msg: " + message);
            Message messageFromJson = getFromJson(message);

            sendMsgToBank(messageFromJson, properties.getCorrelationId(),
                    ExchangeName.OUTPUT_TRANSLATOR_TO_OUR_JSON_BANK, REPLY_QUEUE_NAME);
        }
    };

    channel.basicConsume(queueName, true, consumer);
}

From source file:translators.ToOurXmlBank.java

public static void main(String[] args) throws IOException, TimeoutException {

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare(exchangeName, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, exchangeName, "ourBankSoapXML");

    //get banks from queue. "Get banks" component
    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override/*  w ww  .j a  va  2  s .co  m*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            Message messageFromJson = getFromJson(message);
            connectToWebService(messageFromJson, properties.getCorrelationId(), EXCHANGE_NAME_SCHOOL,
                    replyQueueName);

        }
    };
    channel.basicConsume(queueName, true, consumer);

}

From source file:translators.ToXmlSchool.java

public static void main(String[] args) throws IOException {
    //final String replyQueueName = "replyFromBanks";
    final String replyQueueName = "teachersXmlReply";
    final String EXCHANGE_NAME_SCHOOL = "cphbusiness.bankXML";
    final String exchangeName = ExchangeName.GLOBAL;

    RabbitConnection rabbitConnection = new RabbitConnection();

    Channel channel = rabbitConnection.makeConnection();
    channel.exchangeDeclare(exchangeName, "direct");
    String queueName = channel.queueDeclare().getQueue();

    channel.queueBind(queueName, exchangeName, "keyBankXML");

    //get banks from queue. "Get banks" component
    QueueingConsumer consumer = new QueueingConsumer(channel) {
        @Override/*  ww  w .  ja  v a 2s  .c  om*/
        public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                byte[] body) throws IOException {
            String message = new String(body, "UTF-8");
            Message messageFromJson = getFromJson(message);
            sendMsgToBank(messageFromJson, properties.getCorrelationId(), EXCHANGE_NAME_SCHOOL, replyQueueName);
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void cleanQueue(String queue, String match) throws IOException, InterruptedException {
    logger.trace(queue.concat(": ").concat(match));
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
    Set seen = new HashSet();
    while (delivery != null) {
        String body = new String(delivery.getBody());
        if (seen.contains(body)) {
            break;
        }//from w  ww.ja v  a2 s .co  m
        if (body.contains(match)) {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
            seen.add(body);
        }
        delivery = consumer.nextDelivery(1000);
    }
    channel.close();
    closeRmqConn(connectionRcv);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public List<File> getFiles(String queue, Path outFolder, boolean ack) throws IOException, InterruptedException {
    List files = new ArrayList();
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);

    Set seen = new HashSet();

    while (delivery != null) {
        String host = delivery.getProperties().getHeaders().get("host").toString();
        File outTo = Paths.get(outFolder.toString(), host + ".tar.gz").toFile();
        FileUtils.writeByteArrayToFile(outTo, delivery.getBody());
        if (ack) {
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {/*w  ww.j a v a2 s.  c o  m*/
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
        }
        if (seen.contains(delivery.getProperties().getHeaders().get("host"))) {
            break;
        }
        seen.add(delivery.getProperties().getHeaders().get("host"));
        files.add(outTo);
        logger.info(queue + " retrieved: " + outTo.getAbsolutePath());
        delivery = consumer.nextDelivery(1000);
    }
    logger.warn("getFiles done");
    channel.close();
    closeRmqConn(connectionRcv);
    return files;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public WorkerState getWorkerState(String queue, long wait) throws IOException, InterruptedException {
    WorkerState ws = null;/*from  w  w w . j  a va  2s  .  c  o  m*/
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery;
    if (wait == -1) {
        delivery = consumer.nextDelivery(); // will block until response
    } else {
        delivery = consumer.nextDelivery(wait);
    }
    if (delivery != null) {
        String message = new String(delivery.getBody());
        ws = (WorkerState) Utils.jsonToObject(message, WorkerState.class);
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return ws;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Gets a single message from a queue, ideal for getting an item of work.
 * @param queue/*from   www  .  j av  a  2s .c o  m*/
 * @param wait
 * @return A JSON string representing an object, you need to know what type of object the queue will return and handle this outside of here
 * @throws IOException
 * @throws InterruptedException 
 */
public String getMessageString(String queue, long wait) throws IOException, InterruptedException {
    String message = null;
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery;
    if (wait == -1) {
        delivery = consumer.nextDelivery(); // will block until response
    } else {
        delivery = consumer.nextDelivery(wait);
    }
    if (delivery != null) {
        message = new String(delivery.getBody());
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        logger.info(queue + " recieved: " + message);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return message;
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

public void removeFromStateQueue(String queue, String hostToRemove) throws IOException, InterruptedException {
    logger.trace(queue.concat(": ").concat(hostToRemove));
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    int maxTries = channel.queueDeclare(queue, true, false, false, null).getMessageCount();
    logger.trace("Queue : messages\t" + queue + " : " + maxTries);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, false, consumer);
    QueueingConsumer.Delivery delivery = consumer.nextDelivery(200);
    logger.trace(maxTries);/*from w ww. j  ava 2 s  .c om*/
    while (delivery != null && maxTries > 0) {
        // the toString in the middle of this is needed as it is wrapped with another type that can hold 4GB
        Map<String, Object> headers = delivery.getProperties().getHeaders();
        if (headers != null && headers.get("host").toString().equals(hostToRemove)) {
            logger.trace(headers.get("host").toString().concat(" remove"));
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        } else {
            channel.basicNack(delivery.getEnvelope().getDeliveryTag(), false, true);
        }
        delivery = consumer.nextDelivery(200);
        maxTries--;
    }
    channel.close();
    closeRmqConn(connectionRcv);
}

From source file:uk.ac.sanger.cgp.wwdocker.messages.Messaging.java

License:Open Source License

/**
 * Gets all the messages in a queue, best for queues which receive status updates.
 * @param queue//from   www. j  a  v a2  s .c  o m
 * @param wait
 * @return List of JSON strings representing objects, you need to know what type of object the queue will return and handle this outside of here
 * @throws IOException
 * @throws InterruptedException 
 */
public List<String> getMessageStrings(String queue, long wait) throws IOException, InterruptedException {
    List<String> responses = new ArrayList();
    Connection connectionRcv = getRmqConn();
    Channel channel = connectionRcv.createChannel();
    channel.queueDeclare(queue, true, false, false, null);
    QueueingConsumer consumer = new QueueingConsumer(channel);
    channel.basicConsume(queue, true, consumer);
    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(wait);
        if (delivery == null) {
            break;
        }
        String message = new String(delivery.getBody());
        logger.info(queue + " recieved: " + message);
        responses.add(message);
    }
    channel.close();
    closeRmqConn(connectionRcv);
    return responses;
}

From source file:uk.ac.soton.itinnovation.experimedia.arch.ecc.amqpAPI.impl.faces.AbstractAMQPInterface.java

protected void createSubscriptionComponent() {
    Channel channel = (Channel) amqpChannel.getChannelImpl();

    subProcessor = new AMQPBasicSubscriptionProcessor(channel, subListenQueue, msgDispatch);

    try {/*from  w w  w  .  j  a  v  a  2  s  .  co m*/
        channel.basicConsume(subListenQueue, false, subProcessor);
    } catch (IOException ioe) {
        String err = "AMQP Interface could not create subscription component: " + ioe.getMessage();
        amqpIntLogger.error(err, ioe);
    }
}