Skip to content

Commit 06c0e3c

Browse files
authored
Merge pull request #765 from rabbitmq/add-amqp-tutorials
Add tutorials for AMQP 1.0 for .NET, Java and Go
2 parents e1eee12 + 04b88a7 commit 06c0e3c

75 files changed

Lines changed: 5092 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@ updates:
1313
versions: [ "[6.0,)" ]
1414
- dependency-name: "org.junit.platform:*"
1515
versions: [ "[6.0,)" ]
16+
- package-ecosystem: "maven"
17+
directory: "/java-amqp"
18+
schedule:
19+
interval: "daily"
20+
open-pull-requests-limit: 20
21+
target-branch: "main"
22+
ignore:
23+
- dependency-name: "org.junit.jupiter:*"
24+
versions: [ "[6.0,)" ]
25+
- dependency-name: "org.junit.platform:*"
26+
versions: [ "[6.0,)" ]
1627
- package-ecosystem: "maven"
1728
directory: "/java-stream-mvn"
1829
schedule:

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ target/
4343
.classpath
4444
.project
4545
.settings
46+
47+
/.cursor/plans

dotnet-amqp/.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text=auto
3+
4+
# Auto detect text files and perform LF normalization
5+
*.cs text=auto eol=lf
6+
*.txt text=auto
7+
8+
# Declare files that will always have CRLF line endings on checkout.
9+
*.sln text eol=crlf
10+
*.csproj text eol=crlf
11+
12+
# Custom for Visual Studio
13+
*.cs diff=csharp

dotnet-amqp/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.dll
2+
*.exe
3+
*.lock.json
4+
packages/
5+
bin/
6+
obj/

dotnet-amqp/EmitLog/EmitLog.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="1.0.0" />
12+
</ItemGroup>
13+
14+
</Project>

