Ask Your Question
4

Instead of matching headers, is there a way for RabbitMQ headers Exchange to route messages to all queues?

asked 2021-12-03 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-10-11 08:00:00 +0000

nofretete gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-03 11:00:00 +0000

Seen: 11 times

Last updated: Oct 11 '22