-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Add tutorials for AMQP 1.0 for .NET, Java and Go #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d8dddec
Add Java AMQP 1.0 tutorial
Zerpet b3fc942
Add Go AMQP 1.0 tutorial port in go-amqp
Zerpet caba7b3
Add dotnet-amqp AMQP 1.0 tutorial port
Zerpet 7181ef4
Add Cursor plans to git ignore list
Zerpet 7cbf10d
dotnet-amqp: decode message bodies with BodyAsString()
Zerpet a5f1567
Bump AMQP clients to 1.0
Zerpet d02542f
Multi-target C# tutorials
Zerpet c81c6b6
Showcase different AMQP outcomes
Zerpet 04b88a7
Refactor AMQP 1.0 outcomes handling in go-amqp
Zerpet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,3 +43,5 @@ target/ | |
| .classpath | ||
| .project | ||
| .settings | ||
|
|
||
| /.cursor/plans | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Set the default behavior, in case people don't have core.autocrlf set. | ||
| * text=auto | ||
|
|
||
| # Auto detect text files and perform LF normalization | ||
| *.cs text=auto eol=lf | ||
| *.txt text=auto | ||
|
|
||
| # Declare files that will always have CRLF line endings on checkout. | ||
| *.sln text eol=crlf | ||
| *.csproj text eol=crlf | ||
|
|
||
| # Custom for Visual Studio | ||
| *.cs diff=csharp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| *.dll | ||
| *.exe | ||
| *.lock.json | ||
| packages/ | ||
| bin/ | ||
| obj/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System.Text; | ||
| using RabbitMQ.AMQP.Client; | ||
| using RabbitMQ.AMQP.Client.Impl; | ||
|
|
||
| const string brokerUri = "amqp://guest:guest@localhost:5672/%2f"; | ||
| const string exchangeName = "logs"; | ||
|
|
||
| string message = args.Length < 1 ? "info: Hello World!" : string.Join(" ", args); | ||
|
|
||
| ConnectionSettings settings = ConnectionSettingsBuilder.Create() | ||
| .Uri(new Uri(brokerUri)) | ||
| .ContainerId("tutorial-emitlog") | ||
| .Build(); | ||
|
|
||
| IEnvironment environment = AmqpEnvironment.Create(settings); | ||
| IConnection connection = await environment.CreateConnectionAsync(); | ||
|
|
||
| try | ||
| { | ||
| IManagement management = connection.Management(); | ||
| IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("fanout"); | ||
| await exchangeSpec.DeclareAsync(); | ||
|
|
||
| IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).BuildAsync(); | ||
| try | ||
| { | ||
| var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message)); | ||
| PublishResult pr = await publisher.PublishAsync(amqpMessage); | ||
| if (pr.Outcome.State != OutcomeState.Accepted) | ||
| { | ||
| Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}"); | ||
| Environment.Exit(1); | ||
| } | ||
|
|
||
| Console.WriteLine($" [x] Sent '{message}'"); | ||
| } | ||
| finally | ||
| { | ||
| await publisher.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await connection.CloseAsync(); | ||
| await environment.CloseAsync(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Text; | ||
| using RabbitMQ.AMQP.Client; | ||
| using RabbitMQ.AMQP.Client.Impl; | ||
|
|
||
| const string brokerUri = "amqp://guest:guest@localhost:5672/%2f"; | ||
| const string exchangeName = "logs_direct"; | ||
|
|
||
| string severity = GetSeverity(args); | ||
| string message = GetMessage(args); | ||
|
|
||
| ConnectionSettings settings = ConnectionSettingsBuilder.Create() | ||
| .Uri(new Uri(brokerUri)) | ||
| .ContainerId("tutorial-emitlogdirect") | ||
| .Build(); | ||
|
|
||
| IEnvironment environment = AmqpEnvironment.Create(settings); | ||
| IConnection connection = await environment.CreateConnectionAsync(); | ||
|
|
||
| try | ||
| { | ||
| IManagement management = connection.Management(); | ||
| IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("direct"); | ||
| await exchangeSpec.DeclareAsync(); | ||
|
|
||
| IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(severity).BuildAsync(); | ||
| try | ||
| { | ||
| var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message)); | ||
| PublishResult pr = await publisher.PublishAsync(amqpMessage); | ||
| if (pr.Outcome.State != OutcomeState.Accepted) | ||
| { | ||
| Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}"); | ||
| Environment.Exit(1); | ||
| } | ||
|
|
||
| Console.WriteLine($" [x] Sent '{severity}':'{message}'"); | ||
| } | ||
| finally | ||
| { | ||
| await publisher.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await connection.CloseAsync(); | ||
| await environment.CloseAsync(); | ||
| } | ||
|
|
||
| static string GetSeverity(string[] strings) => strings.Length < 1 ? "info" : strings[0]; | ||
|
|
||
| static string GetMessage(string[] strings) | ||
| { | ||
| if (strings.Length < 2) | ||
| { | ||
| return "Hello World!"; | ||
| } | ||
|
|
||
| return string.Join(" ", strings.Skip(1)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| using System.Text; | ||
| using RabbitMQ.AMQP.Client; | ||
| using RabbitMQ.AMQP.Client.Impl; | ||
|
|
||
| const string brokerUri = "amqp://guest:guest@localhost:5672/%2f"; | ||
| const string exchangeName = "logs_topic"; | ||
|
|
||
| string routingKey = GetRouting(args); | ||
| string message = GetMessage(args); | ||
|
|
||
| ConnectionSettings settings = ConnectionSettingsBuilder.Create() | ||
| .Uri(new Uri(brokerUri)) | ||
| .ContainerId("tutorial-emitlogtopic") | ||
| .Build(); | ||
|
|
||
| IEnvironment environment = AmqpEnvironment.Create(settings); | ||
| IConnection connection = await environment.CreateConnectionAsync(); | ||
|
|
||
| try | ||
| { | ||
| IManagement management = connection.Management(); | ||
| IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("topic"); | ||
| await exchangeSpec.DeclareAsync(); | ||
|
|
||
| IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(routingKey).BuildAsync(); | ||
| try | ||
| { | ||
| var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message)); | ||
| PublishResult pr = await publisher.PublishAsync(amqpMessage); | ||
| if (pr.Outcome.State != OutcomeState.Accepted) | ||
| { | ||
| Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}"); | ||
| Environment.Exit(1); | ||
| } | ||
|
|
||
| Console.WriteLine($" [x] Sent '{routingKey}':'{message}'"); | ||
| } | ||
| finally | ||
| { | ||
| await publisher.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await connection.CloseAsync(); | ||
| await environment.CloseAsync(); | ||
| } | ||
|
|
||
| static string GetRouting(string[] strings) => strings.Length < 1 ? "anonymous.info" : strings[0]; | ||
|
|
||
| static string GetMessage(string[] strings) | ||
| { | ||
| if (strings.Length < 2) | ||
| { | ||
| return "Hello World!"; | ||
| } | ||
|
|
||
| return string.Join(" ", strings.Skip(1)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="RabbitMQ.AMQP.Client" Version="0.60.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| using System.Text; | ||
| using RabbitMQ.AMQP.Client; | ||
| using RabbitMQ.AMQP.Client.Impl; | ||
|
|
||
| const string brokerUri = "amqp://guest:guest@localhost:5672/%2f"; | ||
| const string taskQueueName = "task_queue"; | ||
|
|
||
| string message = args.Length > 0 ? string.Join(" ", args) : "Hello World!"; | ||
|
|
||
| ConnectionSettings settings = ConnectionSettingsBuilder.Create() | ||
| .Uri(new Uri(brokerUri)) | ||
| .ContainerId("tutorial-newtask") | ||
| .Build(); | ||
|
|
||
| IEnvironment environment = AmqpEnvironment.Create(settings); | ||
| IConnection connection = await environment.CreateConnectionAsync(); | ||
|
|
||
| try | ||
| { | ||
| IManagement management = connection.Management(); | ||
| IQueueSpecification queueSpec = management.Queue(taskQueueName).Type(QueueType.QUORUM); | ||
| await queueSpec.DeclareAsync(); | ||
|
|
||
| IPublisher publisher = await connection.PublisherBuilder().Queue(taskQueueName).BuildAsync(); | ||
| try | ||
| { | ||
| var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message)); | ||
| PublishResult pr = await publisher.PublishAsync(amqpMessage); | ||
| if (pr.Outcome.State != OutcomeState.Accepted) | ||
| { | ||
| Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}"); | ||
| Environment.Exit(1); | ||
| } | ||
|
|
||
| Console.WriteLine($" [x] Sent '{message}'"); | ||
| } | ||
| finally | ||
| { | ||
| await publisher.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await connection.CloseAsync(); | ||
| await environment.CloseAsync(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| using System.Text; | ||
| using RabbitMQ.AMQP.Client; | ||
| using RabbitMQ.AMQP.Client.Impl; | ||
|
|
||
| const string brokerUri = "amqp://guest:guest@localhost:5672/%2f"; | ||
|
|
||
| ConnectionSettings settings = ConnectionSettingsBuilder.Create() | ||
| .Uri(new Uri(brokerUri)) | ||
| .ContainerId("tutorial-publisherconfirms") | ||
| .Build(); | ||
|
|
||
| IEnvironment environment = AmqpEnvironment.Create(settings); | ||
| IConnection connection = await environment.CreateConnectionAsync(); | ||
|
|
||
| try | ||
| { | ||
| IManagement management = connection.Management(); | ||
| string queueName = Guid.NewGuid().ToString(); | ||
| IQueueSpecification queueSpec = management.Queue(queueName).Exclusive(true).AutoDelete(true); | ||
| await queueSpec.DeclareAsync(); | ||
|
|
||
| IConsumer consumer = await connection.ConsumerBuilder() | ||
| .Queue(queueName) | ||
| .MessageHandler((ctx, message) => | ||
| { | ||
| Console.WriteLine($"Received a message: {Encoding.UTF8.GetString(message.Body()!)}"); | ||
| ctx.Accept(); | ||
| return Task.CompletedTask; | ||
| }) | ||
| .BuildAndStartAsync(); | ||
|
|
||
| try | ||
| { | ||
| await Task.Delay(300); | ||
|
|
||
| IPublisher publisher = await connection.PublisherBuilder().Queue(queueName).BuildAsync(); | ||
| try | ||
| { | ||
| const string text = "hello"; | ||
| PublishResult pr = await publisher.PublishAsync(new AmqpMessage(Encoding.UTF8.GetBytes(text))); | ||
| if (pr.Outcome.State == OutcomeState.Accepted) | ||
| { | ||
| Console.WriteLine("Confirmed"); | ||
| } | ||
| else | ||
| { | ||
| Console.WriteLine($"Unexpected outcome: {pr.Outcome.State}"); | ||
| } | ||
|
|
||
| await Task.Delay(500); | ||
| } | ||
| finally | ||
| { | ||
| await publisher.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await consumer.CloseAsync(); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| await connection.CloseAsync(); | ||
| await environment.CloseAsync(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.