|
| 1 | +// See https://aka.ms/new-console-template for more information |
| 2 | + |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text; |
| 5 | +using RabbitMQ.AMQP.Client; |
| 6 | +using RabbitMQ.AMQP.Client.Impl; |
| 7 | +using Trace = Amqp.Trace; |
| 8 | +using TraceLevel = Amqp.TraceLevel; |
| 9 | + |
| 10 | +Trace.TraceLevel = TraceLevel.Information; |
| 11 | + |
| 12 | +ConsoleTraceListener consoleListener = new(); |
| 13 | +Trace.TraceListener = (l, f, a) => |
| 14 | + consoleListener.WriteLine($"[{DateTime.Now}] [{l}] - {f}"); |
| 15 | + |
| 16 | +Trace.WriteLine(TraceLevel.Information, "Starting the example..."); |
| 17 | +const string containerId = "sql-filter-example"; |
| 18 | + |
| 19 | +IEnvironment environment = AmqpEnvironment.Create( |
| 20 | + ConnectionSettingsBuilder.Create().ContainerId(containerId).Build()); |
| 21 | + |
| 22 | +IConnection connection = await environment.CreateConnectionAsync().ConfigureAwait(false); |
| 23 | + |
| 24 | +Trace.WriteLine(TraceLevel.Information, $"Connected to the broker {connection} successfully"); |
| 25 | + |
| 26 | +// ------------------------------------------------------------------------------------ |
| 27 | +// The management object is used to declare/delete queues, exchanges, and bindings |
| 28 | +IManagement management = connection.Management(); |
| 29 | +const string queueName = "q_amqp10-client-stream-filter-test"; |
| 30 | +IQueueSpecification queueSpec = management.Queue(queueName).Type(QueueType.STREAM); |
| 31 | +await queueSpec.DeclareAsync().ConfigureAwait(false); |
| 32 | +Trace.WriteLine(TraceLevel.Information, |
| 33 | + $"Queue {queueName} declared successfully"); |
| 34 | +// ------------------------------------------------------------------------------------ |
| 35 | + |
| 36 | +// ------------------------------------------------------------------------------------ |
| 37 | + |
| 38 | +const int total = 10; |
| 39 | +IPublisher publisher = await connection.PublisherBuilder().Queue(queueName).BuildAsync().ConfigureAwait(false); |
| 40 | +for (int i = 0; i < total; i++) |
| 41 | +{ |
| 42 | + string subject = i % 2 == 0 ? "Order" : "Invoice"; |
| 43 | + string region = i % 2 == 0 ? "emea" : "us"; |
| 44 | + IMessage message = new AmqpMessage(Encoding.UTF8.GetBytes($"Hello Filter {i}")) |
| 45 | + .Subject(subject) |
| 46 | + .Property("region", region); |
| 47 | + var pr = await publisher.PublishAsync(message).ConfigureAwait(false); |
| 48 | + |
| 49 | + switch (pr.Outcome.State) |
| 50 | + { |
| 51 | + case OutcomeState.Accepted: |
| 52 | + Trace.WriteLine(TraceLevel.Information, $"[Publisher] Message: {message.BodyAsString()} confirmed"); |
| 53 | + break; |
| 54 | + case OutcomeState.Released: |
| 55 | + Trace.WriteLine(TraceLevel.Information, $"[Publisher] Message: {message.BodyAsString()} Released"); |
| 56 | + break; |
| 57 | + |
| 58 | + case OutcomeState.Rejected: |
| 59 | + Trace.WriteLine(TraceLevel.Error, |
| 60 | + $"[Publisher] Message: {message.BodyAsString()} Rejected with error: {pr.Outcome.Error}"); |
| 61 | + break; |
| 62 | + default: |
| 63 | + throw new ArgumentOutOfRangeException(); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// ------------------------------------------------------------------------------------ |
| 68 | +IConsumer consumer = await connection.ConsumerBuilder().Queue(queueName).MessageHandler((context, message) => |
| 69 | + { |
| 70 | + Trace.WriteLine(TraceLevel.Information, |
| 71 | + $"[Consumer Received] Message Body: [{message.BodyAsString()}], " + |
| 72 | + $"Subject: [{message.Subject()}], Property: [{message.Property("region")}]"); |
| 73 | + context.Accept(); |
| 74 | + return Task.CompletedTask; |
| 75 | + } |
| 76 | + ).Stream().Offset(StreamOffsetSpecification.First).Filter().Subject("&p:Order").Property("region", "emea").Stream() |
| 77 | + .Builder() |
| 78 | + .BuildAndStartAsync().ConfigureAwait(false); |
| 79 | +// close the above consumer to demonstrate SQL filter |
| 80 | +Thread.Sleep(1000); // wait a bit to show the difference in output |
| 81 | + |
| 82 | +// ------------------------------------------------------------------------------------ |
| 83 | +// the same like above but using SQL filter example |
| 84 | + |
| 85 | +IConsumer sqlConsumer = await connection.ConsumerBuilder().Queue(queueName).MessageHandler((context, message) => |
| 86 | + { |
| 87 | + Trace.WriteLine(TraceLevel.Information, |
| 88 | + $"[SQL Consumer Received] Message Body: [{message.BodyAsString()}], " + |
| 89 | + $"Subject: [{message.Subject()}], Property: [{message.Property("region")}]"); |
| 90 | + context.Accept(); |
| 91 | + return Task.CompletedTask; |
| 92 | + } |
| 93 | + ).Stream().Offset(StreamOffsetSpecification.First).Filter() |
| 94 | + .Sql("properties.subject LIKE 'Order%' AND region = 'emea'").Stream().Builder() |
| 95 | + .BuildAndStartAsync().ConfigureAwait(false); |
| 96 | + |
| 97 | +Console.WriteLine("Press [enter] to exit."); |
| 98 | +Console.ReadLine(); |
| 99 | +await consumer.CloseAsync().ConfigureAwait(false); |
| 100 | +await sqlConsumer.CloseAsync().ConfigureAwait(false); |
| 101 | +await publisher.CloseAsync().ConfigureAwait(false); |
| 102 | +await queueSpec.DeleteAsync().ConfigureAwait(false); |
| 103 | +await connection.CloseAsync().ConfigureAwait(false); |
| 104 | +Trace.WriteLine(TraceLevel.Information, "Connection closed"); |
0 commit comments