Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ send and consume message within one connection

Could I create one connection for listening a queue and sending messages to another exchange ? Currently, in case I use one connection for sending, my consumer receives it, but it even is not bound to this exchange.

Maybe, some settings should be changed ?

like image 959
Alex Avatar asked Oct 12 '25 05:10

Alex


2 Answers

Yea, sure you can go with it.

  • In RabbitMq you would like to keep connection (at least one) open as long as possible, due to performance impact of opening one each and every time, it thread safe in C#.

  • On opposite. channel is meant to be opened for each and every thread and can't be shared in C# client implementation.

Until you work strictly linear (no parallel processing), you can consume process and publish via same channel (on of course same connection).

Pay attention that multiple channels that were opened for one connection can block each other in terms of traffic. Only one channel can send/receive through connection simultaneously. Because of this try organizing connections and channels that will not block each other during simultaneous publish and consume.

like image 125
gelmanet Avatar answered Oct 14 '25 21:10

gelmanet


Yes, you can use the same connection but you have to use two channels.

Read here: http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

For example:

using (var channel = myConnection.CreateModel())
{
    channel.QueueDeclare("hello", false, false, false, null);

    var consumer = new QueueingBasicConsumer(channel);
    channel.BasicConsume("", true, consumer);

and with the same connection...

using (var channel = myConnection.CreateModel())
{
    channel.QueueDeclare("hello", false, false, false, null);
    string message = "Hello World!";
    var body = Encoding.UTF8.GetBytes(message);

    channel.BasicPublish("", "", null, body);
    Console.WriteLine(" [x] Sent {0}", message);
}

Please note: The c# channel isn't multi-threading so you should create one for thread.

like image 44
Gabriele Santomaggio Avatar answered Oct 14 '25 21:10

Gabriele Santomaggio