private IConnection CreateNewConnection(string brokerName) { IConnection connection; RabbitMQConnectionSetting connectionSetting = _options.GetConnectionSetting(brokerName); if (connectionSetting == null) { Exception ex = new Exception($"无法找到RabbitMQ ,没有对应broker名字的配置,brokername:{brokerName}"); _logger.LogCritical(ex, $"RabbitMQ 配置不对,找不到brokername为{brokerName}的配置"); throw ex; } //TODO: add polly here, make sure the connection is established, or just keep trying //TODO: add log here ConnectionFactory connectionFactory = new ConnectionFactory { Uri = new Uri(connectionSetting.ConnectionString), NetworkRecoveryInterval = TimeSpan.FromSeconds(_options.NetworkRecoveryIntervalSeconds), AutomaticRecoveryEnabled = true }; connection = connectionFactory.CreateConnection(); connection.CallbackException += Connection_CallbackException; connection.ConnectionBlocked += Connection_ConnectionBlocked; connection.ConnectionUnblocked += Connection_ConnectionUnblocked; connection.RecoverySucceeded += Connection_RecoverySucceeded; connection.ConnectionRecoveryError += Connection_ConnectionRecoveryError; connection.ConnectionShutdown += Connection_ConnectionShutdown; return(connection); }
public async Task <bool> PublishAsync(string brokerName, EventMessage eventMessage) { //大量Request线程放入缓存池中,离开 //缓存池内容不能丢,所以用抗击打的Redis来存储 //注意取消息后需要从kvstore删除 if (!IsBrokerExists(brokerName)) { throw new Exception($"Not exist rabbit broker:{brokerName}"); } EventMessageEntity eventEntity = new EventMessageEntity(eventMessage.Type, eventMessage.JsonData); RabbitMQConnectionSetting connectionSetting = _options.GetConnectionSetting(brokerName); //推送到一个以broker命名的队列中 await _redis.PushAsync(redisInstanceName : connectionSetting.RedisInstanceName, queueName : brokerName, data : eventEntity); //NotifyPublishToRabbitMQ(brokerName); return(true); }