AWS SQS Connector

The AWS SQS connector provides Akka Stream sources and sinks for AWS SQS queues.

For more information about AWS SQS please visit the official documentation.

Reported issues

Tagged issues at Github

Artifacts

sbt
libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sqs" % "0.18"
Maven
<dependency>
  <groupId>com.lightbend.akka</groupId>
  <artifactId>akka-stream-alpakka-sqs_2.12</artifactId>
  <version>0.18</version>
</dependency>
Gradle
dependencies {
  compile group: 'com.lightbend.akka', name: 'akka-stream-alpakka-sqs_2.12', version: '0.18'
}

Usage

Sources, Flows and Sinks provided by this connector need a prepared AmazonSQSAsync to load messages from a queue.

Scala
val client: AmazonSQSAsync = AmazonSQSAsyncClientBuilder
  .standard()
  .withCredentials(credentialsProvider)
  .withEndpointConfiguration(new EndpointConfiguration(sqsEndpoint, "eu-central-1"))
  .build()
Java
AmazonSQSAsync client = AmazonSQSAsyncClientBuilder.standard()
  .withCredentials(credentialsProvider)
  .withEndpointConfiguration(
          new AwsClientBuilder.EndpointConfiguration(sqsEndpoint, "eu-central-1"))
  .build();

We will also need an ActorSystem and an ActorMaterializer.

Scala
implicit val system = ActorSystem()
implicit val mat = ActorMaterializer()
Java
system = ActorSystem.create();
materializer = ActorMaterializer.create(system);

This is all preparation that we are going to need.

Stream messages from a SQS queue

Now we can stream AWS Java SDK SQS Message objects from any SQS queue where we have access to by providing the queue URL to the SqsSource factory.

Scala
SqsSource(queue, sqsSourceSettings).take(100).runWith(Sink.seq).map(_ should have size 100)
Java
final CompletionStage<String> cs = SqsSource.create(queueUrl, sqsSourceSettings, sqsClient)
    .map(Message::getBody)
    .runWith(Sink.head(), materializer);
final CompletionStage<String> cs = SqsSource.create(queueUrl, sqsSourceSettings, customSqsClient)
        .map(Message::getBody)
        .take(1)
        .runWith(Sink.head(), materializer);

As you have seen we take the first 100 elements from the stream. The reason for this is, that reading messages from SQS queues never finishes because there is no direct way to determine the end of a queue.

Source configuration

Scala
final case class SqsSourceSettings(
    waitTimeSeconds: Int,
    maxBufferSize: Int,
    maxBatchSize: Int,
    attributeNames: Seq[AttributeName] = Seq(),
    messageAttributeNames: Seq[MessageAttributeName] = Seq(),
    closeOnEmptyReceive: Boolean = false
) {
  require(maxBatchSize <= maxBufferSize, "maxBatchSize must be lower or equal than maxBufferSize")
  // SQS requirements
  require(0 <= waitTimeSeconds && waitTimeSeconds <= 20,
          s"Invalid value ($waitTimeSeconds) for waitTimeSeconds. Requirement: 0 <= waitTimeSeconds <= 20 ")
  require(1 <= maxBatchSize && maxBatchSize <= 10,
          s"Invalid value ($maxBatchSize) for maxBatchSize. Requirement: 1 <= maxBatchSize <= 10 ")
}

Options:

  • maxBatchSize - the maximum number of messages to return (see MaxNumberOfMessages in AWS docs). Default: 10
  • maxBufferSize - internal buffer size used by the Source. Default: 100 messages
  • waitTimeSeconds - the duration for which the call waits for a message to arrive in the queue before returning (see WaitTimeSeconds in AWS docs). Default: 20 seconds
  • closeOnEmptyReceive - the shutdown behavior of the Source. Default: false

An SqsSource can either provide an infinite stream of messages (the default), or can drain its source queue until no further messages are available. The latter behaviour is enabled by setting the closeOnEmptyReceive flag on creation. If set, the Source will receive messages until it encounters an empty reply from the server. It then continues to emit any remaining messages in its local buffer. The stage will complete once the last message has been send downstream.

Note that for short-polling (waitTimeSeconds of 0), SQS may respond with an empty reply even if there are still messages in the queue. This behavior can be prevented by switching to long-polling (by setting waitTimeSeconds to a nonzero value).

Be aware that the SqsSource runs multiple requests to Amazon SQS in parallel. The maximum number of concurrent requests is limited by parallelism = maxBufferSize / maxBatchSize. E.g.: By default maxBatchSize is set to 10 and maxBufferSize is set to 100 so at the maximum, SqsSource will run 10 concurrent requests to Amazon SQS. AmazonSQSAsyncClient uses a fixed thread pool with 50 threads by default. To tune the thread pool used by AmazonSQSAsyncClient you can supply a custom ExecutorService on client creation.

