Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, there is a way to route messages to all queues using RabbitMQ headers exchange. You can use the special header "x-match" and set it to "all". This will match all headers in the message, and the message will be routed to all queues that have bindings with the headers that match.

Here's an example of how to create a headers exchange with "x-match" set to "all":

channel.exchangeDeclare("myHeadersExchange", "headers");
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("header1", "value1");
headers.put("header2", "value2");
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().headers(headers).build();

// create bindings
channel.queueBind("queue1", "myHeadersExchange", "", headers);
channel.queueBind("queue2", "myHeadersExchange", "", headers);

// publish message with x-match set to all
Map<String, Object> matchHeaders = new HashMap<String, Object>();
matchHeaders.put("x-match", "all");
matchHeaders.put("header1", "value1");
matchHeaders.put("header2", "value2");
channel.basicPublish("myHeadersExchange", "", new AMQP.BasicProperties.Builder()
                                .headers(matchHeaders).build(), "Hello, world!".getBytes());

With this configuration, the message will be routed to both "queue1" and "queue2" since both have bindings with header1=value1 and header2=value2.