Example usage for com.rabbitmq.client DefaultConsumer DefaultConsumer

List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer

Introduction

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

Prototype

public DefaultConsumer(Channel channel) 

Source Link

Document

Constructs a new instance and records its association to the passed-in channel.

Usage

From source file:v2.APAdmin.java

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

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

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

    Consumer consumer = new DefaultConsumer(channel) {
        @Override/*w ww.j  a  va2s .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(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
            //REPLY HERE
            String response = "You are in, breh";
            BasicProperties replyProps = new BasicProperties.Builder()
                    .correlationId(properties.getCorrelationId()).build();
            channel.basicPublish("", properties.getReplyTo(), replyProps, response.getBytes());
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

From source file:v2.PBAAdmin.java

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

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

    channel.queueBind(queueName, EXCHANGE_NAME, bindingKey);

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

    Consumer consumer = new DefaultConsumer(channel) {
        @Override/*from   ww  w .  j  a  v  a 2 s  .c  om*/
        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 '" + envelope.getRoutingKey() + "':'" + message + "'");
        }
    };
    channel.basicConsume(queueName, true, consumer);
}

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);/* w w  w .  j a  v  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:wunderrabbit.RabbitConnection.java

License:Apache License

@Override
public Listener listen(final Endpoint endpoint, final MessageHandler handler, Map<ListenOption, Object> options)
        throws Exception {

    Channel channel = this.connection.createChannel();
    Consumer consumer = new DefaultConsumer(channel) {
        public void handleDelivery(String tag, Envelope envelope, BasicProperties properties, byte[] body) {
            RabbitMessage message = new RabbitMessage(body, properties, endpoint);
            try {
                handler.onMessage(message);
            } catch (Exception e) {
                e.printStackTrace();// ww w . ja  v a  2s . c  om
            }
        }
    };

    //TODO: concurrency
    declareQueue(channel, endpoint);
    String tag = channel.basicConsume((String) endpoint.implementation(), consumer);

    return new RabbitListener(tag, channel);
}

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

License:Apache License

@Override
protected Sender createSender() throws Exception {
    RabbitMQSender result = RabbitMQSender.newBuilder().queue("zipkin-jmh").addresses("localhost:5672").build();

    CheckResult check = result.check();/*from  ww w. j av a 2 s.  co  m*/
    if (!check.ok()) {
        throw new AssumptionViolatedException(check.error().getMessage(), check.error());
    }

    channel = result.get().createChannel();
    channel.queueDelete(result.queue);
    channel.queueDeclare(result.queue, false, true, true, null);

    Thread.sleep(500L);

    new Thread(() -> {
        try {
            channel.basicConsume(result.queue, true, new DefaultConsumer(channel));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();

    return result;
}

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 {/*  ww w. ja v  a 2  s  . c o m*/
        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();
}