work 2.2 done

This commit is contained in:
JulYakJul 2024-10-18 16:24:18 +04:00
parent 060bd2321e
commit 94b8ba783c
2 changed files with 17 additions and 9 deletions

View File

@ -5,8 +5,8 @@ var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection(); using var connection = factory.CreateConnection();
using var channel = connection.CreateModel(); using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "hello", channel.QueueDeclare(queue: "task_queue",
durable: false, durable: true,
exclusive: false, exclusive: false,
autoDelete: false, autoDelete: false,
arguments: null); arguments: null);
@ -14,9 +14,12 @@ channel.QueueDeclare(queue: "hello",
var message = GetMessage(args); var message = GetMessage(args);
var body = Encoding.UTF8.GetBytes(message); var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.BasicPublish(exchange: string.Empty, channel.BasicPublish(exchange: string.Empty,
routingKey: "hello", routingKey: "task_queue",
basicProperties: null, basicProperties: properties,
body: body); body: body);
Console.WriteLine($" [x] Sent {message}"); Console.WriteLine($" [x] Sent {message}");

View File

@ -6,18 +6,20 @@ var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection(); using var connection = factory.CreateConnection();
using var channel = connection.CreateModel(); using var channel = connection.CreateModel();
channel.QueueDeclare(queue: "hello", channel.QueueDeclare(queue: "task_queue",
durable: false, durable: true,
exclusive: false, exclusive: false,
autoDelete: false, autoDelete: false,
arguments: null); arguments: null);
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
Console.WriteLine(" [*] Waiting for messages."); Console.WriteLine(" [*] Waiting for messages.");
var consumer = new EventingBasicConsumer(channel); var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) => consumer.Received += (model, ea) =>
{ {
var body = ea.Body.ToArray(); byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body); var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] Received {message}"); Console.WriteLine($" [x] Received {message}");
@ -25,9 +27,12 @@ consumer.Received += (model, ea) =>
Thread.Sleep(dots * 1000); Thread.Sleep(dots * 1000);
Console.WriteLine(" [x] Done"); Console.WriteLine(" [x] Done");
// here channel could also be accessed as ((EventingBasicConsumer)sender).Model
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
}; };
channel.BasicConsume(queue: "hello", channel.BasicConsume(queue: "task_queue",
autoAck: true, autoAck: false,
consumer: consumer); consumer: consumer);
Console.WriteLine(" Press [enter] to exit."); Console.WriteLine(" Press [enter] to exit.");