public void Publish(IntegrationEvent @event) { if (!_persisterConnection.IsConnected) { _persisterConnection.TryConnect(); } var policy = RetryPolicy.Handle <BrokerUnreachableException>() .Or <SocketException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (ex, time) => { _logger.LogWarning(ex.ToString()); }); using (var channel = _persisterConnection.CreateModel()) { var eventName = @event.GetType() .Name; channel.ExchangeDeclare(exchange: BROKER_NAME, type: "direct"); var message = JsonConvert.SerializeObject(@event); var body = Encoding.UTF8.GetBytes(message); policy.Execute(() => { channel.BasicPublish(exchange: BROKER_NAME, routingKey: eventName, basicProperties: null, body: body); }); } }
/// <summary> /// 处理事件已删除事件 /// </summary> private void SubscriptionsManager_OnEventRemoved(object sender, string eventName) { //建立连接 if (!persisterConnection.IsConnected) { persisterConnection.TryConnect(); } using (var channel = persisterConnection.CreateModel()) { channel.QueueUnbind(queue: queueName, exchange: BROKER_NAME, routingKey: eventName); //没有事件订阅者 if (subscriptionsManager.IsEmpty) { queueName = ""; consumerChannel.Close(); } } }
private void _subsManager_OnEventRemoved(object sender, string eventName) { if (!_persistentConnection.IsConnected) { _persistentConnection.TryConnect(); } using (var channel = _persistentConnection.CreateModel()) { channel.QueueUnbind(queue: _queueName, exchange: BROKER_NAME, routingKey: eventName); if (_subsManager.IsEmpty) { _queueName = string.Empty; _consumerChannel.Close(); } } }
private void SubsManager_OnEventRemoved(object sender, string eventName) { if (!_persister.IsConnected) { _persister.TryConnect(); } using (var channel = _persister.CreateModel()) { channel.QueueUnbind(_queueName, BROKER_NAME, eventName); if (!_subscriptionManager.IsEmpty) { return; } _queueName = string.Empty; _consumerChannel.Close(); } }