예제 #1
0
        public async Task <IDisposable> SubscribeAsync <TMessage>(Func <MessagingEnvelope <TMessage>, Task> handler,
                                                                  MessagingSubscriberOptions options  = null,
                                                                  CancellationToken cancellationToken = default)
        {
            var topicName = _topicRegistry.GetTopicForName(options?.TopicName) ??
                            _topicRegistry.GetTopicForMessageType(typeof(TMessage));

            async Task MsgHandler(byte[] messageData)
            {
                _logger.LogDebug("Messaging subscriber received message from subject {Subject}", topicName);

                try
                {
                    var messageEnvelope = _messageSerDes.DeserializeMessageEnvelope <TMessage>(messageData);

                    await handler(messageEnvelope);
                }
                catch (Exception ex)
                {
                    _logger.LogError(
                        "Messaging consumer encountered an error when handling a message from subject {Subject}.\n {Error}",
                        topicName, ex);

                    //TODO: push to DLQ
                }
            }

            return(await _messagingTransport.SubscribeAsync(topicName, MsgHandler, options?.Transport, cancellationToken));
        }
예제 #2
0
        public Task SubscribeAsync(Func <MessagingEnvelope <TMessage>, Task> handler, CancellationToken cancellationToken = default, string topicName = null, MessagingSubscriberOptions options = null)
        {
            _handlers.Add(handler);

            if (!_subscribedToTopic)
            {
                lock (lockObj)
                {
                    if (!_subscribedToTopic)
                    {
                        _subscribedToTopic = true;
                        _topicName         = _topicRegistry.GetTopicForName(topicName) ??
                                             _topicRegistry.GetTopicForMessageType(typeof(TMessage));
                        _subscriberOptions = options;
                        _topicSubscriber.SubscribeAsync(_topicName, HandleMessage, cancellationToken, options);
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #3
0
 public Task <IDisposable> SubscribeAsync <TMessage>(Func <MessagingEnvelope <TMessage>, Task> handler,
                                                     MessagingSubscriberOptions options  = null,
                                                     CancellationToken cancellationToken = default)
 => _busSubscriber.SubscribeAsync(handler, options, cancellationToken);