Notifying the consumers of queue failures

Following the AMQP standard, consumers are not informed about the queue deletions. A consumer waiting for messages on a queue that is deleted will not receive any error condition and will wait there indefinitely.

However, the RabbitMQ client provides an extension that lets the consumer receive a cancel parameter in such a case: consumer cancel notifications. We are going to see it in the example, which you can find in Chapter02/Recipe07/Java/src/rmqexample.

Getting ready

To use this recipe, we need to set up the Java development environment as indicated in the Introduction section of Chapter 1, Working with AMQP.

How to do it...

To make this extension work, you just need to perform the following step:

  1. Override the handleCancel() method of the customized consumer, which we derived from com.rabbitmq.client.DefaultConsumer (refer to ActualConsumer.java):
    public void handleCancel(String consumerTag) throws IOException {
      ...
    }

How it works...

In our example, we have chosen to implement a consumer (of log events) that must work only if the producer is present and the queue where it listens is created by the producer.

So, if the queue is not present, the Consumer.java file exits immediately with an error. This behavior is accomplished by calling channel.queueDeclarePassive().

The Producer.java class creates the queue as it starts and deletes it when closed, calling channel.queueDelete(). If it's closed while the consumer is consuming messages, the consumer is immediately notified by the RabbitMQ client library that calls handleCancel() overridden in step 1 of our recipe.

Other than being explicitly cancelled by calling channel.basicCancel(), the consumer can be cancelled for any reason using handleCancel(). Only in this case, the RabbitMQ client library invokes a different method of the Consumer interface: handleCancelOK().

There's more...

Consumer cancel notifications are an extension of the client library, and not the general of all the AMQP client libraries. A library that implements them must declare it as an optional capability (refer to http://www.rabbitmq.com/consumer-cancel.html#capabilities).

The RabbitMQ client library supports and declares such a capability to the broker.

See also

In case a node fails and it is part of a RabbitMQ cluster, the same thing happens: a client consuming from its queues is not informed, unless it has defined its own handleCancel() override. For more information on this, refer to Chapter 6, Developing Scalable Applications.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset