-
Notifications
You must be signed in to change notification settings - Fork 57
/
ConfirmsAwareChannel.cs
55 lines (48 loc) · 1.73 KB
/
ConfirmsAwareChannel.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
54
55
namespace NServiceBus.Transports.RabbitMQ
{
using System;
using System.Text.RegularExpressions;
using global::RabbitMQ.Client;
using global::RabbitMQ.Client.Exceptions;
using Janitor;
using Unicast.Queuing;
[SkipWeaving]
class ConfirmsAwareChannel : IDisposable
{
public IModel Channel { get; private set; }
public ConfirmsAwareChannel(IConnection connection, bool usePublisherConfirms, TimeSpan maxWaitTimeForConfirms)
{
this.usePublisherConfirms = usePublisherConfirms;
this.maxWaitTimeForConfirms = maxWaitTimeForConfirms;
Channel = connection.CreateModel();
if (usePublisherConfirms)
{
Channel.ConfirmSelect();
}
}
public void Dispose()
{
try
{
if (usePublisherConfirms)
{
Channel.WaitForConfirmsOrDie(maxWaitTimeForConfirms);
}
Channel.Dispose();
}
catch (AlreadyClosedException ex)
{
if (ex.ShutdownReason != null && ex.ShutdownReason.ReplyCode == 404)
{
var msg = ex.ShutdownReason.ReplyText;
var matches = Regex.Matches(msg, @"'([^' ]*)'");
var exchangeName = matches.Count > 0 && matches[0].Groups.Count > 1 ? Address.Parse(matches[0].Groups[1].Value) : null;
throw new QueueNotFoundException(exchangeName, "Exchange for the recipient does not exist", ex);
}
throw;
}
}
readonly bool usePublisherConfirms;
readonly TimeSpan maxWaitTimeForConfirms;
}
}