Skip to content
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

Improve support to AWS SQS/SNS #3437

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
1ddd758
GH-1294 Add support to FIFO
lillo42 Dec 22, 2024
582d57c
GH-1294 fixes build
lillo42 Dec 22, 2024
c9e3033
GH-1294 Add Fifo test
lillo42 Dec 29, 2024
7a188b5
Merge with master
lillo42 Dec 30, 2024
ab9d3e9
GH-1294 fixes unit test
lillo42 Dec 30, 2024
c9b7e83
GH-1294 Improve support to LocalStack
lillo42 Dec 30, 2024
c94003e
GH-1294 Use AwsFactory
lillo42 Dec 31, 2024
9226568
Merge branch 'master' into GH-1294.add.aws.fifo
lillo42 Dec 31, 2024
849d21f
Merge branch 'master' into GH-1294.add.aws.fifo
lillo42 Dec 31, 2024
c9b3f7c
Merge branch 'master' into GH-1294.add.aws.fifo
lillo42 Jan 2, 2025
85db988
GH-1294 Fixes the majority of test, improve support to localstack and…
lillo42 Jan 2, 2025
e449da8
GH-1294 Fixes http test
lillo42 Jan 2, 2025
527e14a
Add Fifo test
lillo42 Jan 2, 2025
0cfb765
Merge with master
lillo42 Jan 2, 2025
11ffdef
GH-1294 fixes DLQ, Get System Attribute & Unit tests
lillo42 Jan 3, 2025
75f431b
GH-1294 Improve Valid SQS/SNS name logic
lillo42 Jan 3, 2025
58a701e
GH-1294 Improve Valid SQS/SNS name logic
lillo42 Jan 3, 2025
4299f90
Add support to SQS Publication and add FIFO tests
lillo42 Jan 3, 2025
e849a3f
Fixes SQS unit tests
lillo42 Jan 4, 2025
5abb8d6
GH-1294 Add Fifo sample
lillo42 Jan 4, 2025
3f02f03
Improvements
lillo42 Jan 5, 2025
7a868d7
Rename ChannelType
lillo42 Jan 6, 2025
acbf666
Merge with master
lillo42 Jan 6, 2025
9d619e4
Set max delay
lillo42 Jan 6, 2025
2c914eb
Merge with master
lillo42 Jan 6, 2025
c0bb2a4
Merge with master
lillo42 Jan 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docker-compose-localstack.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: '3'

services:
localstack:
image: localstack/localstack
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- "SERVICES=s3,sqs,sns,sts,dynamodb"
- "DEFAULT_REGION=eu-west-1"
- "DEBUG=1"
ports:
- "4566:4566" # LocalStack Gateway
- "4510-4559:4510-4559" # External services port range
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using Greetings.Ports.Commands;
using Paramore.Brighter;

