Simple AmazonSQS Transport usage

Component: Amazon SQS Transport
NuGet Package NServiceBus.AmazonSQS (3.x)
Target NServiceBus Version: 6.x

This sample shows basic usage of SQS as a transport for NServiceBus. The application sends an empty message to itself, via the SQS transport, and writes to the console when the message is received.

AWS Setup

Security and access configuration

Add the AWS Access Key ID and AWS Secret Access Key to the following environment variables:

  • Access Key ID in AWS_ACCESS_KEY_ID
  • Secret Access Key in AWS_SECRET_ACCESS_KEY
  • Default Region in AWS_DEFAULT_REGION

See also AWS Account Identifiers, Managing Access Keys for a AWS Account, and IAM Security Credentials.

See also AWS Regions for a list of available regions.

SQS

Several Amazon SQS queues are required to run this sample. These will be created at start-up via the Installer mechanism of NServiceBus. The queues can be seen in the SQS management UI.

  • Samples-Sqs-Simple: The main message processing queue.
  • Samples-Sqs-Simple-Retries: Queue used for Delayed Retries.
  • error: Queue used for Error handling.

S3

A Amazon S3 bucket is required to leverage the S3 Bucket For Large Messages feature of the SQS transport. Create an S3 Bucket and replace the bucketname in the sample startup with the the name of the created bucket.

Endpoint Configuration

Configure the endpoint to use the SQS Transport.

var endpointConfiguration = new EndpointConfiguration("Samples.Sqs.Simple");
var transport = endpointConfiguration.UseTransport<SqsTransport>();
transport.Region("us-east-1");
transport.S3BucketForLargeMessages("bucketname", "my/key/prefix");

Run the sample

Start the endpoint. A message will be sent and received by a handler.

View a message in transit

At startup the endpoint will send two messages. On is a message that falls under the 256kb size limit. The other is above that limit and will leverage the S3 Bucket For Large Messages feature of the SQS transport.

var myMessage = new MyMessage();
await endpointInstance.SendLocal(myMessage)
    .ConfigureAwait(false);

var myLargeMessage = new MyMessage
{
    Data = new byte[257 * 1024]
};
await endpointInstance.SendLocal(myLargeMessage)
    .ConfigureAwait(false);

To view a message in transit change the endpoint to be Send Only. There is commented out code already in this samples configuration.

//TODO: uncomment to view a message in transit
//endpointConfiguration.SendOnly();

Start the endpoint. Since the endpoint is now Send Only, the messages will be written to SQS but not dequeued. The message can now be viewed in the SQS management UI.

The large message contents can be be viewed in the S3 management UI.

See also Receiving and Deleting a Message from an Amazon SQS Queue.

Related Articles


Last modified