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 ?
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.
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.
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