dotnet-amqp/EmitLog/Program.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs";
7+
8+
string message = args.Length < 1 ? "info: Hello World!" : string.Join(" ", args);
9+
10+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
11+
.Uri(new Uri(brokerUri))
12+
.ContainerId("tutorial-emitlog")
13+
.Build();
14+
15+
IEnvironment environment = AmqpEnvironment.Create(settings);
16+
IConnection connection = await environment.CreateConnectionAsync();
17+
18+
try
19+
{
20+
IManagement management = connection.Management();
21+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("fanout");
22+
await exchangeSpec.DeclareAsync();
23+
24+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).BuildAsync();
25+
try
26+
{
27+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
28+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
29+
switch (pr.Outcome.State)
30+
{
31+
case OutcomeState.Accepted:
32+
break;
33+
case OutcomeState.Released:
34+
Console.Error.WriteLine($"Released message: {pr.Message.BodyAsString()}");
35+
Environment.Exit(1);
36+
break;
37+
case OutcomeState.Rejected:
38+
Console.Error.WriteLine($"[Publisher] Message: {pr.Message.BodyAsString()} rejected with error: {pr.Outcome.Error}");
39+
Environment.Exit(1);
40+
break;
41+
default:
42+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
43+
Environment.Exit(1);
44+
break;
45+
}
46+
47+
Console.WriteLine($" [x] Sent '{message}'");
48+
}
49+
finally
50+
{
51+
await publisher.CloseAsync();
52+
}
53+
}
54+
finally
55+
{
56+
await connection.CloseAsync();
57+
await environment.CloseAsync();
58+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="1.0.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs_direct";
7+
8+
string severity = GetSeverity(args);
9+
string message = GetMessage(args);
10+
11+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
12+
.Uri(new Uri(brokerUri))
13+
.ContainerId("tutorial-emitlogdirect")
14+
.Build();
15+
16+
IEnvironment environment = AmqpEnvironment.Create(settings);
17+
IConnection connection = await environment.CreateConnectionAsync();
18+
19+
try
20+
{
21+
IManagement management = connection.Management();
22+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("direct");
23+
await exchangeSpec.DeclareAsync();
24+
25+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(severity).BuildAsync();
26+
try
27+
{
28+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
29+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
30+
switch (pr.Outcome.State)
31+
{
32+
case OutcomeState.Accepted:
33+
break;
34+
case OutcomeState.Released:
35+
Console.Error.WriteLine($"Released message: {pr.Message.BodyAsString()}");
36+
Environment.Exit(1);
37+
break;
38+
case OutcomeState.Rejected:
39+
Console.Error.WriteLine($"[Publisher] Message: {pr.Message.BodyAsString()} rejected with error: {pr.Outcome.Error}");
40+
Environment.Exit(1);
41+
break;
42+
default:
43+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
44+
Environment.Exit(1);
45+
break;
46+
}
47+
48+
Console.WriteLine($" [x] Sent '{severity}':'{message}'");
49+
}
50+
finally
51+
{
52+
await publisher.CloseAsync();
53+
}
54+
}
55+
finally
56+
{
57+
await connection.CloseAsync();
58+
await environment.CloseAsync();
59+
}
60+
61+
static string GetSeverity(string[] strings) => strings.Length < 1 ? "info" : strings[0];
62+
63+
static string GetMessage(string[] strings)
64+
{
65+
if (strings.Length < 2)
66+
{
67+
return "Hello World!";
68+
}
69+
70+
return string.Join(" ", strings.Skip(1));
71+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFrameworks>net8.0;net10.0</TargetFrameworks>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="RabbitMQ.AMQP.Client" Version="1.0.0" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Text;
2+
using RabbitMQ.AMQP.Client;
3+
using RabbitMQ.AMQP.Client.Impl;
4+
5+
const string brokerUri = "amqp://guest:guest@localhost:5672/%2f";
6+
const string exchangeName = "logs_topic";
7+
8+
string routingKey = GetRouting(args);
9+
string message = GetMessage(args);
10+
11+
ConnectionSettings settings = ConnectionSettingsBuilder.Create()
12+
.Uri(new Uri(brokerUri))
13+
.ContainerId("tutorial-emitlogtopic")
14+
.Build();
15+
16+
IEnvironment environment = AmqpEnvironment.Create(settings);
17+
IConnection connection = await environment.CreateConnectionAsync();
18+
19+
try
20+
{
21+
IManagement management = connection.Management();
22+
IExchangeSpecification exchangeSpec = management.Exchange(exchangeName).Type("topic");
23+
await exchangeSpec.DeclareAsync();
24+
25+
IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(routingKey).BuildAsync();
26+
try
27+
{
28+
var amqpMessage = new AmqpMessage(Encoding.UTF8.GetBytes(message));
29+
PublishResult pr = await publisher.PublishAsync(amqpMessage);
30+
switch (pr.Outcome.State)
31+
{
32+
case OutcomeState.Accepted:
33+
break;
34+
case OutcomeState.Released:
35+
Console.Error.WriteLine($"Released message: {pr.Message.BodyAsString()}");
36+
Environment.Exit(1);
37+
break;
38+
case OutcomeState.Rejected:
39+
Console.Error.WriteLine($"[Publisher] Message: {pr.Message.BodyAsString()} rejected with error: {pr.Outcome.Error}");
40+
Environment.Exit(1);
41+
break;
42+
default:
43+
Console.Error.WriteLine($"Unexpected publish outcome: {pr.Outcome.State}");
44+
Environment.Exit(1);
45+
break;
46+
}
47+
48+
Console.WriteLine($" [x] Sent '{routingKey}':'{message}'");
49+
}
50+
finally
51+
{
52+
await publisher.CloseAsync();
53+
}
54+
}
55+
finally
56+
{
57+
await connection.CloseAsync();
58+
await environment.CloseAsync();
59+
}
60+
61+
static string GetRouting(string[] strings) => strings.Length < 1 ? "anonymous.info" : strings[0];
62+
63+
static string GetMessage(string[] strings)
64+
{
65+
if (strings.Length < 2)
66+
{
67+
return "Hello World!";
68+
}
69+
70+
return string.Join(" ", strings.Skip(1));
71+
}

0 commit comments

Comments
 (0)