private static async Task SendMessages(ConfirmsAwareChannel senderChannel, string inputQueue, CancellationToken cancellationToken) { var messageNumber = 0; while (!cancellationToken.IsCancellationRequested) { var properties = senderChannel.CreateBasicProperties(); await senderChannel.SendMessage(inputQueue, messageNumber ++.ToString(), properties); await Task.Delay(450, cancellationToken); } }
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 receiveChannel = receiveConnection.CreateModel(); #endregion receiveChannel.BasicQos(prefetchSize: 0, prefetchCount: 10, global: false); var consumer = new EventingBasicConsumer(receiveChannel); #region NotRelevant consumer.Registered += Consumer_Registered; receiveConnection.ConnectionShutdown += Connection_ConnectionShutdown; #endregion consumer.Received += (sender, args) => Consumer_Received(sender, args, receiveChannel); 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 }