2

I have been trying to create a .NET RabbitMQ consumer application and there are some questions where I can not find answer through google so I thought I should ask them here:

  1. In the specification I've found that the IModel instance returned by the IConnection.CreateModel is not thread-safe. Is this related only to calling the IModel.BasicPublish method (if I understand correctly then BasicPublish/BasicAck/BasickNack/ets are not thread-safe) or does this also include the registered consumer? In other words is it sufficient to use the same lock around IModel.HandleBasicDeliver or do I also have to have use the same lock to wrap the body of IBasicConsumer.HandleBasicDelivery?

  2. IModel.BasicConsume has a boolean parameter noAck which I can not find any help for. Does setting this parameter to True mean that no auto-acknowledgment will be done when messages are delivered via IBasicConsumer.HandleBasicDeliver? If I set it to False then the RabbitMQ .Net library will automatically send Ack(s) for all received messages?

  3. Are the IBasicConsumer methods calls serialized? In other words will IBasicConsumer.HandleBasicDeliver be called while a message is already being handled by IBasicConsumer.HandleBasicDeliver?

1 Answer 1

3

In other words is it sufficient to use the same lock around IModel.HandleBasicDeliver or do I also have to have use the same lock to wrap the body of IBasicConsumer.HandleBasicDelivery?

I would avoid multithreaded access to the RabbitMQ .NET client API in general. I suggest instead that you run multiple consumers within their own process. I've blogged about this in detail here. To answer your question, if you must allow multiple threads to access the process, I would lock the function, regardless of the implementation that you're using (IModel, IBasicConsumer, etc.). Remember; IModel itself is not threadsafe, and therefore neither are any of its implementations.

If I set it to False then the RabbitMQ .Net library will automatically send Ack(s) for all received messages?

No, it won't. You need to manually send an acknowledgement in this case. Unless an acknowledgement is sent, the message will remain on the Queue.

Are the IBasicConsumer methods calls serialized? In other words will IBasicConsumer.HandleBasicDeliver be called while a message is already being handled by IBasicConsumer.HandleBasicDeliver?

I'm assuming you're asking whether or not calls are invoked serially, one after the other, as opposed to serialisation as a process of converting C# objects to markup. In that case, the answer is yes, calls are handled sequentially and won't overlap. Consumers can leverage the QOS property to determine the number of messages that they read at a time. Even with a high QOS value, the Consumer will still process messages sequentially. Again, see the link above for more detailed information.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I will use separate channel (IModel) for BasicPublish and setup consumers on another channel.
No problem. Yes, it's a good idea to restrict each concern to a separate channel. Channels are simply logical pipelines into a TCP connection, and don't add much overhead.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.