Getting Started
JustSaying uses conventions based on type names to determine which queues, topics, and subscriptions it should use. On startup, JustSaying ensures that that infrastructure is ready before beginning to publish or consume any messages.
Events
Events inherit the JustSaying.Models.Message
class, so that we ensure common properties are available on all messages.
Note that events have a maximum serialized size of 256kb
Publishing a Message
When JustSaying is added to the built in DI system, it makes an IMessagePublisher
available that can be used to publish messages.
Handlers
Handlers should implement the interface IHandlerAsync<T>
which JustSaying will call when a message of the specified type is received.
By default, JustSaying will reserve messages it has downloaded for 30 seconds, and messages will be kept in the queue for 4 days. To override these, see the documentation on SqsReadConfiguration
.
Return value
Note that the handler returns a bool
. Handlers should return true if the message was handled successfully, and false if not. If false is returned, then the message won't be deleted from SQS, and will be re-processed by another worker when its visibility timeout expires.
If an exception is thrown, it is equivalent to the handler returning false.
If a message is re-processed too many times, it will be sent to the corresponding error queue for the event (unless the error queue has been disabled).
Dependency Injection
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.DependencyInjection
The easiest way to get started with JustSaying is to plug it into the built in .NET Core container. Other containers can be used alongside the built in one, but we recommend using this API as it is actively tested and maintained.
The code sample below configures the following things:
A Region to use in AWS. This must be specified as there is no 'default' AWS region.
A publisher for an event named
OrderPlacedEvent
A subscriber for an event named
OrderReadyEvent
A handler for the
OrderReadyEvent
calledOrderReadyEventHandler
Note that the AddJustSaying
extension method requires installing the JustSaying.Extensions.DependencyInjection package, which is currently in pre-release.
Startup
Now that we've created an event and handler, and wired it into the DI container, let's start the bus. How this is done is up to you, but here's an example using the built in IHostedService
that comes with .NET Core.
BackgroundService
is a built-in type that implements IHostedService
to provide a simple ExecuteAsync
method with a CancellationToken
.
Note here that we're calling
StartAsync
on both theIMessagingBus
, andIMessagePublisher
. Starting theIMessagePublisher
ensures that all publish infrastructure is created and ready, while starting theIMessagingBus
will firstly ensure that all subscription infrastructure is created and ready, and then begin consuming messages.The only way to stop the bus is to cancel the cancellation token, which will tear down everything.
Last updated