Skip to content

Commit 670a4c2

Browse files
Add DynamoDB 4 Sample (#7566)
* Using the default port for DynamoDB. * Remove InputLoopService. * Add DynamoDB_4 sample * Remove version wildcard * Revert to alpha.1 * Upgrade NSB versions
1 parent 9432e9b commit 670a4c2

20 files changed

+315
-43
lines changed

samples/aws/dynamodb-simple/DynamoDB_3/Client/InputLoopService.cs

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,61 @@
11
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
24
using Microsoft.Extensions.DependencyInjection;
35
using Microsoft.Extensions.Hosting;
46
using NServiceBus;
57

6-
78
Console.Title = "Client";
89

910
var builder = Host.CreateApplicationBuilder(args);
10-
builder.Services.AddHostedService<InputLoopService>();
1111

1212
var endpointConfiguration = new EndpointConfiguration("Samples.DynamoDB.Simple.Client");
1313
endpointConfiguration.UseTransport<LearningTransport>();
1414
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
1515

16-
17-
1816
Console.WriteLine("Press any key, the application is starting");
1917
Console.ReadKey();
2018
Console.WriteLine("Starting...");
2119

2220
builder.UseNServiceBus(endpointConfiguration);
23-
await builder.Build().RunAsync();
21+
22+
var host = builder.Build();
23+
24+
await host.StartAsync();
25+
26+
// Get the required services
27+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
28+
// Register a cancellation token to gracefully handle application shutdown
29+
var ct = host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping;
30+
31+
Console.WriteLine("Press 'S' to send a StartOrder message to the server endpoint.");
32+
Console.WriteLine("Press Ctrl+C to shut down.");
33+
34+
// Wait for user input to publish messages
35+
while (!ct.IsCancellationRequested)
36+
{
37+
if (!Console.KeyAvailable)
38+
{
39+
// If no key is pressed, wait for a short time before checking again
40+
await Task.Delay(100, CancellationToken.None);
41+
continue;
42+
}
43+
44+
var key = Console.ReadKey();
45+
Console.WriteLine();
46+
47+
if (key.Key == ConsoleKey.S)
48+
{
49+
var orderId = Guid.NewGuid();
50+
var startOrder = new StartOrder
51+
{
52+
OrderId = orderId
53+
};
54+
55+
await messageSession.Send("Samples.DynamoDB.Simple.Server", startOrder);
56+
Console.WriteLine($"StartOrder Message sent to Server with OrderId {orderId}");
57+
}
58+
}
59+
60+
// Wait for the host to stop gracefully
61+
await host.StopAsync();

samples/aws/dynamodb-simple/DynamoDB_3/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ services:
55
image: "amazon/dynamodb-local:latest"
66
container_name: dynamodb-local
77
ports:
8-
- "8080:8000"
8+
- "8000:8000"
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+
<PropertyGroup>
3+
<TargetFramework>net10.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<LangVersion>preview</LangVersion>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="4.0.0-alpha.2" />
10+
</ItemGroup>
11+
<ItemGroup>
12+
<ProjectReference Include="..\SharedMessages\SharedMessages.csproj" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
public class OrderCompletedHandler(ILogger<OrderCompletedHandler> logger) :
4+
IHandleMessages<OrderCompleted>
5+
{
6+
public Task Handle(OrderCompleted message, IMessageHandlerContext context)
7+
{
8+
logger.LogInformation("Received OrderCompleted for OrderId {OrderId}", message.OrderId);
9+
return Task.CompletedTask;
10+
}
11+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using Microsoft.Extensions.Hosting;
3+
4+
Console.Title = "Client";
5+
6+
var builder = Host.CreateApplicationBuilder(args);
7+
8+
var endpointConfiguration = new EndpointConfiguration("Samples.DynamoDB.Simple.Client");
9+
endpointConfiguration.UseTransport<LearningTransport>();
10+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
11+
12+
builder.UseNServiceBus(endpointConfiguration);
13+
14+
var host = builder.Build();
15+
16+
await host.StartAsync();
17+
18+
// Get the required services
19+
var messageSession = host.Services.GetRequiredService<IMessageSession>();
20+
// Register a cancellation token to gracefully handle application shutdown
21+
var ct = host.Services.GetRequiredService<IHostApplicationLifetime>().ApplicationStopping;
22+
23+
Console.WriteLine("Press 'S' to send a StartOrder message to the server endpoint.");
24+
Console.WriteLine("Press Ctrl+C to shut down.");
25+
26+
// Wait for user input to publish messages
27+
while (!ct.IsCancellationRequested)
28+
{
29+
if (!Console.KeyAvailable)
30+
{
31+
// If no key is pressed, wait for a short time before checking again
32+
await Task.Delay(100, CancellationToken.None);
33+
continue;
34+
}
35+
36+
var key = Console.ReadKey();
37+
Console.WriteLine();
38+
39+
if (key.Key == ConsoleKey.S)
40+
{
41+
var orderId = Guid.NewGuid();
42+
var startOrder = new StartOrder
43+
{
44+
OrderId = orderId
45+
};
46+
47+
await messageSession.Send("Samples.DynamoDB.Simple.Server", startOrder);
48+
Console.WriteLine($"StartOrder Message sent to Server with OrderId {orderId}");
49+
}
50+
}
51+
52+
// Wait for the host to stop gracefully
53+
await host.StopAsync();
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Client\Client.csproj
2+
Server\Server.csproj
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.8.34302.71
4+
MinimumVisualStudioVersion = 15.0.26730.12
5+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Server\Server.csproj", "{48F718EE-6C45-41BA-80EC-81BF34D4A623}"
6+
EndProject
7+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SharedMessages", "SharedMessages\SharedMessages.csproj", "{DD438DB2-9C03-4BC0-BA52-BB7A35098458}"
8+
EndProject
9+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{2FE71442-7F81-428E-B945-D564850D6564}"
10+
EndProject
11+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3E9D20AD-5EF2-46B6-AF34-F4E639129E05}"
12+
ProjectSection(SolutionItems) = preProject
13+
docker-compose.yml = docker-compose.yml
14+
EndProjectSection
15+
EndProject
16+
Global
17+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
18+
Debug|Any CPU = Debug|Any CPU
19+
EndGlobalSection
20+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
21+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
22+
{48F718EE-6C45-41BA-80EC-81BF34D4A623}.Debug|Any CPU.Build.0 = Debug|Any CPU
23+
{DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{DD438DB2-9C03-4BC0-BA52-BB7A35098458}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{2FE71442-7F81-428E-B945-D564850D6564}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
EndGlobalSection
28+
GlobalSection(SolutionProperties) = preSolution
29+
HideSolutionNode = FALSE
30+
EndGlobalSection
31+
GlobalSection(ExtensibilityGlobals) = postSolution
32+
SolutionGuid = {BB6580A8-3A40-4373-B226-A86402536658}
33+
EndGlobalSection
34+
EndGlobal
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public class CompleteOrder
2+
{
3+
public string OrderDescription { get; set; }
4+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
#region thesaga
4+
5+
public class OrderSaga(ILogger<OrderSagaData> logger) :
6+
Saga<OrderSagaData>,
7+
IAmStartedByMessages<StartOrder>,
8+
IHandleTimeouts<CompleteOrder>
9+
{
10+
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<OrderSagaData> mapper)
11+
{
12+
mapper.MapSaga(saga => saga.OrderId).ToMessage<StartOrder>(msg => msg.OrderId);
13+
}
14+
15+
public Task Handle(StartOrder message, IMessageHandlerContext context)
16+
{
17+
var orderDescription = $"The saga for order {message.OrderId}";
18+
Data.OrderDescription = orderDescription;
19+
20+
logger.LogInformation("Received StartOrder message {OrderId}. Starting Saga", Data.OrderId);
21+
22+
var shipOrder = new ShipOrder
23+
{
24+
OrderId = message.OrderId
25+
};
26+
27+
logger.LogInformation("Order will complete in 5 seconds");
28+
var timeoutData = new CompleteOrder
29+
{
30+
OrderDescription = orderDescription,
31+
};
32+
33+
return Task.WhenAll(
34+
context.SendLocal(shipOrder),
35+
RequestTimeout(context, TimeSpan.FromSeconds(5), timeoutData)
36+
);
37+
}
38+
39+
public Task Timeout(CompleteOrder state, IMessageHandlerContext context)
40+
{
41+
logger.LogInformation("Saga with OrderId {OrderId} completed", Data.OrderId);
42+
MarkAsComplete();
43+
var orderCompleted = new OrderCompleted
44+
{
45+
OrderId = Data.OrderId
46+
};
47+
return context.Publish(orderCompleted);
48+
}
49+
}
50+
51+
#endregion

0 commit comments

Comments
 (0)