-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
54 lines (50 loc) · 1.83 KB
/
Program.cs
File metadata and controls
54 lines (50 loc) · 1.83 KB
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
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Threading;
using RabbitMQTopic;
namespace ConsumerApp
{
class Program
{
static void Main(string[] args)
{
var consumer1 = new Consumer(new ConsumerSettings
{
AmqpUri = new Uri("amqp://demo:123456@localhost/test"),
ClientName = "Consumer1App",
PrefetchCount = 10,
GroupName = "Group1",
ConsumerCount = 2,
ConsumerSequence = 1 // 将消费队列 0,2
});
consumer1.OnMessageReceived += (sender, e) =>
{
Console.WriteLine($"consumer1:{System.Text.Encoding.UTF8.GetString(e.Context.GetBody())}");
e.Context.Ack();
};
var consumer2 = new Consumer(new ConsumerSettings
{
AmqpUri = new Uri("amqp://demo:123456@localhost/test"),
ClientName = "Consumer2App",
Mode = ConsumeMode.Pull,
GroupName = "Group1",
ConsumerCount = 2,
ConsumerSequence = 2 // 将消费队列 1,3
});
consumer2.OnMessageReceived += (sender, e) =>
{
Console.WriteLine($"consumer2:{System.Text.Encoding.UTF8.GetString(e.Context.GetBody())}");
e.Context.Ack();
};
consumer1.Subscribe("CommandTopic", 4);
consumer2.Subscribe("CommandTopic", 4);
consumer1.Start();
consumer2.Start();
Console.WriteLine("Consumer started!");
Thread.Sleep(3000);
consumer1.Shutdown();
Console.WriteLine("Consumer1 shutdown!");
consumer2.Shutdown();
Console.WriteLine("Consumer2 shutdown!");
}
}
}