manuel mourato
manuel mourato

Reputation: 799

RabbitMQ basicPublish not inserting messages to queue

This is probably some silly mistake I'm missing, but here is the issue:

I am trying to insert a simple "hello" message into a Rabbit queue, with a predefined exchange and routing key. This is the code that I am using:

    private static void send_equity_task_to_rabbitmq(ConnectionFactory factory) throws IOException,TimeoutException{

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare("b", false, false, false, null);
        channel.exchangeDeclare("b", "direct");

        channel.basicPublish("b","b",null, "hello".getBytes());

        channel.close();
        connection.close();
    }

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

    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("127.0.0.1");

    Date start_time= Calendar.getInstance().getTime();
    Long start_time_timestamp=System.currentTimeMillis();

    System.out.println("[INFO] Starting connection to queue at:"+start_time);
        send_equity_task_to_rabbitmq(factory);

        Long end_time_timestamp=System.currentTimeMillis();

        System.out.println("[INFO] Message sent and processed successfully after:"+ (end_time_timestamp-start_time_timestamp)+" miliseconds");

 }
}

The code runs without any error. However, when I check the amount of records inside the "b" queue, I get:

$ rabbitmqctl list_queues
Listing queues ...
b       0
...done.

I don't have consumers for this queue at the moment, so I assume since it has 0 records, that I am using basicPublish badly. What could be wrong?

Thank you.

Upvotes: 2

Views: 3786

Answers (1)

Breandán Dalton
Breandán Dalton

Reputation: 1639

I think you need to bind the queue to the exchange. You've created a queue called "b" and an exchange called "b". The exchange will distribute messages to queues that are bound to it, using the "b" routingKey, but as the "b" queue isn't bound to the "b" exchange, the "b" exchange doesn't publish to that queue.

Upvotes: 4

Related Questions