2
votes

I am trying to test the behavior spring rabbitmq confirm callback , As per the api description if any negative acknowledgment is sent through consumer, confirm callback should gives the false value of ack but in my case always gives true. Even i published the message to deleted queue , i got the true value in confirm callback . Please let me know how to get the negative acknowledgement.

Below is the code how i created the RabbitTemplate bean.

@Bean
    public RabbitTemplate rabbitTemplate( ConnectionFactory connectionFactory ) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate( connectionFactory );
        ((CachingConnectionFactory)rabbitTemplate.getConnectionFactory()).setPublisherConfirms( true );
        rabbitTemplate.setConfirmCallback( new ConfirmCallback() {

            @Override
            public void confirm( CorrelationData corData, boolean ack, String cause ) {
                System.out.println( "devconfig.rabbitTemplate(...).new ConfirmCallback() {...}.confirm()"+corData );
                System.out.println( "devconfig.rabbitTemplate(...).new ConfirmCallback() {...}.confirm()"+ack );
            }
        } );
        return rabbitTemplate;

    }
1

1 Answers

0
votes

That's not the way acks work - ack means it was delivered to an exchange - it is rare to get a nack - according to the RabbitMQ documentation you will only get a nack if there's a problem in the broker itself...

Negative Acknowledgment

In exceptional cases when the broker is unable to handle messages successfully, instead of a basic.ack, the broker will send a basic.nack. In this context, fields of the basic.nack have the same meaning as the corresponding ones in basic.ack and the requeue field should be ignored. By nack'ing one or more messages, the broker indicates that it was unable to process the messages and refuses responsibility for them; at that point, the client may choose to re-publish the messages.

After a channel is put into confirm mode, all subsequently published messages will be confirmed or nack'd once. No guarantees are made as to how soon a message is confirmed. No message will be both confirmed and nack'd.

basic.nack will only be delivered if an internal error occurs in the Erlang process responsible for a queue.

In addition to this, Spring AMQP generates a Nack if the connection is closed before an ack is received (again - very rare).

If you want to get notified about the inability to deliver a message to a queue, you must enable publisher returns and set mandatory to true, and the message will be returned to you.