diff --git a/yakovleva_yulia_lab_4/NewTask/NewTask.cs b/yakovleva_yulia_lab_4/NewTask/NewTask.cs index 0fe09d7..31f4518 100644 --- a/yakovleva_yulia_lab_4/NewTask/NewTask.cs +++ b/yakovleva_yulia_lab_4/NewTask/NewTask.cs @@ -5,8 +5,8 @@ var factory = new ConnectionFactory { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); -channel.QueueDeclare(queue: "hello", - durable: false, +channel.QueueDeclare(queue: "task_queue", + durable: true, exclusive: false, autoDelete: false, arguments: null); @@ -14,9 +14,12 @@ channel.QueueDeclare(queue: "hello", var message = GetMessage(args); var body = Encoding.UTF8.GetBytes(message); +var properties = channel.CreateBasicProperties(); +properties.Persistent = true; + channel.BasicPublish(exchange: string.Empty, - routingKey: "hello", - basicProperties: null, + routingKey: "task_queue", + basicProperties: properties, body: body); Console.WriteLine($" [x] Sent {message}"); diff --git a/yakovleva_yulia_lab_4/Worker/Worker.cs b/yakovleva_yulia_lab_4/Worker/Worker.cs index f67c1fe..e712fba 100644 --- a/yakovleva_yulia_lab_4/Worker/Worker.cs +++ b/yakovleva_yulia_lab_4/Worker/Worker.cs @@ -6,18 +6,20 @@ var factory = new ConnectionFactory { HostName = "localhost" }; using var connection = factory.CreateConnection(); using var channel = connection.CreateModel(); -channel.QueueDeclare(queue: "hello", - durable: false, +channel.QueueDeclare(queue: "task_queue", + durable: true, exclusive: false, autoDelete: false, arguments: null); +channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false); + Console.WriteLine(" [*] Waiting for messages."); var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { - var body = ea.Body.ToArray(); + byte[] body = ea.Body.ToArray(); var message = Encoding.UTF8.GetString(body); Console.WriteLine($" [x] Received {message}"); @@ -25,9 +27,12 @@ consumer.Received += (model, ea) => Thread.Sleep(dots * 1000); 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", - autoAck: true, +channel.BasicConsume(queue: "task_queue", + autoAck: false, consumer: consumer); Console.WriteLine(" Press [enter] to exit.");