Skip to content

Commit 2e421c5

Browse files
committed
[Host.AmazonSQS] Add SNS support
Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
1 parent d6024e0 commit 2e421c5

File tree

46 files changed

+2262
-528
lines changed

Some content is hidden

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

46 files changed

+2262
-528
lines changed

docs/provider_amazon_sqs.md

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# Amazon SQS Provider for SlimMessageBus <!-- omit in toc -->
1+
# SlimMessageBus - Amazon SQS and SNS Transport <!-- omit in toc -->
22

33
Before diving into this provider documentation, please make sure to read the [Introduction](intro.md).
44

55
### Table of Contents
66

77
- [Configuration](#configuration)
8-
- [Amazon SNS](#amazon-sns)
98
- [Producing Messages](#producing-messages)
109
- [Consuming Messages](#consuming-messages)
1110
- [Consumer Context](#consumer-context)
@@ -15,11 +14,14 @@ Before diving into this provider documentation, please make sure to read the [In
1514
- [Producing Request Messages](#producing-request-messages)
1615
- [Handling Request Messages](#handling-request-messages)
1716
- [Topology Provisioning](#topology-provisioning)
17+
- [Future Ideas](#future-ideas)
18+
- [Application-to-Person (A2P) Support](#application-to-person-a2p-support)
1819

1920
## Configuration
2021

21-
To configure Amazon SQS as your transport provider, you need to specify the AWS region and choose an authentication method:
22+
To configure Amazon SQS / SNS as the transport provider, you need to specify the AWS region and choose an authentication method:
2223

24+
- **Ambient Credential**: Connect to AWS using default credentials that are pulled from the environment variables (e.g. Fargate, ECS, EC2, etc.).
2325
- **Static Credentials**: [Learn more](https://docs.aws.amazon.com/sdkref/latest/guide/access-iam-users.html)
2426
- **Temporary Credentials**: [Learn more](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS)
2527

@@ -35,21 +37,23 @@ services.AddSlimMessageBus((mbb) =>
3537
cfg.UseRegion(Amazon.RegionEndpoint.EUCentral1);
3638

3739
// Use static credentials: https://docs.aws.amazon.com/sdkref/latest/guide/access-iam-users.html
38-
cfg.UseCredentials(accessKey, secretAccessKey);
40+
cfg.UseStaticCredentials(accessKey, secretAccessKey, SqsMessageBusMode.All);
41+
42+
// Use default credentials pulled from environment variables (EC2, ECS, Fargate, etc.):
43+
// cfg.UseDefaultCredentials(); // This is the default, so you can skip this line if you want to use the default credentials.
3944
4045
// Use temporary credentials: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS
41-
//cfg.UseTemporaryCredentials(roleArn, roleSessionName);
46+
// cfg.UseTemporaryCredentials(roleArn, roleSessionName);
4247
4348
AdditionalSqsSetup(cfg);
4449
});
4550
});
4651
```
4752

48-
For an example configuration, check out this file: [`SqsMessageBusSettings`](../src/SlimMessageBus.Host.AmazonSQS/SqsMessageBusSettings.cs). The settings allow you to customize the SQS client object and control topology provisioning for advanced scenarios.
49-
50-
## Amazon SNS
53+
For an example configuration, check out this file: [`SqsMessageBusSettings`](../src/SlimMessageBus.Host.AmazonSQS/SqsMessageBusSettings.cs). The settings allow you to customize the SQS and SNS client object and control topology provisioning for advanced scenarios.
5154

52-
Support for Amazon SNS (Simple Notification Service) will be added soon to this transport plugin.
55+
The plugin supports both SQS (Simple Queue Service Queues) and SNS (Simple Notification Service).
56+
However, if you want to use just one specify the `mode` parameter in either `.UseStaticCredentials(mode: SqsMessageBusMode.Sqs)` or `.UseStaticCredentials(mode: SqsMessageBusMode.Sns)`.
5357

5458
## Producing Messages
5559

@@ -72,7 +76,6 @@ TMessage msg;
7276

7377
// Send msg to "some-queue"
7478
await bus.Publish(msg, "some-queue");
75-
7679
// OR
7780
7881
// Send msg to "some-topic"
@@ -102,35 +105,36 @@ Note that if no explicit configuration is provided, the system assumes the messa
102105

103106
## Consuming Messages
104107

105-
To consume messages of type `TMessage` by `TConsumer` from an Amazon SNS topic named `some-topic`:
108+
To consume messages of type `TMessage` by `TConsumer` from an Amazon SQS queue named `some-queue`:
106109

107110
```csharp
108111
mbb.Consume<TMessage>(x => x
109-
.Queue("some-topic")
112+
.Queue("some-queue")
110113
//.WithConsumer<TConsumer>());
111114
```
112115

113-
To consume messages from an Amazon SQS queue named `some-queue`:
116+
To consume messages from an Amazon SQS queue `some-queue` that is subscribed to a `some-topic` Amazon SNS topic:
114117

115118
```csharp
116119
mbb.Consume<TMessage>(x => x
117120
.Queue("some-queue")
121+
.SubscribeToTopic("some-topic")
118122
//.WithConsumer<TConsumer>());
119123
```
120124

125+
> The `.SubscribeToTopic()` will create a subscription on the SNS and setup a policy on the SQS queue to accept messages from that topic.
126+
121127
### Consumer Context
122128

123-
The consumer can implement the `IConsumerWithContext` interface to access native Amazon SQS messages:
129+
The consumer can get the `IConsumerContext` interface injected to access native Amazon SQS messages:
124130

125131
```csharp
126-
public class PingConsumer : IConsumer<PingMessage>, IConsumerWithContext
132+
public class PingConsumer(IConsumerContext context) : IConsumer<PingMessage>
127133
{
128-
public IConsumerContext Context { get; set; }
129-
130134
public Task OnHandle(PingMessage message, CancellationToken cancellationToken)
131135
{
132136
// Access the native Amazon SQS message:
133-
var transportMessage = Context.GetTransportMessage(); // Amazon.SQS.Model.Message type
137+
var transportMessage = context.GetTransportMessage(); // Amazon.SQS.Model.Message type
134138
}
135139
}
136140
```
@@ -161,6 +165,7 @@ For a consumer:
161165
mbb.Consume<TMessage>(x => x
162166
.WithConsumer<TConsumer>()
163167
.Queue("some-queue")
168+
.EnableFifo()
164169
.MaxMessageCount(10)
165170
.Instances(1));
166171
```
@@ -283,4 +288,10 @@ mbb.WithProviderAmazonSQS(cfg =>
283288

284289
> By default, both flags are enabled (`true`).
285290

286-
This flexibility allows you to define ownership of queues/topics clearlye.g., producers handle queue creation while consumers manage subscriptions.
291+
This flexibility allows you to define ownership of queues/topics clearlye.g., producers handle queue creation while consumers manage subscriptions.
292+
293+
## Future Ideas
294+
295+
### Application-to-Person (A2P) Support
296+
297+
The SNS supports sending SMS messages and Email to persons. While this is not a typical system to system communication, there could be few configuration and mapping features that could be added to leverage that capability from SMB. If you need that, please raise an issue (feature request).

docs/provider_amazon_sqs.t.md

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
# Amazon SQS Provider for SlimMessageBus <!-- omit in toc -->
1+
# SlimMessageBus - Amazon SQS and SNS Transport <!-- omit in toc -->
22

33
Before diving into this provider documentation, please make sure to read the [Introduction](intro.md).
44

55
### Table of Contents
66

77
- [Configuration](#configuration)
8-
- [Amazon SNS](#amazon-sns)
98
- [Producing Messages](#producing-messages)
109
- [Consuming Messages](#consuming-messages)
1110
- [Consumer Context](#consumer-context)
@@ -15,11 +14,14 @@ Before diving into this provider documentation, please make sure to read the [In
1514
- [Producing Request Messages](#producing-request-messages)
1615
- [Handling Request Messages](#handling-request-messages)
1716
- [Topology Provisioning](#topology-provisioning)
17+
- [Future Ideas](#future-ideas)
18+
- [Application-to-Person (A2P) Support](#application-to-person-a2p-support)
1819

1920
## Configuration
2021

21-
To configure Amazon SQS as your transport provider, you need to specify the AWS region and choose an authentication method:
22+
To configure Amazon SQS / SNS as the transport provider, you need to specify the AWS region and choose an authentication method:
2223

24+
- **Ambient Credential**: Connect to AWS using default credentials that are pulled from the environment variables (e.g. Fargate, ECS, EC2, etc.).
2325
- **Static Credentials**: [Learn more](https://docs.aws.amazon.com/sdkref/latest/guide/access-iam-users.html)
2426
- **Temporary Credentials**: [Learn more](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_temp_use-resources.html#RequestWithSTS)
2527

@@ -29,11 +31,10 @@ using SlimMessageBus.Host.AmazonSQS;
2931

3032
@[:cs](../src/Tests/SlimMessageBus.Host.AmazonSQS.Test/SqsMessageBusIt.cs,ExampleSetup)
3133

32-
For an example configuration, check out this file: [`SqsMessageBusSettings`](../src/SlimMessageBus.Host.AmazonSQS/SqsMessageBusSettings.cs). The settings allow you to customize the SQS client object and control topology provisioning for advanced scenarios.
34+
For an example configuration, check out this file: [`SqsMessageBusSettings`](../src/SlimMessageBus.Host.AmazonSQS/SqsMessageBusSettings.cs). The settings allow you to customize the SQS and SNS client object and control topology provisioning for advanced scenarios.
3335

34-
## Amazon SNS
35-
36-
Support for Amazon SNS (Simple Notification Service) will be added soon to this transport plugin.
36+
The plugin supports both SQS (Simple Queue Service Queues) and SNS (Simple Notification Service).
37+
However, if you want to use just one specify the `mode` parameter in either `.UseStaticCredentials(mode: SqsMessageBusMode.Sqs)` or `.UseStaticCredentials(mode: SqsMessageBusMode.Sns)`.
3738

3839
## Producing Messages
3940

@@ -56,7 +57,6 @@ TMessage msg;
5657

5758
// Send msg to "some-queue"
5859
await bus.Publish(msg, "some-queue");
59-
6060
// OR
6161
6262
// Send msg to "some-topic"
@@ -86,35 +86,36 @@ Note that if no explicit configuration is provided, the system assumes the messa
8686

8787
## Consuming Messages
8888

89-
To consume messages of type `TMessage` by `TConsumer` from an Amazon SNS topic named `some-topic`:
89+
To consume messages of type `TMessage` by `TConsumer` from an Amazon SQS queue named `some-queue`:
9090

9191
```csharp
9292
mbb.Consume<TMessage>(x => x
93-
.Queue("some-topic")
93+
.Queue("some-queue")
9494
//.WithConsumer<TConsumer>());
9595
```
9696

97-
To consume messages from an Amazon SQS queue named `some-queue`:
97+
To consume messages from an Amazon SQS queue `some-queue` that is subscribed to a `some-topic` Amazon SNS topic:
9898

9999
```csharp
100100
mbb.Consume<TMessage>(x => x
101101
.Queue("some-queue")
102+
.SubscribeToTopic("some-topic")
102103
//.WithConsumer<TConsumer>());
103104
```
104105

106+
> The `.SubscribeToTopic()` will create a subscription on the SNS and setup a policy on the SQS queue to accept messages from that topic.
107+
105108
### Consumer Context
106109

107-
The consumer can implement the `IConsumerWithContext` interface to access native Amazon SQS messages:
110+
The consumer can get the `IConsumerContext` interface injected to access native Amazon SQS messages:
108111

109112
```csharp
110-
public class PingConsumer : IConsumer<PingMessage>, IConsumerWithContext
113+
public class PingConsumer(IConsumerContext context) : IConsumer<PingMessage>
111114
{
112-
public IConsumerContext Context { get; set; }
113-
114115
public Task OnHandle(PingMessage message, CancellationToken cancellationToken)
115116
{
116117
// Access the native Amazon SQS message:
117-
var transportMessage = Context.GetTransportMessage(); // Amazon.SQS.Model.Message type
118+
var transportMessage = context.GetTransportMessage(); // Amazon.SQS.Model.Message type
118119
}
119120
}
120121
```
@@ -145,6 +146,7 @@ For a consumer:
145146
mbb.Consume<TMessage>(x => x
146147
.WithConsumer<TConsumer>()
147148
.Queue("some-queue")
149+
.EnableFifo()
148150
.MaxMessageCount(10)
149151
.Instances(1));
150152
```
@@ -268,3 +270,9 @@ mbb.WithProviderAmazonSQS(cfg =>
268270
> By default, both flags are enabled (`true`).
269271

270272
This flexibility allows you to define ownership of queues/topics clearlye.g., producers handle queue creation while consumers manage subscriptions.
273+
274+
## Future Ideas
275+
276+
### Application-to-Person (A2P) Support
277+
278+
The SNS supports sending SMS messages and Email to persons. While this is not a typical system to system communication, there could be few configuration and mapping features that could be added to leverage that capability from SMB. If you need that, please raise an issue (feature request).

src/Host.Plugin.Properties.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<Import Project="Common.NuGet.Properties.xml" />
55

66
<PropertyGroup>
7-
<Version>3.3.0-rc100</Version>
7+
<Version>3.3.0-rc200</Version>
88
</PropertyGroup>
99

1010
</Project>

src/Samples/Sample.Simple.ConsoleApp/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
void ConfigureMessageBus(MessageBusBuilder mbb, IConfiguration configuration)
2727
{
2828
// Choose your transport
29-
var provider = Provider.AzureServiceBus;
29+
var provider = Provider.Memory;
3030

3131
// Provide your event hub-names OR kafka/service bus topic names
3232
var topicForAddCommand = "add-command";
@@ -224,7 +224,7 @@ void AddSsl(ClientConfig c)
224224
builder.WithProviderAmazonSQS(cfg =>
225225
{
226226
cfg.UseRegion(Amazon.RegionEndpoint.EUCentral1);
227-
cfg.UseCredentials(accessKey, secretAccess);
227+
cfg.UseStaticCredentials(accessKey, secretAccess);
228228
});
229229
break;
230230
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace SlimMessageBus.Host.AmazonSQS;
2+
3+
public abstract class AbstractClientProvider<TClient>(TClient client) : IDisposable
4+
where TClient : IDisposable
5+
{
6+
private bool _disposedValue;
7+
8+
public TClient Client => client;
9+
10+
public virtual Task EnsureClientAuthenticated() => Task.CompletedTask;
11+
12+
#region Dispose Pattern
13+
14+
protected virtual void Dispose(bool disposing)
15+
{
16+
if (!_disposedValue)
17+
{
18+
if (disposing)
19+
{
20+
client?.Dispose();
21+
}
22+
23+
_disposedValue = true;
24+
}
25+
}
26+
27+
public void Dispose()
28+
{
29+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
30+
Dispose(disposing: true);
31+
GC.SuppressFinalize(this);
32+
}
33+
34+
#endregion
35+
36+
}

0 commit comments

Comments
 (0)