Is there any solution for sharing one queue among different message types?
I know topic exchange, but its use different queue.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With