-
Notifications
You must be signed in to change notification settings - Fork 0
/
Worker.cs
43 lines (41 loc) · 1.82 KB
/
Worker.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using System;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading;
using Microsoft.Extensions.Configuration;
class Worker
{
public static void Main()
{
//https://leonidius2010.wordpress.com/2017/09/21/reading-appsettings-in-net-core-2-console-application/
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
string RabbitMQHost = config["RabbitMQHost"];
string RabbitMQQueueName = config["RabbitMQQueueName"];
//https://www.rabbitmq.com/tutorials/tutorial-two-dotnet.html
var factory = new ConnectionFactory() { HostName = RabbitMQHost };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: RabbitMQQueueName, 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;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
//int dots = message.Split('.').Length - 1;
//Thread.Sleep(dots * 1000);
Console.WriteLine(" [x] Done {0}", message);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: RabbitMQQueueName, autoAck: false, consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
}
}