38 lines
1.1 KiB
C#
38 lines
1.1 KiB
C#
|
using System.Text;
|
|||
|
using System.Threading.Channels;
|
|||
|
using RabbitMQ.Client;
|
|||
|
using RabbitMQ.Client.Events;
|
|||
|
|
|||
|
var factory = new ConnectionFactory { HostName = "localhost" };
|
|||
|
|
|||
|
using (var connection = factory.CreateConnection())
|
|||
|
{
|
|||
|
using (var channel = connection.CreateModel())
|
|||
|
{
|
|||
|
channel.QueueDeclare("task_queue", true, false, false, null);
|
|||
|
|
|||
|
channel.BasicQos(0, 1, false);
|
|||
|
|
|||
|
Console.WriteLine(" [*] Waiting for messages.");
|
|||
|
|
|||
|
var consumer = new EventingBasicConsumer(channel);
|
|||
|
consumer.Received += (model, ea) =>
|
|||
|
{
|
|||
|
byte[] body = ea.Body.ToArray();
|
|||
|
var message = Encoding.UTF8.GetString(body);
|
|||
|
Console.WriteLine($" [x] Received {message}");
|
|||
|
|
|||
|
int dots = message.Split('.').Length - 1;
|
|||
|
Thread.Sleep(dots * 1000);
|
|||
|
|
|||
|
Console.WriteLine(" [x] Done");
|
|||
|
|
|||
|
channel.BasicAck(ea.DeliveryTag, false);
|
|||
|
};
|
|||
|
|
|||
|
channel.BasicConsume("task_queue", false, consumer);
|
|||
|
|
|||
|
Console.WriteLine(" Press [enter] to exit.");
|
|||
|
Console.ReadLine();
|
|||
|
}
|
|||
|
}
|