Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rabbitmq different message types in one queue

Is there any solution for sharing one queue among different message types?

I know topic exchange, but its use different queue.

like image 379
Mohammad Hatami Avatar asked Sep 02 '25 08:09

Mohammad Hatami


1 Answers

RabbitMQ is agnostic to the type/content of messages, so you can easily publish very different types of data to a single queue. It's your application that has to handle the parsing. For this, I'll present two solutions:

Message headers

You can use the message headers to add additional information about the type of message.

Dictionary<string, object> headers = new Dictionary<string, object>();
headers("type", "type1");

IBasicProperties basicProperties = model.CreateBasicProperties();
basicProperties.Headers = headers;
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
model.BasicPublish(_headersExchange, "", basicProperties, messageBytes);

Since it uses the message headers, you can later always route them to different queues using a header exchange, so that's one benefit over the next approach.

To use the header after receiving a message, you can use something like this:

deliveryArguments.BasicProperties.Headers[headerKey]

Encoding message type in message body

You can also define your own message format, or add an additional field describing the type of data. But this implementation is highly dependent on your current message format, so I don't think it would make sense to give an example.

like image 165
Pieter Delobelle Avatar answered Sep 04 '25 21:09

Pieter Delobelle