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:wiki.messaging.Receive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;//from w ww  .  java  2s . com
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResult(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:wiki.messaging.ResultConsumer.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String rabbitMqUrl = null;//from   w  w  w . java  2s.c om
    String resultUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("rs")) {
            resultUrl = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        }
    }

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(RESULT_QUEUE_NAME, autoAck, consumer);

    DbConnector dbc = new DbConnector(resultUrl);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000);
        if (delivery == null)
            break;
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        saveResult(message, dbc);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
    channel.close();
    connection.close();
}

From source file:wiki.messaging.Treeceive.java

License:Open Source License

public static void main(String[] args) throws IOException, InterruptedException {
    String url = null;/*from w  w  w  . j  av a  2s . c  o  m*/
    String rabbitMqUrl = null;
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("ds")) {
            url = args[i + 1];
        } else if (args[i].equals("rq")) {
            rabbitMqUrl = args[i + 1];
        } else if (args[i].equals("mj")) {
            maxJobs = Integer.valueOf(args[i + 1]);
        }
    }
    if (rabbitMqUrl == null) {
        rabbitMqUrl = "192.168.1.108";
    }
    if (url == null) {
        url = "localhost";
    }
    //        if (resultUrl == null) {
    //            resultUrl = url;
    //        }
    threadPool = new ExecutorCompletionService<>(Executors.newFixedThreadPool(maxJobs));
    System.out.println("DataSource: " + url);
    //        System.out.println("ResultWrite: " + resultUrl);
    DbConnector ds = new DbConnector(url);
    //        DbConnector rs = new DbConnector(resultUrl);

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitMqUrl);
    factory.setUsername("wiki");
    factory.setPassword("wiki");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.queueDeclare(QUEUE_NAME, false, false, false, null);
    channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages.");

    channel.basicQos(maxJobs);

    QueueingConsumer consumer = new QueueingConsumer(channel);
    boolean autoAck = false;
    channel.basicConsume(QUEUE_NAME, autoAck, consumer);

    while (true) {
        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
        String message = new String(delivery.getBody());
        System.out.println(" [x] Received '" + message + "'");
        doWork(message, ds);
        saveResults(channel);
        System.out.println(" [x] Done");
        channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
}

From source file:workqueue.Worker.java

public static void main(String[] argv) throws Exception {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("datdb.cphbusiness.dk");
    final Connection connection = factory.newConnection();
    final Channel channel = connection.createChannel();

    channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);
    System.out.println(" [*] Waiting for messages. To exit press CTRL+C");

    channel.basicQos(1);//from  w w w.  jav a  2 s. c  o  m

    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, "UTF-8");

            System.out.println(" [x] Received '" + message + "'");
            try {
                doWork(message);
            } finally {
                System.out.println(" [x] Done");
                channel.basicAck(envelope.getDeliveryTag(), false);
            }
        }
    };
    channel.basicConsume(TASK_QUEUE_NAME, false, consumer);
}

From source file:zipkin2.reporter.amqp.RabbitMQSenderTest.java

License:Apache License

private byte[] readMessage() throws Exception {
    final CountDownLatch countDown = new CountDownLatch(1);
    final AtomicReference<byte[]> result = new AtomicReference<>();

    Channel channel = sender.get().createChannel();
    try {// www  . j  av a2  s .c  om
        channel.basicConsume(sender.queue, true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                    byte[] body) throws IOException {
                result.set(body);
                countDown.countDown();
            }
        });
        countDown.await(5, TimeUnit.SECONDS);
    } finally {
        channel.close();
    }
    return result.get();
}