public void Declare(Exchange exchange, Queue queue, params RoutingKey[] routingKeys) { using (var channel = PublisherConnection.CreateModel()) { exchange.Declare(channel); queue.Declare(channel); foreach (var key in routingKeys) { queue.Bind(channel, exchange, key); } channel.Close(); } }
private void PublishBufferOnCleared(IEnumerable <BatchItem> removedItems) { var items = removedItems.ToList(); try { Policy .Handle <Exception>() .WaitAndRetry( _options.Value.PublishMaxRetry, _ => TimeSpan.FromMilliseconds(_options.Value.PublishRetryDelayInMilliseconds), (exception, span) => { _logger?.WriteException("Publisher", exception, new KeyValuePair <string, object>("Events", removedItems)); }) .Execute(() => { using var channel = PublisherConnection.CreateModel(); channel.ConfirmSelect(); var batch = channel.CreateBasicPublishBatch(); try { foreach (var group in items.GroupBy(x => (x.Exchange, x.Queue, x.RoutingKey))) { var(exchange, queue, routingKey) = @group.Key; exchange.Declare(channel); queue?.Declare(channel); queue?.Bind(channel, exchange, routingKey); foreach (var item in @group) { var(body, basicProperties) = item.Message.GetData(channel, _serializer); batch.Add(exchange.Name.Value, routingKey.Value, false, basicProperties, body); } } batch.Publish(); } finally { channel.WaitForConfirmsOrDie(); channel.Close(); } }); PublishSuccessed?.Invoke(items.AsReadOnly()); } catch (Exception ex) { _logger?.WriteException(nameof(PublishBufferOnCleared), ex, new KeyValuePair <string, object>("Events", items)); if (PublishFailed == null) { throw; } PublishFailed.Invoke(items.AsReadOnly(), ex); } }