Exemple #1
0
        private async Task Producer(CancellationToken cancellationToken)
        {
            int producerCount = _cmdOptions.Count > 0 ? _cmdOptions.Count : 1;

            try
            {
                for (int i = 0; i < producerCount; i++)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    try
                    {
                        var messageData = new BusinessMessage
                        {
                            MessageId  = i.ToString(),
                            Content    = $"我是内容_{i}",
                            CreateTime = DateTime.Now
                        };
                        await _messageBus.PublishAsync(messageData);

                        // await _messageBus.PublishDelayAsync(messageData,TimeSpan.FromSeconds(7));
                        _logger.LogInformation($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}生产数据:MessageId={messageData.MessageId}");
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex, "生产消息出错");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "");
            }
        }
Exemple #2
0
 public static Task PublishAsync <T>(this IRabbitMQMessageBus messageBus, T message)
 {
     return(messageBus.PublishAsync(typeof(T), message));
 }