Search code examples
springrabbitmqmessage-queueamqpspring-amqp

Spring amqp: Remove all bindings for specific queue


Is there a way to remove all bindings for specific queue using spring-amqp?

There's a workaround, first delete a queue, and then redeclare it

amqpAdmin.deleteQueue("testQueue");
amqpAdmin.declareQueue(new Queue("testQueue"));

but this is pretty ugly solution and I'd like to avoid it


Solution

  • You can use the REST API to list the bindings and amqpAdmin.removeBinding() for those you want to remove.

    EDIT

    Here's the code using a Java 8 Stream - you can do the same thing by iterating over the list if you are not using Java 8...

    RabbitManagementTemplate rmt = new RabbitManagementTemplate("http://localhost:15672/api/", "guest", "guest");
    rmt.getBindings().stream()
        .filter(b -> b.getDestination().equals("q1") && b.isDestinationQueue())
        .forEach(b -> {
            System.out.println("Deleting " + b);
            amqpAdmin.removeBinding(b);
        });
    

    Result:

    Deleting Binding [destination=q1, exchange=, routingKey=q1]
    Deleting Binding [destination=q1, exchange=ex1, routingKey=foo]
    Deleting Binding [destination=q1, exchange=ex2, routingKey=foo]
    

    (when q1 was bound to the default exchange and 2 others).

    The RabbitAdmin amqpAdmin is used to do the deletes.