static async Task Main(string[] args) { var connectionFactory = new ConnectionFactory { HostName = "localhost", Port = 5672, UseBackgroundThreadsForIO = true, DispatchConsumersAsync = true, ProcessingConcurrency = 4, // rc.1 // ConsumerDispatchConcurrency = 4, final }; #region TopologyAndSending CreateTopologyIfNecessary(InputQueue, connectionFactory); var cts = new CancellationTokenSource(); var sendConnection = connectionFactory.CreateConnection($"{InputQueue} sender"); var senderChannel = new ConfirmsAwareChannel(sendConnection); var sendMessagesTask = Task.Run(() => SendMessages(senderChannel, InputQueue, cts.Token), CancellationToken.None); var receiveConnection = connectionFactory.CreateConnection($"{InputQueue} pump"); var receiveChannel = receiveConnection.CreateModel(); #endregion receiveChannel.BasicQos(prefetchSize: 0, prefetchCount: 10, global: false); var consumer = new AsyncEventingBasicConsumer(receiveChannel); #region NotRelevant consumer.Registered += Consumer_Registered; receiveConnection.ConnectionShutdown += Connection_ConnectionShutdown; #endregion consumer.Received += (sender, deliverEventArgs) => Consumer_Received(deliverEventArgs, receiveChannel, cts.Token); receiveChannel.BasicConsume(InputQueue, false, ConsumerTag, consumer); #region Stop await Console.Error.WriteLineAsync("Press any key to stop"); Console.ReadLine(); await Console.Error.WriteLineAsync("Shutting down"); cts.Cancel(); try { await sendMessagesTask; } catch (OperationCanceledException) { } receiveChannel.Close(); receiveConnection.Close(); senderChannel.Dispose(); sendConnection.Close(); #endregion }
static async Task Main(string[] args) { var connectionFactory = new ConnectionFactory { HostName = "localhost", Port = 5672, UseBackgroundThreadsForIO = true }; #region TopologyAndSending CreateTopologyIfNecessary(InputQueue, connectionFactory); var cts = new CancellationTokenSource(); var sendConnection = connectionFactory.CreateConnection($"{InputQueue} sender"); var senderChannel = new ConfirmsAwareChannel(sendConnection); var sendMessagesTask = Task.Run(() => SendMessages(senderChannel, InputQueue, cts.Token), CancellationToken.None); var receiveConnection = connectionFactory.CreateConnection($"{InputQueue} pump"); var receiveModel = receiveConnection.CreateModel(); TaskScheduler.UnobservedTaskException += (sender, args) => { }; #endregion receiveModel.BasicQos(prefetchSize: 0, prefetchCount: 10, global: false); var consumer = new EventingBasicConsumer(receiveModel); #region NotRelevant consumer.Registered += Consumer_Registered; receiveConnection.ConnectionShutdown += Connection_ConnectionShutdown; #endregion var exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler; var maxConcurrency = 4; var semaphore = new SemaphoreSlim(maxConcurrency); consumer.Received += (sender, deliverEventArgs) => Consumer_Received(deliverEventArgs, receiveModel, semaphore, exclusiveScheduler, cts.Token); receiveModel.BasicConsume(InputQueue, false, ConsumerTag, consumer); #region Stop await Console.Error.WriteLineAsync("Press any key to stop"); Console.ReadLine(); await Console.Error.WriteLineAsync("Shutting down"); cts.Cancel(); try { await sendMessagesTask; } catch (OperationCanceledException) { } while (semaphore.CurrentCount != maxConcurrency) { await Task.Delay(50).ConfigureAwait(false); } receiveModel.Close(); receiveConnection.Close(); senderChannel.Dispose(); sendConnection.Close(); #endregion }