Scala
val customSqsClient: AmazonSQSAsync =
  AmazonSQSAsyncClientBuilder
    .standard()
    .withCredentials(credentialsProvider)
    .withExecutorFactory(new ExecutorFactory {
      override def newExecutor() = Executors.newFixedThreadPool(10)
    })
    .withEndpointConfiguration(new EndpointConfiguration(sqsEndpoint, "eu-central-1"))
    .build()
Java
AmazonSQSAsync customSqsClient =
  AmazonSQSAsyncClientBuilder
    .standard()
    .withCredentials(credentialsProvider)
    .withExecutorFactory(() -> Executors.newFixedThreadPool(10))
    .withEndpointConfiguration(
            new AwsClientBuilder.EndpointConfiguration(sqsEndpoint, "eu-central-1"))
    .build();

Please make sure to configure a big enough thread pool to avoid resource starvation. This is especially important if you share the client between multiple Sources, Sinks and Flows. For the SQS Sinks and Sources the sum of all parallelism (Source) and maxInFlight (Sink) must be less than or equal to the thread pool size.

Stream messages to a SQS queue

Create a sink, that forwards String to the SQS queue.

Scala
val future = Source.single("alpakka").runWith(SqsSink(queue))
Await.ready(future, 1.second)
Java
CompletionStage<Done> done = Source
  .single("alpakka")
  .runWith(SqsSink.create(queueUrl, sqsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Create a sink, that forwards SendMessageRequest to the SQS queue.

Scala
val future = Source.single(new SendMessageRequest().withMessageBody("alpakka")).runWith(SqsSink.messageSink(queue))
Await.ready(future, 1.second)
Java
CompletionStage<Done> done = Source
        .single(new SendMessageRequest().withMessageBody("alpakka"))
        .runWith(SqsSink.messageSink(queueUrl, sqsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Stream messages to a SQS queue with underlying batching

Create a sink, that forwards String to the SQS queue. However, the main difference from the previous use case, it batches items and sends as a one request.

Note: There is also another option to send batch of messages to SQS which is using AmazonSQSBufferedAsyncClient. This client buffers SendMessageRequests under the hood and sends them as a batch instead of sending them one by one. However, beware that AmazonSQSBufferedAsyncClient does not support FIFO Queues. See documentation for client-side buffering.

Scala
val messages = for (i <- 0 until 20) yield s"Message - $i"

val future = Source(messages).runWith(SqsSink.grouped(queue))
Await.ready(future, 1.second)
Java
ArrayList<String> messagesToSend = new ArrayList<>();
for (int i = 0; i < 20; i++) {
    messagesToSend.add("message - " + i);
}

CompletionStage<Done> done = Source
        .from(messagesToSend)
        .runWith(SqsSink.grouped(queueUrl,sqsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Batch configuration

Scala
final case class SqsBatchFlowSettings(maxBatchSize: Int, maxBatchWait: FiniteDuration, concurrentRequests: Int) {
  require(
    maxBatchSize > 0 && maxBatchSize <= 10,
    s"Invalid value for maxBatchSize: $maxBatchSize. It should be 0 < maxBatchSize < 10, due to the Amazon SQS requirements."
  )
}

Options:

  • maxBatchSize - the maximum number of messages in batch to send SQS. Default: 10.
  • maxBatchWait - the maximum duration for which the stage waits until maxBatchSize messages arrived. Sends what is collects at the end of the time period even though the maxBatchSize is not fulfilled. Default: 500 milliseconds
  • concurrentRequests - the number of batches sending to SQS concurrently.

Stream batches of messages to a SQS queue

Create a sink, that forwards Seq[String] to the SQS queue.

Be aware that the size of the batch must be less than or equal to 10 because Amazon SQS has a limit for batch request. If the batch has more than 10 entries, the request will fail.

Scala
val messages = for (i <- 0 until 10) yield s"Message - $i"

val future = Source.single(messages).runWith(SqsSink.batch(queue))
Await.ready(future, 1.second)
Java
ArrayList<String> messagesToSend = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    messagesToSend.add("Message - " + i);
}
Iterable<String> it = messagesToSend;

CompletionStage<Done> done = Source
        .single(it)
        .runWith(SqsSink.batch(queueUrl,sqsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Create a sink, that forwards Seq[SendMessageRequest] to the SQS queue.

Be aware that the size of the batch must be less than or equal to 10 because Amazon SQS has a limit for batch request. If the batch has more than 10 entries, the request will fail.

Scala
val messages = for (i <- 0 until 10) yield new SendMessageRequest().withMessageBody(s"Message - $i")

val future = Source.single(messages).runWith(SqsSink.batchedMessageSink(queue))
Await.ready(future, 1.second)
Java
ArrayList<SendMessageRequest> messagesToSend = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    messagesToSend.add(new SendMessageRequest().withMessageBody("Message - " + i));
}
Iterable<SendMessageRequest> it = messagesToSend;

CompletionStage<Done> done = Source
        .single(it)
        .runWith(SqsSink.batchedMessageSink(queueUrl,sqsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Sink configuration

Scala
final case class SqsSinkSettings(maxInFlight: Int) {
  require(maxInFlight > 0)
}

Options:

  • maxInFlight - maximum number of messages being processed by AmazonSQSAsync at the same time. Default: 10

Message processing with acknowledgement

SqsAckSink provides possibility to acknowledge (delete), ignore, or postpone a message.

Your flow must decide which action to take and push it with message:

  • Delete - delete message from the queue
  • Ignore - ignore the message and let it reappear in the queue after visibility timeout
  • ChangeMessageVisibility(visibilityTimeout: Int) - can be used to postpone a message, or make the message immediately visible to other consumers. See official documentation for more details.

Acknowledge (delete) messages:

Scala
val future = SqsSource(queue)(awsSqsClient)
  .take(1)
  .map { m: Message =>
    (m, MessageAction.Delete)
  }
  .runWith(SqsAckSink(queue)(awsSqsClient))
Java
Tuple2<Message, MessageAction> pair = new Tuple2<>(
        new Message().withBody("test"),
        MessageAction.delete()
);
CompletionStage<Done> done = Source
        .single(pair)
        .runWith(SqsAckSink.create(queueUrl, awsClient), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Ignore messages:

Scala
val result = SqsSource(queue)(awsSqsClient)
  .take(1)
  .map { m: Message =>
    (m, MessageAction.Ignore)
  }
  .via(SqsAckFlow(queue)(awsSqsClient))
  .runWith(TestSink.probe[AckResult])
  .requestNext(1.second)
Java
Tuple2<Message, MessageAction> pair = new Tuple2<>(
        new Message().withBody("test"),
        MessageAction.ignore()
);
CompletionStage<AckResult> stage = Source
        .single(pair)
        .via(SqsAckFlow.create(queueUrl, awsClient))
        .runWith(Sink.head(), materializer);
AckResult result = stage.toCompletableFuture().get(1, TimeUnit.SECONDS);

Change Visibility Timeout of messages:

Scala
val future = SqsSource(queue)(awsSqsClient)
  .take(1)
  .map { m: Message =>
    (m, MessageAction.ChangeMessageVisibility(5))
  }
  .runWith(SqsAckSink(queue)(awsSqsClient))
Java
Tuple2<Message, MessageAction> pair = new Tuple2<>(
        new Message().withBody("test"),
        MessageAction.changeMessageVisibility(12)
);
CompletionStage<Done> done = Source
        .single(pair)
        .runWith(SqsAckSink.create(queueUrl, awsClient), materializer);
done.toCompletableFuture().get(1, TimeUnit.SECONDS);

SqsAckSink configuration

Same as the normal SqsSink:

Scala
final case class SqsAckSinkSettings(maxInFlight: Int) {
  require(maxInFlight > 0)
}

Options:

  • maxInFlight - maximum number of messages being processed by AmazonSQSAsync at the same time. Default: 10

Message processing with acknowledgement with underlying batching

SqsAckFlow.grouped is a flow that can acknowledge (delete), ignore, or postpone messages, but it batches items and sends them as one request per action.

Acknowledge (delete) messages:

Scala
future = SqsSource(queue)(awsSqsClient)
  .take(10)
  .map { m: Message =>
    (m, MessageAction.Delete)
  }
  .via(SqsAckFlow.grouped(queue, SqsBatchAckFlowSettings.Defaults)(awsSqsClient))
  .runWith(Sink.ignore)
Java
List<Tuple2<Message, MessageAction>> messages = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    messages.add(new Tuple2<>(
            new Message().withBody("test"),
            MessageAction.delete()
    ));
}
CompletionStage<Done> done = Source
        .fromIterator(() -> messages.iterator())
        .via(SqsAckFlow.grouped(queueUrl, awsClient))
        .runWith(Sink.ignore(), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Ignore messages:

Scala
val stream = Source(messages)
  .take(10)
  .map { m: Message =>
    (m, MessageAction.Ignore)
  }
  .via(SqsAckFlow.grouped("queue", SqsBatchAckFlowSettings.Defaults))
  .runWith(TestSink.probe[AckResult])
Java
List<Tuple2<Message, MessageAction>> messages = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    messages.add(new Tuple2<>(
            new Message().withBody("test"),
            MessageAction.ignore()
    ));
}
CompletionStage<List<AckResult>> stage = Source
        .fromIterator(() -> messages.iterator())
        .via(SqsAckFlow.grouped(queueUrl, awsClient))
        .runWith(Sink.seq(), materializer);
List<AckResult> result = stage.toCompletableFuture().get(1, TimeUnit.SECONDS);

Change Visibility Timeout of messages:

Scala
future = SqsSource(queue)(awsSqsClient)
  .take(10)
  .map { m: Message =>
    (m, MessageAction.ChangeMessageVisibility(5))
  }
  .via(SqsAckFlow.grouped(queue, SqsBatchAckFlowSettings.Defaults)(awsSqsClient))
  .runWith(Sink.ignore)
Java
List<Tuple2<Message, MessageAction>> messages = new ArrayList<>();
for (int i = 0; i < 10; i++) {
    messages.add(new Tuple2<>(
            new Message().withBody("test"),
            MessageAction.changeMessageVisibility(5)
    ));
}
CompletionStage<Done> done = Source
        .fromIterator(() -> messages.iterator())
        .via(SqsAckFlow.grouped(queueUrl, awsClient))
        .runWith(Sink.ignore(), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Batch configuration

Scala
final case class SqsBatchAckFlowSettings(maxBatchSize: Int, maxBatchWait: FiniteDuration, concurrentRequests: Int) {
  require(concurrentRequests > 0)
  require(
    maxBatchSize > 0 && maxBatchSize <= 10,
    s"Invalid value for maxBatchSize: $maxBatchSize. It should be 0 < maxBatchSize < 10, due to the Amazon SQS requirements."
  )
  def withMaxBatchSize(maxBatchSize: Int): SqsBatchAckFlowSettings = this.copy(maxBatchSize = maxBatchSize)
  def withMaxBatchWait(maxBatchWait: FiniteDuration): SqsBatchAckFlowSettings = this.copy(maxBatchWait = maxBatchWait)
  def withConcurrentRequests(concurrentRequests: Int): SqsBatchAckFlowSettings =
    this.copy(concurrentRequests = concurrentRequests)
}

Options:

  • maxBatchSize - the maximum number of messages in batch to send SQS. Default: 10.
  • maxBatchWait - the maximum duration for which the stage waits until maxBatchSize messages arrived. Sends what is collects at the end of the time period even though the maxBatchSize is not fulfilled. Default: 500 milliseconds
  • concurrentRequests - the number of batches sending to SQS concurrently.

Using SQS as a Flow

You can also build flow stages which put or acknowledge messages in SQS, backpressure on queue response and then forward responses further down the stream. The API is similar to creating Sinks.

Scala (flow)
val future = Source.single(new SendMessageRequest(queue, "alpakka")).via(SqsFlow(queue)).runWith(Sink.ignore)
Java (flow)
CompletionStage<Done> done = Source
        .single(new SendMessageRequest(queueUrl, "alpakka-flow"))
        .via(SqsFlow.create(queueUrl, sqsClient))
        .runWith(Sink.ignore(), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);
CompletionStage<Done> done = Source
        .single(new SendMessageRequest(queueUrl, "alpakka-flow"))
        .via(SqsFlow.create(queueUrl, sqsClient))
        .runWith(Sink.ignore(), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);
Scala (flow with ack)
val future = SqsSource(queue)(awsSqsClient)
  .take(1)
  .map { m: Message =>
    (m, MessageAction.Delete)
  }
  .via(SqsAckFlow(queue)(awsSqsClient))
  .runWith(Sink.ignore)
Java (flow with ack)
Tuple2<Message, MessageAction> pair = new Tuple2<>(
        new Message().withBody("test-ack-flow"),
        MessageAction.delete()
);
CompletionStage<Done> done = Source
        .single(pair)
        .via(SqsAckFlow.create(queueUrl, awsClient))
        .runWith(Sink.ignore(), materializer);

done.toCompletableFuture().get(1, TimeUnit.SECONDS);

Running the example code

The code in this guide is part of runnable tests of this project. You are welcome to edit the code and run it in sbt.

The test code uses embedded ElasticMQ as queuing service which serves an AWS SQS compatible API.

Scala
sbt 'project sqs' test
Java
sbt 'project sqs' test
The source code for this page can be found here.