/
MqttMessageQueue.go
641 lines (538 loc) · 18.6 KB
/
MqttMessageQueue.go
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
package queues
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
mqtt "github.com/eclipse/paho.mqtt.golang"
cconf "github.com/pip-services3-go/pip-services3-commons-go/config"
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
cref "github.com/pip-services3-go/pip-services3-commons-go/refer"
clog "github.com/pip-services3-go/pip-services3-components-go/log"
cqueues "github.com/pip-services3-go/pip-services3-messaging-go/queues"
connect "github.com/pip-services3-go/pip-services3-mqtt-go/connect"
)
/*
MqttMessageQueue are message queue that sends and receives messages via MQTT message broker.
Configuration parameters:
- topic: name of MQTT topic to subscribe
- connection(s):
- discovery_key: (optional) a key to retrieve the connection from IDiscovery
- host: host name or IP address
- port: port number
- uri: resource URI or connection string with all parameters in it
- credential(s):
- store_key: (optional) a key to retrieve the credentials from ICredentialStore
- username: user name
- password: user password
- options:
- serialize_envelope: (optional) true to serialize entire message as JSON, false to send only message payload (default: true)
- autosubscribe: (optional) true to automatically subscribe on option (default: false)
- qos: (optional) quality of service level aka QOS (default: 0)
- retain: (optional) retention flag for published messages (default: false)
- retry_connect: (optional) turns on/off automated reconnect when connection is log (default: true)
- connect_timeout: (optional) number of milliseconds to wait for connection (default: 30000)
- reconnect_timeout: (optional) number of milliseconds to wait on each reconnection attempt (default: 1000)
- keepalive_timeout: (optional) number of milliseconds to ping broker while inactive (default: 3000)
References:
- *:logger:*:*:1.0 (optional) ILogger components to pass log messages
- *:counters:*:*:1.0 (optional) ICounters components to pass collected measurements
- *:discovery:*:*:1.0 (optional) IDiscovery services to resolve connections
- *:credential-store:*:*:1.0 (optional) Credential stores to resolve credentials
- *:connection:mqtt:*:1.0 (optional) Shared connection to MQTT service
See MessageQueue
See MessagingCapabilities
Example:
queue := NewMqttMessageQueue("myqueue")
queue.Configure(cconf.NewConfigParamsFromTuples(
"subject", "mytopic",
"connection.protocol", "mqtt"
"connection.host", "localhost"
"connection.port", 1883
))
queue.open("123")
queue.Send("123", NewMessageEnvelope("", "mymessage", "ABC"))
message, err := queue.Receive("123")
if (message != nil) {
...
queue.Complete("123", message);
}
*/
type MqttMessageQueue struct {
cqueues.MessageQueue
defaultConfig *cconf.ConfigParams
config *cconf.ConfigParams
references cref.IReferences
opened bool
localConnection bool
//The dependency resolver.
DependencyResolver *cref.DependencyResolver
//The logger.
Logger *clog.CompositeLogger
//The MQTT connection component.
Connection *connect.MqttConnection
serializeEnvelope bool
topic string
qos byte
retain bool
autoSubscribe bool
subscribed bool
messages []cqueues.MessageEnvelope
receiver cqueues.IMessageReceiver
}
// Creates a new instance of the queue component.
// - name (optional) a queue name.
func NewMqttMessageQueue(name string) *MqttMessageQueue {
c := MqttMessageQueue{
defaultConfig: cconf.NewConfigParamsFromTuples(
"topic", nil,
"options.autosubscribe", false,
"options.serialize_envelope", false,
"options.retry_connect", true,
"options.connect_timeout", 30000,
"options.reconnect_timeout", 1000,
"options.keepalive_timeout", 60000,
"options.qos", 0,
"options.retain", false,
),
Logger: clog.NewCompositeLogger(),
}
c.MessageQueue = *cqueues.InheritMessageQueue(&c, name,
cqueues.NewMessagingCapabilities(false, true, true, true, true, false, false, false, true))
c.DependencyResolver = cref.NewDependencyResolver()
c.DependencyResolver.Put("connection", cref.NewDescriptor("pip-services", "connection", "mqtt", "*", "1.0"))
c.DependencyResolver.Configure(c.defaultConfig)
c.messages = make([]cqueues.MessageEnvelope, 0)
return &c
}
// Configures component by passing configuration parameters.
// - config configuration parameters to be set.
func (c *MqttMessageQueue) Configure(config *cconf.ConfigParams) {
config = config.SetDefaults(c.defaultConfig)
c.config = config
c.DependencyResolver.Configure(config)
c.topic = config.GetAsStringWithDefault("topic", c.topic)
c.serializeEnvelope = config.GetAsBooleanWithDefault("options.serialize_envelope", c.serializeEnvelope)
c.autoSubscribe = config.GetAsBooleanWithDefault("options.autosubscribe", c.autoSubscribe)
c.qos = byte(config.GetAsIntegerWithDefault("options.qos", int(c.qos)))
c.retain = config.GetAsBooleanWithDefault("options.retain", c.retain)
}
// Sets references to dependent components.
// - references references to locate the component dependencies.
func (c *MqttMessageQueue) SetReferences(references cref.IReferences) {
c.references = references
c.Logger.SetReferences(references)
// Get connection
c.DependencyResolver.SetReferences(references)
result := c.DependencyResolver.GetOneOptional("connection")
if dep, ok := result.(*connect.MqttConnection); ok {
c.Connection = dep
}
// Or create a local one
if c.Connection == nil {
c.Connection = c.createConnection()
c.localConnection = true
} else {
c.localConnection = false
}
}
// Unsets (clears) previously set references to dependent components.
func (c *MqttMessageQueue) UnsetReferences() {
c.Connection = nil
}
func (c *MqttMessageQueue) createConnection() *connect.MqttConnection {
connection := connect.NewMqttConnection()
if c.config != nil {
connection.Configure(c.config)
}
if c.references != nil {
connection.SetReferences(c.references)
}
return connection
}
// Checks if the component is opened.
// Returns true if the component has been opened and false otherwise.
func (c *MqttMessageQueue) IsOpen() bool {
return c.opened
}
// Opens the component.
// - correlationId (optional) transaction id to trace execution through call chain.
// - Returns error or nil no errors occured.
func (c *MqttMessageQueue) Open(correlationId string) (err error) {
if c.opened {
return nil
}
if c.Connection == nil {
c.Connection = c.createConnection()
c.localConnection = true
}
if c.localConnection {
err = c.Connection.Open(correlationId)
}
if err == nil && c.Connection == nil {
err = cerr.NewInvalidStateError(correlationId, "NO_CONNECTION", "MQTT connection is missing")
}
if err == nil && !c.Connection.IsOpen() {
err = cerr.NewInvalidStateError(correlationId, "CONNECT_FAILED", "MQTT connection is not opened")
}
if err != nil {
return err
}
// Automatically subscribe if needed
if c.autoSubscribe {
err = c.subscribe(correlationId)
if err != nil {
return err
}
}
c.opened = true
return err
}
// Closes component and frees used resources.
// - correlationId (optional) transaction id to trace execution through call chain.
// - Returns error or nil no errors occured.
func (c *MqttMessageQueue) Close(correlationId string) (err error) {
if !c.opened {
return nil
}
if c.Connection == nil {
return cerr.NewInvalidStateError(correlationId, "NO_CONNECTION", "MQTT connection is missing")
}
if c.localConnection {
err = c.Connection.Close(correlationId)
}
if err != nil {
return err
}
// Unsubscribe from topic
if c.subscribed {
topic := c.getTopic()
c.Connection.Unsubscribe(topic, c)
c.subscribed = false
}
c.Lock.Lock()
defer c.Lock.Unlock()
c.opened = false
c.receiver = nil
c.messages = make([]cqueues.MessageEnvelope, 0)
return nil
}
func (c *MqttMessageQueue) getTopic() string {
if c.topic != "" {
return c.topic
}
return c.Name()
}
func (c *MqttMessageQueue) subscribe(correlationId string) error {
c.Lock.Lock()
defer c.Lock.Unlock()
// Check if already were subscribed
if c.subscribed {
return nil
}
// Subscribe to the topic
topic := c.getTopic()
err := c.Connection.Subscribe(topic, c.qos, c)
if err != nil {
c.Logger.Error(correlationId, err, "Failed to subscribe to topic "+topic)
return err
}
c.subscribed = true
return nil
}
func (c *MqttMessageQueue) fromMessage(message *cqueues.MessageEnvelope) ([]byte, error) {
if message == nil {
return nil, nil
}
data := message.Message
if c.serializeEnvelope {
message.SentTime = time.Now()
var err error
data, err = json.Marshal(message)
if err != nil {
return nil, err
}
}
return data, nil
}
func (c *MqttMessageQueue) toMessage(msg mqtt.Message) (*cqueues.MessageEnvelope, error) {
message := cqueues.NewEmptyMessageEnvelope()
if c.serializeEnvelope {
err := json.Unmarshal(msg.Payload(), message)
if err != nil {
return nil, err
}
} else {
message.MessageId = strconv.FormatUint(uint64(msg.MessageID()), 10)
message.MessageType = msg.Topic()
message.Message = msg.Payload()
}
message.SetReference(msg)
return message, nil
}
func (c *MqttMessageQueue) OnMessage(msg mqtt.Message) {
// Skip if it came from a wrong topic
expectedTopic := c.getTopic()
if !strings.Contains(expectedTopic, "*") && expectedTopic != msg.Topic() {
return
}
// Deserialize message
message, err := c.toMessage(msg)
if message == nil || err != nil {
c.Logger.Error("", err, "Failed to read received message")
return
}
c.Counters.IncrementOne("queue." + c.Name() + ".received_messages")
c.Logger.Debug(message.CorrelationId, "Received message %s via %s", message, c.Name())
// Send message to receiver if its set or put it into the queue
c.Lock.Lock()
if c.receiver != nil {
receiver := c.receiver
c.Lock.Unlock()
c.sendMessageToReceiver(receiver, message)
} else {
c.messages = append(c.messages, *message)
c.Lock.Unlock()
}
}
// Clear method are clears component state.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
// Returns error or nil no errors occured.
func (c *MqttMessageQueue) Clear(correlationId string) error {
c.Lock.Lock()
defer c.Lock.Unlock()
c.messages = make([]cqueues.MessageEnvelope, 0)
return nil
}
// ReadMessageCount method are reads the current number of messages in the queue to be delivered.
// Returns number of messages or error.
func (c *MqttMessageQueue) ReadMessageCount() (int64, error) {
c.Lock.Lock()
defer c.Lock.Unlock()
count := (int64)(len(c.messages))
return count, nil
}
// Peek method are peeks a single incoming message from the queue without removing it.
// If there are no messages available in the queue it returns nil.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
// Returns: result *cqueues.MessageEnvelope, err error
// message or error.
func (c *MqttMessageQueue) Peek(correlationId string) (*cqueues.MessageEnvelope, error) {
err := c.CheckOpen(correlationId)
if err != nil {
return nil, err
}
// Subscribe if needed
err = c.subscribe(correlationId)
if err != nil {
return nil, err
}
var message *cqueues.MessageEnvelope
// Pick a message
c.Lock.Lock()
if len(c.messages) > 0 {
message = &c.messages[0]
}
c.Lock.Unlock()
if message != nil {
c.Logger.Trace(message.CorrelationId, "Peeked message %s on %s", message, c.String())
}
return message, nil
}
// PeekBatch method are peeks multiple incoming messages from the queue without removing them.
// If there are no messages available in the queue it returns an empty list.
// Important: This method is not supported by MQTT.
// Parameters:
// - correlationId (optional) transaction id to trace execution through call chain.
// - messageCount a maximum number of messages to peek.
// Returns: callback function that receives a list with messages or error.
func (c *MqttMessageQueue) PeekBatch(correlationId string, messageCount int64) ([]*cqueues.MessageEnvelope, error) {
err := c.CheckOpen(correlationId)
if err != nil {
return nil, err
}
// Subscribe if needed
err = c.subscribe(correlationId)
if err != nil {
return nil, err
}
c.Lock.Lock()
batchMessages := c.messages
if messageCount <= (int64)(len(batchMessages)) {
batchMessages = batchMessages[0:messageCount]
}
c.Lock.Unlock()
messages := []*cqueues.MessageEnvelope{}
for _, message := range batchMessages {
messages = append(messages, &message)
}
c.Logger.Trace(correlationId, "Peeked %d messages on %s", len(messages), c.Name())
return messages, nil
}
// Receive method are receives an incoming message and removes it from the queue.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
// - waitTimeout time.Duration a timeout in milliseconds to wait for a message to come.
// Returns: result *cqueues.MessageEnvelope, err error
// receives a message or error.
func (c *MqttMessageQueue) Receive(correlationId string, waitTimeout time.Duration) (*cqueues.MessageEnvelope, error) {
err := c.CheckOpen(correlationId)
if err != nil {
return nil, err
}
// Subscribe if needed
err = c.subscribe(correlationId)
if err != nil {
return nil, err
}
messageReceived := false
var message *cqueues.MessageEnvelope
elapsedTime := time.Duration(0)
for elapsedTime < waitTimeout && !messageReceived {
c.Lock.Lock()
if len(c.messages) == 0 {
c.Lock.Unlock()
time.Sleep(time.Duration(100) * time.Millisecond)
elapsedTime += time.Duration(100)
continue
}
// Get message from the queue
message = &c.messages[0]
c.messages = c.messages[1:]
// Add messages to locked messages list
messageReceived = true
c.Lock.Unlock()
}
return message, nil
}
// Send method are sends a message into the queue.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
// - envelope *cqueues.MessageEnvelope a message envelop to be sent.
// Returns: error or nil for success.
func (c *MqttMessageQueue) Send(correlationId string, envelop *cqueues.MessageEnvelope) error {
err := c.CheckOpen(correlationId)
if err != nil {
return err
}
c.Counters.IncrementOne("queue." + c.Name() + ".sent_messages")
c.Logger.Debug(envelop.CorrelationId, "Sent message %s via %s", envelop.String(), c.Name())
msg, err := c.fromMessage(envelop)
if err != nil {
return err
}
topic := c.topic
if topic == "" {
topic = c.Name()
}
err = c.Connection.Publish(topic, c.qos, c.retain, msg)
if err != nil {
c.Logger.Error(envelop.CorrelationId, err, "Failed to send message via %s", c.Name())
return err
}
return nil
}
// RenewLock method are renews a lock on a message that makes it invisible from other receivers in the queue.
// This method is usually used to extend the message processing time.
// Important: This method is not supported by MQTT.
// Parameters:
// - message *cqueues.MessageEnvelope a message to extend its lock.
// - lockTimeout time.Duration a locking timeout in milliseconds.
// Returns: error
// receives an error or nil for success.
func (c *MqttMessageQueue) RenewLock(message *cqueues.MessageEnvelope, lockTimeout time.Duration) (err error) {
// Not supported
return nil
}
// Complete method are permanently removes a message from the queue.
// This method is usually used to remove the message after successful processing.
// Important: This method is not supported by MQTT.
// Parameters:
// - message *cqueues.MessageEnvelope a message to remove.
// Returns: error
// error or nil for success.
func (c *MqttMessageQueue) Complete(message *cqueues.MessageEnvelope) error {
// Not supported
return nil
}
// Abandon method are returnes message into the queue and makes it available for all subscribers to receive it again.
// This method is usually used to return a message which could not be processed at the moment
// to repeat the attempt. Messages that cause unrecoverable errors shall be removed permanently
// or/and send to dead letter queue.
// Important: This method is not supported by MQTT.
// Parameters:
// - message *cqueues.MessageEnvelope a message to return.
// Returns: error
// error or nil for success.
func (c *MqttMessageQueue) Abandon(message *cqueues.MessageEnvelope) error {
// Not supported
return nil
}
// Permanently removes a message from the queue and sends it to dead letter queue.
// Important: This method is not supported by MQTT.
// Parameters:
// - message *cqueues.MessageEnvelope a message to be removed.
// Returns: error
// error or nil for success.
func (c *MqttMessageQueue) MoveToDeadLetter(message *cqueues.MessageEnvelope) error {
// Not supported
return nil
}
func (c *MqttMessageQueue) sendMessageToReceiver(receiver cqueues.IMessageReceiver, message *cqueues.MessageEnvelope) {
correlationId := message.CorrelationId
defer func() {
if r := recover(); r != nil {
err := fmt.Sprintf("%v", r)
c.Logger.Error(correlationId, nil, "Failed to process the message - "+err)
}
}()
err := receiver.ReceiveMessage(message, c)
if err != nil {
c.Logger.Error(correlationId, err, "Failed to process the message")
}
}
// Listens for incoming messages and blocks the current thread until queue is closed.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
// - receiver cqueues.IMessageReceiver a receiver to receive incoming messages.
//
// See IMessageReceiver
// See receive
func (c *MqttMessageQueue) Listen(correlationId string, receiver cqueues.IMessageReceiver) error {
err := c.CheckOpen(correlationId)
if err != nil {
return err
}
// Subscribe if needed
err = c.subscribe(correlationId)
if err != nil {
return err
}
c.Logger.Trace("", "Started listening messages at %s", c.Name())
// Get all collected messages
c.Lock.Lock()
batchMessages := c.messages
c.messages = []cqueues.MessageEnvelope{}
c.Lock.Unlock()
// Resend collected messages to receiver
for _, message := range batchMessages {
receiver.ReceiveMessage(&message, c)
}
// Set the receiver
c.Lock.Lock()
c.receiver = receiver
c.Lock.Unlock()
return nil
}
// EndListen method are ends listening for incoming messages.
// When this method is call listen unblocks the thread and execution continues.
// Parameters:
// - correlationId string (optional) transaction id to trace execution through call chain.
func (c *MqttMessageQueue) EndListen(correlationId string) {
c.Lock.Lock()
c.receiver = nil
c.Lock.Unlock()
}