-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefaultActiveMQPersistentConnection.cs
210 lines (183 loc) · 5.64 KB
/
DefaultActiveMQPersistentConnection.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using Apache.NMS;
using Apache.NMS.Util;
using System.Collections.Generic;
namespace EventBus.ActiveMQ
{
/// <summary>
/// https://activemq.apache.org/components/nms/examples/nms-simple-synchronous-consumer-example
/// https://activemq.apache.org/components/nms/examples/nms-simple-asynchronous-consumer-example
/// </summary>
public class DefaultActiveMQPersistentConnection : IActiveMQPersistentConnection
{
private readonly ILogger<DefaultActiveMQPersistentConnection> _logger;
private readonly IConnectionFactory _connectionFactory;
private readonly string _topic;
private readonly string _userName;
private readonly string _password;
IConnection _connection;
ISession _session;
IDestination _destination;
IMessageProducer _messageProducer;
IMessageConsumer _messageConsumer;
bool _disposed;
public DefaultActiveMQPersistentConnection(ILogger<DefaultActiveMQPersistentConnection> logger,
IDictionary<string, string> config = null)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
if(config == null)
{
config = new Dictionary<string, string>
{
["uri"] = "tcp://localhost:61616",
["topic"] = "test-topic",
["username"] = "admin",
["password"] = "admin",
};
}
_topic = config["topic"];
_userName = config["username"];
_password = config["password"];
var connecturi = new Uri(config["uri"]);
// Create WMQ Connection Factory.
_connectionFactory = new NMSConnectionFactory(connecturi);
}
private IConnection Connection
{
get
{
if(_connection == null)
{
// Create connection.
_connection = _connectionFactory.CreateConnection(userName: _userName, password: _password);
}
return _connection;
}
}
private ISession Session
{
get
{
if (_session == null)
{
// Create session
_session = Connection.CreateSession(AcknowledgementMode.AutoAcknowledge);
}
return _session;
}
}
private IDestination Destination
{
get
{
if (_destination == null)
{
// Create destination
///_destination = SessionUtil.GetDestination($"queue://{_queue}");
_destination = SessionUtil.GetDestination(Session, $"topic://{_topic}");
}
return _destination;
}
}
public (IMessageProducer, ISession) Producer
{
get
{
if(_messageProducer == null)
{
// Create producer
_messageProducer = Session.CreateProducer(Destination);
// Start the connection to receive messages.
Connection.Start();
}
return (_messageProducer, Session);
}
}
public IMessageConsumer Consumer
{
get
{
if(_messageConsumer == null)
{
// Create subscriber
_messageConsumer = Session.CreateConsumer(Destination);
// Start the connection to receive messages.
Connection.Start();
}
return _messageConsumer;
}
}
private void DisposeMessageConsumer()
{
try
{
_messageConsumer?.Close();
_messageConsumer = null;
}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}
private void DisposeMessageProducer()
{
try
{
_messageProducer?.Close();
_messageProducer = null;
}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}
private void DisposeDestination()
{
try
{
_destination?.Dispose();
_destination = null;
}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}
private void DisposeSession()
{
try
{
_session?.Dispose();
_session = null;
}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}
private void DisposeConnection()
{
try
{
_connection?.Close();
_connection = null;
}
catch (IOException ex)
{
_logger.LogCritical(ex.ToString());
}
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
DisposeMessageConsumer();
DisposeMessageProducer();
DisposeDestination();
DisposeSession();
DisposeConnection();
}
}
}