-
Notifications
You must be signed in to change notification settings - Fork 0
/
RabbitMQPersistentConnection.cs
122 lines (99 loc) · 3.55 KB
/
RabbitMQPersistentConnection.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.IO;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Exceptions;
namespace Receive
{
public interface IRabbitMQPersistentConnection : IDisposable
{
bool IsConnected { get; }
bool TryConnect();
IModel CreateModel();
}
public class RabbitMQPersistentConnection : IRabbitMQPersistentConnection
{
readonly RabbitMQEventBusSettings _settings;
readonly IConnectionFactory _connectionFactory;
IConnection _connection;
bool _disposed;
object sync_root = new object();
public RabbitMQPersistentConnection( IConnectionFactory connectionFactory, RabbitMQEventBusSettings settings )
{
_connectionFactory = connectionFactory;
_settings = settings;
}
public bool IsConnected => _connection != null && _connection.IsOpen && !_disposed;
public IModel CreateModel()
{
if ( !IsConnected )
{
throw new InvalidOperationException( "No RabbitMQ connections are available to perform this action" );
}
return _connection.CreateModel();
}
public void Dispose()
{
if ( _disposed ) return;
_disposed = true;
try
{
_connection?.Dispose();
}
catch ( IOException )
{
}
}
public bool TryConnect()
{
lock ( sync_root )
{
var policy = RetryPolicy.Handle<SocketException>().Or<BrokerUnreachableException>()
.WaitAndRetry( _settings.ConnectionRetryCount, retryAttempt => TimeSpan.FromSeconds( Math.Pow( 2, retryAttempt ) ), ( ex, time ) =>
{
}
);
policy.Execute( () =>
{
_connection = _connectionFactory.CreateConnection();
} );
if ( IsConnected )
{
return true;
}
else
{
return false;
}
}
}
public static ConnectionFactory CreateConnectionFactory( RabbitMQConnectionSettings settings )
{
var connectionFactory = new ConnectionFactory
{
AutomaticRecoveryEnabled = true,
HostName = settings.HostName,
UserName = settings.UserName,
Password = settings.Password,
VirtualHost = settings.VirtualHost
};
//Взял из реализации в masstransit
if ( settings.UseSsl )
{
connectionFactory.Ssl.Enabled = true;
connectionFactory.Ssl.ServerName = string.Empty;
connectionFactory.Ssl.CertPath = string.Empty;
connectionFactory.Ssl.CertPassphrase = string.Empty;
connectionFactory.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateChainErrors | SslPolicyErrors.RemoteCertificateNameMismatch;
connectionFactory.Ssl.Version = SslProtocols.Tls12;
}
return connectionFactory;
}
}
}