forked from EasyNetQ/EasyNetQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PublisherConfirms.cs
executable file
·53 lines (42 loc) · 1.71 KB
/
PublisherConfirms.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
44
45
46
47
48
49
50
51
52
53
using System;
using System.Text;
using System.Threading;
using RabbitMQ.Client;
namespace Mike.AmqpSpike
{
public class PublisherConfirms
{
public void Spike()
{
WithChannel.Do(model =>
{
var queue = model.QueueDeclare("confirms.test", false, false, false, null);
model.ConfirmSelect();
model.BasicAcks += (model1, args) =>
Console.Out.WriteLine("Delivered DeliveryTag: '{0}', Multiple: {1}", args.DeliveryTag, args.Multiple);
model.BasicNacks += (model1, args) =>
Console.Out.WriteLine("Failed DeliverTag: '{0}', Multiple: {1}", args.DeliveryTag, args.Multiple);
Publish(model, queue, "Hello 1");
Publish(model, queue, "Hello 2");
Console.Out.WriteLine("Waiting for result");
Thread.Sleep(2000);
BasicGetResult result;
do
{
Thread.Sleep(100);
result = model.BasicGet(queue, false);
} while (result == null);
Console.Out.WriteLine("result.Body = {0}", Encoding.UTF8.GetString(result.Body));
model.BasicAck(result.DeliveryTag, false);
});
}
public void Publish(IModel model, string queue, string message)
{
var properties = model.CreateBasicProperties();
properties.SetPersistent(true);
var nextPublishSeqNo = model.NextPublishSeqNo;
Console.Out.WriteLine("Published seq no = {0}", nextPublishSeqNo);
model.BasicPublish("", queue, properties, Encoding.UTF8.GetBytes(message));
}
}
}