public void CloseAll() { foreach (var channel in _channels) { try { if (channel != ConsumerChannelRegistry.GetConsumerChannel()) { channel.Close(); } else { _logger?.LogDebug("Skipping close of consumer channel: {channel} ", channel); } } catch (Exception ex) { _logger?.LogDebug(ex, "Could not close synchronized Rabbit Channel after transaction"); } } foreach (var con in _connections) { RabbitUtils.CloseConnection(con); } _connections.Clear(); _channels.Clear(); _channelsPerConnection.Clear(); }
private static RabbitResourceHolder DoGetTransactionalResourceHolder(IConnectionFactory connectionFactory, IResourceFactory resourceFactory) { if (connectionFactory == null) { throw new ArgumentNullException(nameof(connectionFactory)); } if (resourceFactory == null) { throw new ArgumentNullException(nameof(resourceFactory)); } var resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory); if (resourceHolder != null) { var model = resourceFactory.GetChannel(resourceHolder); if (model != null) { return(resourceHolder); } } var resourceHolderToUse = resourceHolder; if (resourceHolderToUse == null) { resourceHolderToUse = new RabbitResourceHolder(); } var connection = resourceFactory.GetConnection(resourceHolderToUse); IModel channel; try { /* * If we are in a listener container, first see if there's a channel registered * for this consumer and the consumer is using the same connection factory. */ channel = ConsumerChannelRegistry.GetConsumerChannel(connectionFactory); if (channel == null && connection == null) { connection = resourceFactory.CreateConnection2(); if (resourceHolder == null) { /* * While creating a connection, a connection listener might have created a * transactional channel and bound it to the transaction. */ resourceHolder = (RabbitResourceHolder)TransactionSynchronizationManager.GetResource(connectionFactory); if (resourceHolder != null) { channel = resourceHolder.GetChannel(); resourceHolderToUse = resourceHolder; } } resourceHolderToUse.AddConnection(connection); } if (channel == null) { channel = resourceFactory.CreateChannel(connection); } resourceHolderToUse.AddChannel(channel, connection); if (!resourceHolderToUse.Equals(resourceHolder)) { BindResourceToTransaction(resourceHolderToUse, connectionFactory, resourceFactory.IsSynchedLocalTransactionAllowed); } return(resourceHolderToUse); } catch (IOException ex) { RabbitUtils.CloseConnection(connection); throw new AmqpIOException(ex); } }