|
| 1 | +// See https://aka.ms/new-console-template for more information |
| 2 | + |
| 3 | +using System.Diagnostics; |
| 4 | +using Amqp; |
| 5 | +using RabbitMQ.AMQP.Client; |
| 6 | +using RabbitMQ.AMQP.Client.Impl; |
| 7 | +using IConnection = RabbitMQ.AMQP.Client.IConnection; |
| 8 | +using Trace = Amqp.Trace; |
| 9 | +using TraceLevel = Amqp.TraceLevel; |
| 10 | + |
| 11 | +Trace.TraceLevel = TraceLevel.Information; |
| 12 | + |
| 13 | +ConsoleTraceListener consoleListener = new(); |
| 14 | +Trace.TraceListener = (l, f, a) => |
| 15 | + consoleListener.WriteLine($"[{DateTime.Now}] [{l}] - {f}"); |
| 16 | + |
| 17 | +Trace.WriteLine(TraceLevel.Information, "Starting the example..."); |
| 18 | +const string containerId = "dispositions-id"; |
| 19 | + |
| 20 | +IEnvironment environment = AmqpEnvironment.Create( |
| 21 | + ConnectionSettingsBuilder.Create().ContainerId(containerId).Build()); |
| 22 | + |
| 23 | +IConnection connection = await environment.CreateConnectionAsync().ConfigureAwait(false); |
| 24 | + |
| 25 | +Trace.WriteLine(TraceLevel.Information, $"Connected to the broker {connection} successfully"); |
| 26 | + |
| 27 | +IManagement management = connection.Management(); |
| 28 | +const string queueName = "dispositions"; |
| 29 | +IQueueSpecification queueSpec = management.Queue(queueName).Type(QueueType.QUORUM); |
| 30 | +await queueSpec.DeclareAsync().ConfigureAwait(false); |
| 31 | + |
| 32 | +IPublisher publisher = await connection.PublisherBuilder().Queue(queueName) |
| 33 | + .BuildAsync().ConfigureAwait(false); |
| 34 | + |
| 35 | +BatchDeliveryContext batchContext = new(); |
| 36 | +IConsumer consumer = await connection.ConsumerBuilder().Queue(queueName).MessageHandler( |
| 37 | + (context, message) => |
| 38 | + { |
| 39 | + Trace.WriteLine(TraceLevel.Information, $"[Consumer] Message: {message.BodyAsString()} received"); |
| 40 | + batchContext.Add(context); |
| 41 | + if (batchContext.Count() >= 10) |
| 42 | + { |
| 43 | + Trace.WriteLine(TraceLevel.Information, "[Consumer] Committing batch of messages"); |
| 44 | + // here the batch is committed, all messages in the batch will be accepted |
| 45 | + // the contexts will be disposed and deleted after the batch is committed |
| 46 | + batchContext.Accept(); |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + Trace.WriteLine(TraceLevel.Information, "[Consumer] Adding message to batch"); |
| 51 | + } |
| 52 | + return Task.CompletedTask; |
| 53 | + } |
| 54 | +).BuildAndStartAsync().ConfigureAwait(false); |
| 55 | + |
| 56 | +const int total = 10; |
| 57 | +for (int i = 0; i < total; i++) |
| 58 | +{ |
| 59 | + string body = $"Message {i}"; |
| 60 | + Trace.WriteLine(TraceLevel.Information, $"[Publisher] Publishing message: {body}"); |
| 61 | + var message = new AmqpMessage($"Hello World_{i}"); |
| 62 | + await publisher.PublishAsync(message).ConfigureAwait(false); |
| 63 | + // ignoring the publish result for this example |
| 64 | +} |
| 65 | + |
| 66 | +Console.WriteLine("Press any key to delete the queue and close the environment."); |
| 67 | +Console.ReadKey(); |
| 68 | + |
| 69 | +await publisher.CloseAsync().ConfigureAwait(false); |
| 70 | +await consumer.CloseAsync().ConfigureAwait(false); |
| 71 | +await queueSpec.DeleteAsync().ConfigureAwait(false); |
| 72 | +await environment.CloseAsync().ConfigureAwait(false); |
0 commit comments