项目作者: sethfduke

项目描述 :
A library for processing messages from AWS queues with by-name definable processing.
高级语言: C#
项目地址: git://github.com/sethfduke/dotnet-awsqueuebroker.git
创建时间: 2021-02-16T18:38:11Z
项目社区:https://github.com/sethfduke/dotnet-awsqueuebroker

开源协议:MIT License

下载


AwsQueueBroker

The AwsQueueBroker library allows for SQS queue message definitions to be constructed by assigning unique name attributes on each message and attaching messages by that name to an implementation of a message processor class and it’s associated .net object model. This allows the developer to remove all the logic of receiving, sending, and deleting SQS queue messages from their code-base so that the focus can be placed on the actual processing of messages in a structured and repeatable manner.

Usage

The library works by associating an instance of the abstract QProcessor class with a unique message name and a .Net class that represents the body of that particular message. The QBroker can be setup with any number of named message types and proecssors.

Documentation

Generated documentation can be viewed at https://sethfduke.github.io/dotnet-awsqueuebroker/index.html

Example

  1. public class TestMessageModel
  2. {
  3. // This model should match the expected format of the message body.
  4. public string Name { get; set; }
  5. }
  6. public class TestQProcessor : QProcessor<TestMessageModel>
  7. {
  8. public override async Task Received(Message message, ILogger logger = null)
  9. {
  10. // do something here
  11. }
  12. public override async Task<object> Validate(Message message, TestMessageModel body, ILogger logger = null)
  13. {
  14. if (body.Name == "valid")
  15. {
  16. // returning the body (or some modfiied version of it) tells the process it is valid and to continue.
  17. return body;
  18. }
  19. if (body.Name == "throw")
  20. {
  21. // throwing an exception in any method will forward the exception to the Error() method.
  22. throw new Exception("We blew it.");
  23. }
  24. // returning null tells the processor the model was not valid and to stop processing this message.
  25. return null;
  26. }
  27. public override async Task<QReply> Process(Message message, TestMessageModel model, ILogger logger = null)
  28. {
  29. // optionally returning a QReply instance will send a reply QMessage to the specified queue.
  30. // return null if no reply is needed.
  31. return new QReply()
  32. {
  33. Message = new QMessage("reply-message", "Some content"),
  34. ReplyToQueueUrl = "http://localhost:4566/000000000000/qbroker-test-reply-queue"
  35. };
  36. }
  37. public override async Task Error(Message message, Exception exception, ILogger logger = null)
  38. {
  39. // do something here if an exception was thrown in any of the other methods.
  40. }
  41. }

The QBroker can now be setup to process messages that match an assigned name for the above process implementation:

  1. var sqsClient = new AmazonSQSClient(RegionEndpoint.USEast1);
  2. var broker = new QBroker(sqsClient)
  3. .Setup(settings =>
  4. {
  5. // keep fetching messages until there are no more.
  6. settings.FetchUntilEmpty = true;
  7. // delete succesfully processed messages
  8. settings.DeleteIfSuccess = true;
  9. // delete messages marked invalid
  10. settings.DeleteIfInvalid = true;
  11. // delete messages that resulted in an error
  12. settings.DeleteIfError = true;
  13. // number of messages to fetch at a time (1 to 10)
  14. settings.MaxNumberOfMessages = 10;
  15. // polling wait time in seconds
  16. settings.WaitTimeSeconds = 0;
  17. // queue url to poll for messages
  18. settings.QueueUrl = "https://sqs.us-east-2.amazonaws.com/123456789012/MyQueue";
  19. // optional Serilog ILogger
  20. settings.Logger = _createLogger();
  21. })
  22. // add the test processor with a message name attribute of
  23. // test-message-name with TestMessageModel as the expected body.
  24. .AddMessageType<TestMessageModel, TestQProcessor>("test-message-name");
  25. // poll for messages and process
  26. await broker.FetchAsync()

The broker can also be utilized to send messages outside of the scope of a processor if needed using

  1. var model = new TestMessageModel() {
  2. Name = "test"
  3. };
  4. // QMessage constructor can also include additional message attributes to include.
  5. var message = new QMessage(
  6. "test-message-name",
  7. JsonSerializer.Serialize(model));
  8. broker.SendAsync(message, queueUrl));

Dependency Injection

  1. public void ConfigureServices(IServiceCollection services)
  2. {
  3. services.AddSingleton<IQBroker, QBroker>();
  4. }
  1. public class TestQProcessor : QProcessor<TestMessageModel>
  2. {
  3. private IMyDependency _myDependency;
  4. public TestQProcessor(IMyDependency myDependency){
  5. _myDependency = myDependency;
  6. }
  7. public override async Task Received(Message message, ILogger logger = null){
  8. ...
  9. }
  10. public override async Task<object> Validate(Message message, TestMessageModel body, ILogger logger = null)
  11. {
  12. ...
  13. }
  14. public override async Task<QReply> Process(Message message, TestMessageModel model, ILogger logger = null)
  15. {
  16. ...
  17. }
  18. public override async Task Error(Message message, Exception exception, ILogger logger = null)
  19. {
  20. ...
  21. }
  22. }