namespace Greetings.Ports.CommandHandlers
{
public class FarewellEventHandler : RequestHandler<FarewellEvent>
{
public override FarewellEvent Handle(FarewellEvent @event)
{
Console.BackgroundColor = ConsoleColor.Blue;
Console.ForegroundColor = ConsoleColor.White;

Console.WriteLine("Received Farewell. Message Follows");
Console.WriteLine("----------------------------------");
Console.WriteLine(@event.Farewell);
Console.WriteLine("----------------------------------");
Console.WriteLine("Message Ends");

Console.ResetColor();
return base.Handle(@event);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using Paramore.Brighter;

namespace Greetings.Ports.Commands
{
public class FarewellEvent : Event
{
public FarewellEvent() : base(Guid.NewGuid())
{
}

public FarewellEvent(string farewell) : base(Guid.NewGuid())
{
Farewell = farewell;
}

public string Farewell { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#region Licence
/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. */

#endregion

using System.Text.Json;
using Greetings.Ports.Commands;
using Paramore.Brighter;
using Paramore.Brighter.MessagingGateway.AWSSQS;

namespace Greetings.Ports.Mappers
{
public class FarewellEventMessageMapper : IAmAMessageMapper<FarewellEvent>
{
public IRequestContext Context { get; set; }

public Message MapToMessage(FarewellEvent request, Publication publication)
{
var header = new MessageHeader(messageId: request.Id, topic: publication.Topic, messageType: MessageType.MT_EVENT);
var body = new MessageBody(JsonSerializer.Serialize(request, JsonSerialisationOptions.Options));
var message = new Message(header, body);
return message;
}

public FarewellEvent MapToRequest(Message message)
{
var greetingCommand = JsonSerializer.Deserialize<FarewellEvent>(message.Body.Value, JsonSerialisationOptions.Options);

return greetingCommand;
}
}
}
57 changes: 36 additions & 21 deletions samples/TaskQueue/AWSTaskQueue/GreetingsPumper/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,47 @@ private static async Task Main(string[] args)
var host = new HostBuilder()
.ConfigureServices((hostContext, services) =>

{
if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
{
var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.EUWest1);
if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
{
var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.USEast1,
cfg =>
{
var serviceURL = Environment.GetEnvironmentVariable("LOCALSTACK_SERVICE_URL");
if (!string.IsNullOrWhiteSpace(serviceURL))
{
cfg.ServiceURL = serviceURL;
}
});

var producerRegistry = new SnsProducerRegistryFactory(
awsConnection,
new SnsPublication[]
{
new SnsPublication
var producerRegistry = new SnsProducerRegistryFactory(
awsConnection,
new SnsPublication[]
{
Topic = new RoutingKey(typeof(GreetingEvent).FullName.ToValidSNSTopicName())
new SnsPublication
{
Topic = new RoutingKey(typeof(GreetingEvent).FullName
.ToValidSNSTopicName())
},
new SnsPublication
{
Topic = new RoutingKey(
typeof(FarewellEvent).FullName.ToValidSNSTopicName(true)),
SnsAttributes = new SnsAttributes { Type = SnsSqsType.Fifo }
}
}
}
).Create();

services.AddBrighter()
.UseExternalBus((configure) =>
{
configure.ProducerRegistry = producerRegistry;
})
.AutoFromAssemblies(typeof(GreetingEvent).Assembly);
}
).Create();

services.AddHostedService<RunCommandProcessor>();
}
services.AddBrighter()
.UseExternalBus((configure) =>
{
configure.ProducerRegistry = producerRegistry;
})
.AutoFromAssemblies(typeof(GreetingEvent).Assembly);
}

services.AddHostedService<RunCommandProcessor>();
}
)
.UseConsoleLifetime()
.UseSerilog()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region Licence

/* The MIT License (MIT)
Copyright © 2014 Ian Cooper <[email protected]>

Expand Down Expand Up @@ -58,21 +59,36 @@ public static async Task Main(string[] args)
new ChannelName(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
new RoutingKey(typeof(GreetingEvent).FullName.ToValidSNSTopicName()),
bufferSize: 10,
timeOut: TimeSpan.FromMilliseconds(20),
timeOut: TimeSpan.FromMilliseconds(20),
lockTimeout: 30),
new SqsSubscription<FarewellEvent>(new SubscriptionName("paramore.example.farewell"),
new ChannelName(typeof(FarewellEvent).FullName.ToValidSNSTopicName(true)),
new RoutingKey(typeof(FarewellEvent).FullName.ToValidSNSTopicName(true)),
bufferSize: 10,
timeOut: TimeSpan.FromMilliseconds(20),
lockTimeout: 30)
};

//create the gateway
if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
{
var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.EUWest1);
var serviceURL = Environment.GetEnvironmentVariable("LOCALSTACK_SERVICE_URL");
var region = string.IsNullOrWhiteSpace(serviceURL) ? RegionEndpoint.EUWest1 : RegionEndpoint.USEast1;
var awsConnection = new AWSMessagingGatewayConnection(credentials, region,
cfg =>
{
if (!string.IsNullOrWhiteSpace(serviceURL))
{
cfg.ServiceURL = serviceURL;
}
});

services.AddServiceActivator(options =>
{
options.Subscriptions = subscriptions;
options.DefaultChannelFactory = new ChannelFactory(awsConnection);
})
.AutoFromAssemblies();
{
options.Subscriptions = subscriptions;
options.DefaultChannelFactory = new ChannelFactory(awsConnection);
})
.AutoFromAssemblies();
}

services.AddHostedService<ServiceActivatorHostedService>();
Expand All @@ -82,11 +98,6 @@ public static async Task Main(string[] args)
.Build();

await host.RunAsync();




}
}
}

19 changes: 15 additions & 4 deletions samples/TaskQueue/AWSTaskQueue/GreetingsSender/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region Licence

/* The MIT License (MIT)
Copyright © 2017 Ian Cooper <[email protected]>

Expand All @@ -22,6 +23,7 @@ THE SOFTWARE. */

#endregion

using System;
using System.Transactions;
using Amazon;
using Amazon.Runtime.CredentialManagement;
Expand All @@ -48,10 +50,18 @@ static void Main(string[] args)

var serviceCollection = new ServiceCollection();
serviceCollection.AddSingleton<ILoggerFactory>(new SerilogLoggerFactory());

if (new CredentialProfileStoreChain().TryGetAWSCredentials("default", out var credentials))
{
var awsConnection = new AWSMessagingGatewayConnection(credentials, RegionEndpoint.EUWest1);
var serviceURL = Environment.GetEnvironmentVariable("LOCALSTACK_SERVICE_URL");
var region = string.IsNullOrWhiteSpace(serviceURL) ? RegionEndpoint.EUWest1 : RegionEndpoint.USEast1;
var awsConnection = new AWSMessagingGatewayConnection(credentials, region, cfg =>
{
if (!string.IsNullOrWhiteSpace(serviceURL))
{
cfg.ServiceURL = serviceURL;
}
});

var producerRegistry = new SnsProducerRegistryFactory(
awsConnection,
Expand All @@ -64,7 +74,7 @@ static void Main(string[] args)
}
}
).Create();

serviceCollection.AddBrighter()
.UseExternalBus((configure) =>
{
Expand All @@ -76,7 +86,8 @@ static void Main(string[] args)

var commandProcessor = serviceProvider.GetService<IAmACommandProcessor>();

commandProcessor.Post(new GreetingEvent("Ian"));
commandProcessor.Post(new GreetingEvent("Ian says: Hi there!"));
commandProcessor.Post(new FarewellEvent("Ian says: See you later!"));
}
}
}
Expand Down
57 changes: 15 additions & 42 deletions src/Paramore.Brighter.MessagingGateway.AWSSQS/AWSClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#region Licence

/* The MIT License (MIT)
Copyright © 2022 Ian Cooper <[email protected]>

Expand Down Expand Up @@ -31,68 +32,40 @@ THE SOFTWARE. */

namespace Paramore.Brighter.MessagingGateway.AWSSQS;

internal class AWSClientFactory
internal class AWSClientFactory(
AWSCredentials credentials,
RegionEndpoint region,
Action<ClientConfig>? clientConfigAction)
{
private readonly AWSCredentials _credentials;
private readonly RegionEndpoint _region;
private readonly Action<ClientConfig>? _clientConfigAction;

public AWSClientFactory(AWSMessagingGatewayConnection connection)
: this(connection.Credentials, connection.Region, connection.ClientConfigAction)
{
_credentials = connection.Credentials;
_region = connection.Region;
_clientConfigAction = connection.ClientConfigAction;
}

public AWSClientFactory(AWSCredentials credentials, RegionEndpoint region, Action<ClientConfig>? clientConfigAction)
{
_credentials = credentials;
_region = region;
_clientConfigAction = clientConfigAction;
}

public AmazonSimpleNotificationServiceClient CreateSnsClient()
{
var config = new AmazonSimpleNotificationServiceConfig
{
RegionEndpoint = _region
};
var config = new AmazonSimpleNotificationServiceConfig { RegionEndpoint = region };

if (_clientConfigAction != null)
{
_clientConfigAction(config);
}
clientConfigAction?.Invoke(config);

return new AmazonSimpleNotificationServiceClient(_credentials, config);
return new AmazonSimpleNotificationServiceClient(credentials, config);
}

public AmazonSQSClient CreateSqsClient()
{
var config = new AmazonSQSConfig
{
RegionEndpoint = _region
};
var config = new AmazonSQSConfig { RegionEndpoint = region };

if (_clientConfigAction != null)
{
_clientConfigAction(config);
}
clientConfigAction?.Invoke(config);

return new AmazonSQSClient(_credentials, config);
return new AmazonSQSClient(credentials, config);
}

public AmazonSecurityTokenServiceClient CreateStsClient()
{
var config = new AmazonSecurityTokenServiceConfig
{
RegionEndpoint = _region
};
var config = new AmazonSecurityTokenServiceConfig { RegionEndpoint = region };

if (_clientConfigAction != null)
{
_clientConfigAction(config);
}
clientConfigAction?.Invoke(config);

return new AmazonSecurityTokenServiceClient(_credentials, config);
return new AmazonSecurityTokenServiceClient(credentials, config);
}
}
Loading
Loading