예제 #1
0
    public async Task HandleApplicationMessageReceivedAsync(MqttApplicationMessageReceivedEventArgs eventArgs)
    {
        var consumeResult = new ConsumeMessageResult(_topics);

        consumeResult.SetMessageAsync(eventArgs.ApplicationMessage);
        consumeResult.PublishToDomainAsync(_serviceProvider, _logger, CancellationToken.None);
    }
예제 #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            using var consumer = MakeConsumer();
            consumer.Subscribe(_topics.Keys);

            using var scope = _services.CreateScope();
            var mediator = scope.ServiceProvider.GetRequiredService <IMicroMediator>();

            while (!stoppingToken.IsCancellationRequested)
            {
                try
                {
                    var consumeResult = new ConsumeMessageResult(_topics);

                    if (!consumer.TryConsumeMessage(_connection.MillisecondsTimeout, consumeResult, stoppingToken))
                    {
                        // _logger.LogTrace($"{consumer.Name}:{consumer.MemberId} is empty. Sleep for {_connection.MillisecondsTimeout}MS");

                        await Task.Delay(1, stoppingToken);

                        // TimeTrackerCollection.Append(consumeResult.GetTimes());
                        // TimeTrackerCollection.Print();

                        continue;
                    }

                    // var sw = Stopwatch.StartNew();

                    _logger.LogTrace($"{consumer.Name} - Pull Message.TP:{consumeResult.Result.TopicPartition.Topic}:{consumeResult.Result.TopicPartition.Partition}, Offset:{consumeResult.Result.Offset.Value}");

                    var processTask = consumeResult.PublishToDomainAsync(mediator, _logger, stoppingToken);

                    consumer.StoreOffset(consumeResult.Result);
                    // sw.Stop();

                    // consumeResult.SetProcessTime(sw.Elapsed.TotalMilliseconds);
                }
                catch (OperationCanceledException ex) when(stoppingToken.IsCancellationRequested)
                {
                    _logger.LogError(ex,
                                     "Error occurred executing {WorkItem}.", nameof(_connection));
                    if (stoppingToken.IsCancellationRequested)
                    {
                        throw;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex,
                                     "Error occurred executing {WorkItem}.", nameof(_connection));
                }
            }

            consumer.Close();

            _logger.LogWarning(
                $"Stopping conusmer request has been raised => IsCancellationRequested={stoppingToken.IsCancellationRequested}");
        }
예제 #3
0
    private void HandleApplicationMessageReceivedAsync(RedisChannel channel, RedisValue value)
    {
        var consumeResult = new ConsumeMessageResult(_topics);
        var message       = new RedisMessage()
        {
            Payload = value,
            Topic   = channel
        };

        consumeResult.SetMessageAsync(message);
        consumeResult.PublishToDomainAsync(_serviceProvider, _logger, CancellationToken.None);
    }