Martin Kóňa
Martin Kóňa

Reputation: 151

RabbitMq: How to receive message through DefaultConsumer?

Im using DefaultConsumer instead of QueueingConsumer becouse in this consumer i will connect to database and run procedures and after that i will send back ACK.

My question is very silly and i think easy...how to receive messages though class which extends DefaultConsumer?

When you use QueueingConsumer, you receive messages via QueueingConsumer.Delivery delivery = consumer.nextDelivery();

Upvotes: 1

Views: 3463

Answers (1)

Gabriele Santomaggio
Gabriele Santomaggio

Reputation: 22682

This is the main:

 Connection connection = factory.newConnection();
 Channel channel = connection.createChannel();
 channel.queueDeclare("myQueue", false, false, false, null);
 MyConsumer consumer = new MyConsumer(channel);
 String consumerTag = channel.basicConsume("myQueue", false, consumer);
 System.out.println("press any key to terminate");
 System.in.read();
 channel.basicCancel(consumerTag);
 channel.close();
 connection.close();

The actual consumer class is:

public class MyConsumer extends DefaultConsumer {


    public void handleDelivery(String consumerTag, Envelope envelope,
            BasicProperties properties, byte[] body) throws java.io.IOException {

        /// byte[] body = body Message
        /// here you have your message

        getChannel().basicAck(envelope.getDeliveryTag(), false);
    }


}

Upvotes: 2

Related Questions