16
ConnectionFactory factory = new ConnectionFactory {HostName = "localhost"};

using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
    channel.QueueDeclare("hello", false, false, false, null);
    for (int i = 0; i < 100000; i++)
    {
        MemoryStream stream = new MemoryStream();

        var user = new User 
                       {
                           Id = i
                       };

        Serializer.Serialize(stream, user);


        channel.BasicPublish("", "hello", null, stream.ToArray());

    }

}

I have the code above, and I'm curious about thread safety.

I am not sure, but I would imagine ConnectionFactory is thread safe. But is IConnection thread safe? Should I create a connection per request? Or rather a single persistent connection? And what about channel (IModel)?

Also, should I store the connection as ThreadLocal? Or should I create a connection per request?

Pang
  • 9,564
  • 146
  • 81
  • 122
DarthVader
  • 52,984
  • 76
  • 209
  • 300
  • 1
    see my answer to this question http://stackoverflow.com/questions/10407760/is-there-a-performance-difference-between-pooling-connections-or-channels-in-rab/10501593#10501593 – robthewolf Aug 19 '12 at 08:13

1 Answers1

44

IConnection is thread safe, IModel is not. Generally you should endeavour to keep a connection open for the lifetime of your application. This is especially true if you have consumers which need an open connection in order to receive messages. It's important to detect and recover from interrupted connections, either because of network or Broker failure. I'd recommend reading 'RabbitMQ in Action' by Videla and Williams, especially chapter 6 'Writing code that survives failure'.

Now for a shameless plug. I'm the author of EasyNetQ, a high-level .NET API for RabbitMQ. It does all the connection management for you and will automatically re-connect and rebuild all your subscribers if there's a network or broker outage. It also provides cluster and fail-over support out of the box. Give it a try.

Mike Hadlow
  • 9,427
  • 4
  • 45
  • 37
  • 12
    "Extremely careful"? What kind of vague threat is that? EasyNetQ is a perfectly appropriate suggestion here. If people disagree, that's what the downvote button is for. It's unlikely the suggestion would've been given a second thought had he not also done the courtesy of disclaiming his authorship. I would be extremely careful about riding that high on your horse. – G-Wiz Mar 08 '14 at 03:04
  • Good answer. It aligns with https://www.rabbitmq.com/dotnet-api-guide.html – Max Barraclough Aug 22 '18 at 09:09
  • EasyNetQ is great! However I have been getting "Pipelining of requests forbidden" sometimes. – Allan Zeidler Jul 20 '21 at 17:39