Don't require full URL for queue/topic #148
brendonparker
started this conversation in
Ideas
Replies: 1 comment
-
I was able to fudge this in using a custom extension. Also made the registration for message types a bit more terse... using AWS.Messaging.Configuration;
using AWS.Messaging.Publishers.SQS;
using AWS.Messaging.Services;
using LearnAwsMessaging.Consumer.Customizations;
using LearnAwsMessaging.Contracts;
using Microsoft.Extensions.DependencyInjection;
namespace LearnAwsMessaging.Consumer;
public static class MessageBusBuilderExtensions
{
// TODO: How can we derive these and not hardcode them?
const string ACCOUNT_ID = "0123456789";
const string REGION_ID = "us-east-1";
public static MessageBusBuilder AddDemoMessages(this MessageBusBuilder builder)
{
// Standard messages
builder.AddSqsQueue("aws-msg-demo")
.RouteMessageType<HelloMessage>()
.RouteMessageType<StartJob>();
// FIFO messages
builder.AddSqsQueue("aws-msg-demo.fifo")
.RouteMessageType<DownloadSales>()
.RouteMessageType<DownloadReceipts>();
// SNS Notifications
builder.AddSnsTopic("aws-msg-demo-job-started").RouteMessageType<JobStarted>();
builder.AddSnsTopic("aws-msg-demo-sales-downloaded").RouteMessageType<SalesDownloaded>();
builder.AddSnsTopic("aws-msg-demo-receipts-downloaded").RouteMessageType<ReceiptsDownloaded>();
return builder;
}
public static SNSPublisherHelper AddSnsTopic(this MessageBusBuilder builder, string topicNameOrArn) =>
new(builder, topicNameOrArn);
public static SQSPublisherHelper AddSqsQueue(this MessageBusBuilder builder, string queueNameOrUrl) =>
new(builder, queueNameOrUrl);
public class SNSPublisherHelper
{
private readonly MessageBusBuilder _builder;
private readonly string _topicArn;
public SNSPublisherHelper(MessageBusBuilder builder, string topicNameOrArn)
{
_builder = builder;
_topicArn = topicNameOrArn.StartsWith("arn:aws:sns")
? topicNameOrArn
: $"arn:aws:sns:{REGION_ID}:{ACCOUNT_ID}:{topicNameOrArn}";
}
public SNSPublisherHelper RouteMessageType<T>()
{
_builder.AddSNSPublisher<T>(_topicArn);
return this;
}
}
public class SQSPublisherHelper
{
private readonly MessageBusBuilder _builder;
private readonly string _queueUrl;
public SQSPublisherHelper(MessageBusBuilder builder, string queueNameOrUrl)
{
_builder = builder;
_queueUrl = queueNameOrUrl.StartsWith("https://sqs")
? queueNameOrUrl
: $"https://sqs.{REGION_ID}.amazonaws.com/{ACCOUNT_ID}/{queueNameOrUrl}";
}
public SQSPublisherHelper RouteMessageType<T>()
{
_builder.AddSQSPublisher<T>(_queueUrl);
return this;
}
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Just started looking at this library and excited for the simplicity it will bring.
One of the first things I ran into is needing to provide the queue url. What if instead of providing:
We could just provide:
And it would assume the currently configured account/region when constructing the URL?
perhaps some other convention is needed, like:
This would remove the need for code to care about where it is being deployed.
Beta Was this translation helpful? Give feedback.
All reactions