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(8)); //await _messageBus.PublishCrontabAsync(messageData, new CrontabJobInfo //{ // JobId = "1", // JobName = "定时统计商品", // CrontabExpression = "0/1 * * * * *", // Status = CrontabJobStatus.Enabled //}); _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, ""); } }
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(5)); _logger.LogInformation($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff")}生产数据:MessageId={messageData.MessageId}"); //await Task.Delay(5); } catch (Exception ex) { _logger.LogError(ex, "生产消息出错"); throw; } } } catch (Exception ex) { _logger.LogError(ex, ""); } }
public static Task PublishAsync <T>(this IRedisMessageBus messageBus, T message) { return(messageBus.PublishAsync(typeof(T), message)); }