From 2882bc7bfd125a07a497b5b42972681b0686bea4 Mon Sep 17 00:00:00 2001 From: sanych-sun Date: Tue, 3 Sep 2024 22:12:56 -0700 Subject: [PATCH] Examples clean up --- RabbitMQ.Next.sln | 7 ++ .../Program.cs | 29 +++---- .../Program.cs | 37 ++++++++ ...Q.Next.Examples.PublisherMiddleware.csproj | 16 ++++ .../Program.cs | 67 +++++++-------- .../Program.cs | 67 +++++---------- .../Program.cs | 85 +++++++------------ 7 files changed, 156 insertions(+), 152 deletions(-) create mode 100644 docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/Program.cs create mode 100644 docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/RabbitMQ.Next.Examples.PublisherMiddleware.csproj diff --git a/RabbitMQ.Next.sln b/RabbitMQ.Next.sln index c343ea4..2ba79f8 100644 --- a/RabbitMQ.Next.sln +++ b/RabbitMQ.Next.sln @@ -52,6 +52,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RabbitMQ.Next.Examples.Dyna EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RabbitMQ.Next.Examples.DemoSaslAuthMechanism", "docs\examples\RabbitMQ.Next.Examples.DemoSaslAuthMechanism\RabbitMQ.Next.Examples.DemoSaslAuthMechanism.csproj", "{C0440748-9787-4891-874A-054F5E64020B}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RabbitMQ.Next.Examples.PublisherMiddleware", "docs\examples\RabbitMQ.Next.Examples.PublisherMiddleware\RabbitMQ.Next.Examples.PublisherMiddleware.csproj", "{23340CFA-F105-4F7F-B2C5-16693A8626F9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -142,6 +144,10 @@ Global {C0440748-9787-4891-874A-054F5E64020B}.Debug|Any CPU.Build.0 = Debug|Any CPU {C0440748-9787-4891-874A-054F5E64020B}.Release|Any CPU.ActiveCfg = Release|Any CPU {C0440748-9787-4891-874A-054F5E64020B}.Release|Any CPU.Build.0 = Release|Any CPU + {23340CFA-F105-4F7F-B2C5-16693A8626F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {23340CFA-F105-4F7F-B2C5-16693A8626F9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {23340CFA-F105-4F7F-B2C5-16693A8626F9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {23340CFA-F105-4F7F-B2C5-16693A8626F9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {AB00A6F3-C3E0-4E61-B302-F9CF0288819E} = {C895EE24-13DF-45C3-AF1D-B6B6EE4A10A9} @@ -167,5 +173,6 @@ Global {95003EED-4B62-430E-8A4D-1D3B09CAA173} = {AD3AF1AA-71EE-4A2C-84DD-5C7DF141C187} {B93A51A9-0A31-4419-B8E8-C304EF18CFAB} = {750B1D71-E43B-4714-8AA3-AB9B0EC7E931} {C0440748-9787-4891-874A-054F5E64020B} = {750B1D71-E43B-4714-8AA3-AB9B0EC7E931} + {23340CFA-F105-4F7F-B2C5-16693A8626F9} = {750B1D71-E43B-4714-8AA3-AB9B0EC7E931} EndGlobalSection EndGlobal diff --git a/docs/examples/RabbitMQ.Next.Examples.DemoSaslAuthMechanism/Program.cs b/docs/examples/RabbitMQ.Next.Examples.DemoSaslAuthMechanism/Program.cs index 2e8772e..0a8e20f 100644 --- a/docs/examples/RabbitMQ.Next.Examples.DemoSaslAuthMechanism/Program.cs +++ b/docs/examples/RabbitMQ.Next.Examples.DemoSaslAuthMechanism/Program.cs @@ -1,24 +1,17 @@ using System; -using System.Threading.Tasks; +using RabbitMQ.Next; +using RabbitMQ.Next.Examples.DemoSaslAuthMechanism; -namespace RabbitMQ.Next.Examples.DemoSaslAuthMechanism; +Console.WriteLine("Hello World! Will try to connect RabbitMQ server with RABBIT-CR-DEMO auth mechanism."); -internal static class Program -{ - private static async Task Main() - { - Console.WriteLine("Hello World! Will try to connect RabbitMQ server with RABBIT-CR-DEMO auth mechanism."); +await using var connection = ConnectionBuilder.Default + .Endpoint("amqp://localhost:5672/") + .WithRabbitCrDemoAuth("guest", "guest") + .Build(); - await using var connection = ConnectionBuilder.Default - .Endpoint("amqp://localhost:5672/") - .WithRabbitCrDemoAuth("guest", "guest") - .Build(); +await connection.OpenAsync().ConfigureAwait(false); - await connection.OpenAsync().ConfigureAwait(false); +Console.WriteLine("Connection opened"); +Console.WriteLine("Press any key to close the connection"); - Console.WriteLine("Connection opened"); - Console.WriteLine("Press any key to close the connection"); - - Console.ReadKey(); - } -} +Console.ReadKey(); diff --git a/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/Program.cs b/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/Program.cs new file mode 100644 index 0000000..106cf24 --- /dev/null +++ b/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/Program.cs @@ -0,0 +1,37 @@ +using RabbitMQ.Next; +using RabbitMQ.Next.Publisher; +using RabbitMQ.Next.Serialization.PlainText; + +Console.WriteLine("Hello World! This is publisher based on RabbitMQ.Next library."); + +var connection = ConnectionBuilder.Default + .Endpoint("amqp://guest:guest@localhost:5672/") + .UsePlainTextSerializer() + .Build(); + +await using var publisher = connection.Publisher("amq.fanout", + builder => builder.UsePublishMiddleware((message, content) => + { + message.SetType(message.ClrType.Name); + })); + +Console.WriteLine("Publisher created. Type any text to send it to the 'amq.fanout' exchange. Enter empty string to exit"); + +while(true) +{ + var input = Console.ReadLine(); + if (string.IsNullOrEmpty(input)) + { + break; + } + + try + { + await publisher.PublishAsync(input).ConfigureAwait(false); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } +} diff --git a/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/RabbitMQ.Next.Examples.PublisherMiddleware.csproj b/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/RabbitMQ.Next.Examples.PublisherMiddleware.csproj new file mode 100644 index 0000000..9d72731 --- /dev/null +++ b/docs/examples/RabbitMQ.Next.Examples.PublisherMiddleware/RabbitMQ.Next.Examples.PublisherMiddleware.csproj @@ -0,0 +1,16 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + diff --git a/docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs b/docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs index 511fd0a..6e4320b 100644 --- a/docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs +++ b/docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs @@ -1,58 +1,51 @@ using System; using System.Threading; using System.Threading.Tasks; +using RabbitMQ.Next; using RabbitMQ.Next.Consumer; using RabbitMQ.Next.Serialization.PlainText; -namespace RabbitMQ.Next.Examples.SimpleConsumer; -internal static class Program -{ - private static async Task Main() - { - Console.WriteLine("Hello World! This is consumer based on RabbitMQ.Next library."); +Console.WriteLine("Hello World! This is consumer based on RabbitMQ.Next library."); - var connection = ConnectionBuilder.Default - .Endpoint("amqp://guest:guest@localhost:5672/") - .UsePlainTextSerializer() - .Build(); +var connection = ConnectionBuilder.Default + .Endpoint("amqp://guest:guest@localhost:5672/") + .UsePlainTextSerializer() + .Build(); - Console.WriteLine("Connection opened"); +await using var consumer = connection.Consumer( +builder => builder + .BindToQueue("my-queue") + .PrefetchCount(10)); - await using var consumer = connection.Consumer( - builder => builder - .BindToQueue("my-queue") - .PrefetchCount(10)); +Console.WriteLine("Consumer created. Press Ctrl+C to exit."); - Console.WriteLine("Consumer created. Press Ctrl+C to exit."); +using var cancellation = new CancellationTokenSource(); - using var cancellation = new CancellationTokenSource(); +MonitorKeypressAsync(cancellation); - MonitorKeypressAsync(cancellation); +await consumer.ConsumeAsync((message, content) => +{ + Console.WriteLine($"[{DateTimeOffset.Now.TimeOfDay}] Message received via '{message.Exchange}' exchange: {content.Get()}"); +} ,cancellation.Token); - await consumer.ConsumeAsync((message, content) => - { - Console.WriteLine($"[{DateTimeOffset.Now.TimeOfDay}] Message received via '{message.Exchange}' exchange: {content.Get()}"); - } ,cancellation.Token); - } - - private static Task MonitorKeypressAsync(CancellationTokenSource cancellation) + +static Task MonitorKeypressAsync(CancellationTokenSource cancellation) +{ + void WaitForInput() { - void WaitForInput() + ConsoleKeyInfo key; + do { - ConsoleKeyInfo key; - do - { - key = Console.ReadKey(true); + key = Console.ReadKey(true); - } while (key.Key != ConsoleKey.C && key.Modifiers != ConsoleModifiers.Control); + } while (key.Key != ConsoleKey.C && key.Modifiers != ConsoleModifiers.Control); - if (!cancellation.IsCancellationRequested) - { - cancellation.Cancel(); - } + if (!cancellation.IsCancellationRequested) + { + cancellation.Cancel(); } - - return Task.Run(WaitForInput); } + + return Task.Run(WaitForInput); } diff --git a/docs/examples/RabbitMQ.Next.Examples.SimplePublisher/Program.cs b/docs/examples/RabbitMQ.Next.Examples.SimplePublisher/Program.cs index 25ba60e..14a5bb4 100644 --- a/docs/examples/RabbitMQ.Next.Examples.SimplePublisher/Program.cs +++ b/docs/examples/RabbitMQ.Next.Examples.SimplePublisher/Program.cs @@ -1,55 +1,34 @@ using System; -using System.Diagnostics; -using System.Threading.Tasks; - +using RabbitMQ.Next; using RabbitMQ.Next.Publisher; using RabbitMQ.Next.Serialization.PlainText; -namespace RabbitMQ.Next.Examples.SimplePublisher; - -internal static class Program -{ - private static async Task Main() - { - Console.WriteLine("Hello World! This is publisher based on RabbitMQ.Next library."); - - var connection = ConnectionBuilder.Default - .Endpoint("amqp://guest:guest@localhost:5672/") - .UsePlainTextSerializer() - .Build(); +Console.WriteLine("Hello World! This is publisher based on RabbitMQ.Next library."); - Console.WriteLine("Connection opened"); +var connection = ConnectionBuilder.Default + .Endpoint("amqp://guest:guest@localhost:5672/") + .UsePlainTextSerializer() + .Build(); - await using var publisher = connection.Publisher("amq.fanout", - builder => builder.UsePublishMiddleware(async (message, content, next) => - { - var ts = Stopwatch.GetTimestamp(); - await next(message, content); - Console.WriteLine(Stopwatch.GetTimestamp() - ts); - }).UsePublishMiddleware((message, content) => - { - message.SetType(message.ClrType.Name); - })); +await using var publisher = connection.Publisher("amq.fanout"); - Console.WriteLine("Publisher created. Type any text to send it to the 'amq.fanout' exchange. Enter empty string to exit"); +Console.WriteLine("Publisher created. Type any text to send it to the 'amq.fanout' exchange. Enter empty string to exit"); - while(true) - { - var input = Console.ReadLine(); - if (string.IsNullOrEmpty(input)) - { - break; - } +while(true) +{ + var input = Console.ReadLine(); + if (string.IsNullOrEmpty(input)) + { + break; + } - try - { - await publisher.PublishAsync(input).ConfigureAwait(false); - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - } + try + { + await publisher.PublishAsync(input).ConfigureAwait(false); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; } } diff --git a/docs/examples/RabbitMQ.Next.Examples.TopologyBuilder/Program.cs b/docs/examples/RabbitMQ.Next.Examples.TopologyBuilder/Program.cs index fa2f34f..2368767 100644 --- a/docs/examples/RabbitMQ.Next.Examples.TopologyBuilder/Program.cs +++ b/docs/examples/RabbitMQ.Next.Examples.TopologyBuilder/Program.cs @@ -1,59 +1,38 @@ using System; -using System.Threading.Tasks; +using RabbitMQ.Next; using RabbitMQ.Next.TopologyBuilder; +Console.WriteLine("Hello World! This is topology builder based on RabbitMQ.Next library."); -namespace RabbitMQ.Next.Examples.TopologyBuilder; +var connection = ConnectionBuilder.Default + .Endpoint("amqp://guest:guest@localhost:5672/") + .Build(); -internal static class Program +await connection.ConfigureAsync(async topology => { - private static async Task Main() - { - try - { - Console.WriteLine("Hello World! This is topology builder based on RabbitMQ.Next library."); - - var connection = ConnectionBuilder.Default - .Endpoint("amqp://guest:guest@localhost:5672/") - .Build(); - - Console.WriteLine("Connection opened"); - - await connection.ConfigureAsync(async topology => - { - await topology.Exchange.DeclareAsync("my-exchange", ExchangeType.Topic); - Console.WriteLine("'my-exchange' was created with using library defaults (durable by default)"); - - await topology.Exchange.DeclareAsync("my-advanced-exchange", ExchangeType.Topic, - builder => builder - .AutoDelete()); - Console.WriteLine("'my-advanced-exchange' was created by explicitly configuring to be auto-delete"); - - Console.WriteLine("--------------------------------------------------------------"); - - await topology.Queue.DeclareQuorumAsync("my-queue"); - Console.WriteLine("Declare quorum queue named 'my-queue'"); - - await topology.Queue.DeclareClassicAsync("my-advanced-queue", - builder => builder - .AutoDelete() - .MaxLength(1000)); - Console.WriteLine("'my-advanced-queue' was created by explicitly configuring to be auto-delete and max-length 1000"); - - Console.WriteLine("--------------------------------------------------------------"); - - await topology.Queue.BindAsync("my-queue", "amq.fanout"); - await topology.Queue.BindAsync("my-queue", "my-exchange", "cat"); - await topology.Queue.BindAsync("my-queue", "my-exchange", "dog"); - Console.WriteLine("my-queue was bound to my-exchange by 2 bindings."); - }); - - await connection.DisposeAsync(); - } - catch (Exception e) - { - Console.WriteLine(e); - throw; - } - } -} + await topology.Exchange.DeclareAsync("my-exchange", ExchangeType.Topic); + Console.WriteLine("'my-exchange' was created with using library defaults (durable by default)"); + + await topology.Exchange.DeclareAsync("my-advanced-exchange", ExchangeType.Topic, + builder => builder + .AutoDelete()); + Console.WriteLine("'my-advanced-exchange' was created by explicitly configuring to be auto-delete"); + + Console.WriteLine("--------------------------------------------------------------"); + + await topology.Queue.DeclareQuorumAsync("my-queue"); + Console.WriteLine("Declare quorum queue named 'my-queue'"); + + await topology.Queue.DeclareClassicAsync("my-advanced-queue", + builder => builder + .AutoDelete() + .MaxLength(1000)); + Console.WriteLine("'my-advanced-queue' was created by explicitly configuring to be auto-delete and max-length 1000"); + + Console.WriteLine("--------------------------------------------------------------"); + + await topology.Queue.BindAsync("my-queue", "amq.fanout"); + await topology.Queue.BindAsync("my-queue", "my-exchange", "cat"); + await topology.Queue.BindAsync("my-queue", "my-exchange", "dog"); + Console.WriteLine("my-queue was bound to my-exchange by 2 bindings."); +});