Skip to content
Advertisement

Re-creating a queue afters its manually deleted RabbitMQ

Is it possible to automatically re-create a queue afters it has been manually removed?

I’m able to create the queue on start up using

@Bean
public Queue createRandomQueue(@Qualifier("exporterRabbitAdmin") RabbitAdmin exporterRabbitAdmin) {
    Queue queue = createQueue("random.queue", exporterRabbitAdmin);
    exporterRabbitAdmin.declareQueue(queue);
    return queue;
}

However, if it’s manually removed from rabbitmq, I want to be able to create a queue with the same name automatically.

Today if I remove it, I will keep throwing exceptions like

org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[random.queue]
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:710) [spring-rabbit-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.passiveDeclarations(BlockingQueueConsumer.java:594) [spring-rabbit-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at ....


Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'export-hub-activity.retry.queue' in vhost 'default', class-id=50, method-id=10)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66) ~[amqp-client-5.4.3.jar:5.4.3]
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36) ~[amqp-client-5.4.3.jar:5.4.3]

Advertisement

Answer

Yes, you can re-create the queue which you have deleted. In RabbitMQ each operation is an event. When a queue is deleted, it is recorded as an event and these events can be consumed like normal messages.

You can enable RabbitMQ events by enabling the rabbitmq_event_exchange plugin. Once it is set, bind it to a queue. While consuming the messages from the events-queue, you need to check for the message that contains queue.deleted. If there is any such message, you can re-create the queue. The queue.deleted event will also contain the queue name and the virtual host

Reference: https://www.rabbitmq.com/event-exchange.html

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement