Skip to content

Commit

Permalink
Examples clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sanych-sun committed Sep 4, 2024
1 parent 37f9e95 commit 2882bc7
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 152 deletions.
7 changes: 7 additions & 0 deletions RabbitMQ.Next.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\RabbitMQ.Next.Publisher\RabbitMQ.Next.Publisher.csproj" />
<ProjectReference Include="..\..\..\src\RabbitMQ.Next.Serialization.PlainText\RabbitMQ.Next.Serialization.PlainText.csproj" />
<ProjectReference Include="..\..\..\src\RabbitMQ.Next\RabbitMQ.Next.csproj" />
</ItemGroup>

</Project>
67 changes: 30 additions & 37 deletions docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs
Original file line number Diff line number Diff line change
@@ -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);

Check warning on line 25 in docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

Check warning on line 25 in docs/examples/RabbitMQ.Next.Examples.SimpleConsumer/Program.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

MonitorKeypressAsync(cancellation);
await consumer.ConsumeAsync((message, content) =>
{
Console.WriteLine($"[{DateTimeOffset.Now.TimeOfDay}] Message received via '{message.Exchange}' exchange: {content.Get<string>()}");
} ,cancellation.Token);

await consumer.ConsumeAsync((message, content) =>
{
Console.WriteLine($"[{DateTimeOffset.Now.TimeOfDay}] Message received via '{message.Exchange}' exchange: {content.Get<string>()}");
} ,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);
}
67 changes: 23 additions & 44 deletions docs/examples/RabbitMQ.Next.Examples.SimplePublisher/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
85 changes: 32 additions & 53 deletions docs/examples/RabbitMQ.Next.Examples.TopologyBuilder/Program.cs
Original file line number Diff line number Diff line change
@@ -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.");
});

0 comments on commit 2882bc7

Please sign in to comment.