I wrote these code lines, but I couldn't figure out how I can print some values on console in consumer.receive
. This code is working because I checked some values on RabbitMQ CloudAMQP, but I cannot see any changes on console.
Problem is here:
Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()) + " received");
Full code:
// publisher
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri("amqps://guest:guest@localhost");
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
channel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);
for (int i = 1; i <= 100; i++)
{
byte [] bytemessage = Encoding.UTF8.GetBytes($"is - {i}");
IBasicProperties properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.BasicPublish(exchange: "kuyruk", routingKey: "", basicProperties: properties, body: bytemessage);
}
}
// Consumer
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri("amqps://guest:guest@localhost");
using (IConnection connection = factory.CreateConnection())
using (IModel channel = connection.CreateModel())
{
channel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);
// Here consumer İçin Oluşturulacak Kuyruklara Random İsim Oluşturma
string queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName, exchange: "kuyruk", routingKey: "");
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
channel.BasicConsume(queueName, false, consumer);
consumer.Received += (sender, e) =>
{
Thread.Sleep(500);
Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()) + " received");
channel.BasicAck(e.DeliveryTag, false);
};
Console.Read();
}