-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
293 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
aws/sqs/src/main/scala/de/commercetools/queue/aws/sqs/SQSAdministration.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package de.commercetools.queue.aws.sqs | ||
|
||
import cats.effect.IO | ||
import de.commercetools.queue.QueueAdministration | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient | ||
import software.amazon.awssdk.services.sqs.model.{CreateQueueRequest, QueueAttributeName} | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
import scala.jdk.CollectionConverters._ | ||
import software.amazon.awssdk.services.sqs.model.DeleteQueueRequest | ||
import software.amazon.awssdk.services.sqs.model.QueueDoesNotExistException | ||
|
||
class SQSAdministration(client: SqsAsyncClient, getQueueUrl: String => IO[String]) extends QueueAdministration { | ||
|
||
override def create(name: String, messageTTL: FiniteDuration, lockTTL: FiniteDuration): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.createQueue( | ||
CreateQueueRequest | ||
.builder() | ||
.queueName(name) | ||
.attributes(Map( | ||
QueueAttributeName.MESSAGE_RETENTION_PERIOD -> messageTTL.toSeconds.toString(), | ||
QueueAttributeName.VISIBILITY_TIMEOUT -> lockTTL.toSeconds.toString()).asJava) | ||
.build()) | ||
} | ||
}.void | ||
|
||
override def delete(name: String): IO[Unit] = | ||
getQueueUrl(name).flatMap { queueUrl => | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.deleteQueue( | ||
DeleteQueueRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.build()) | ||
} | ||
} | ||
}.void | ||
|
||
override def exists(name: String): IO[Boolean] = | ||
getQueueUrl(name).as(true).recover { _: QueueDoesNotExistException => false } | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
aws/sqs/src/main/scala/de/commercetools/queue/aws/sqs/SQSClient.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.commercetools.queue.aws.sqs | ||
|
||
import cats.effect.{IO, Resource} | ||
import de.commercetools.queue.{Deserializer, QueueAdministration, QueueClient, QueuePublisher, QueueSubscriber, Serializer} | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient | ||
import software.amazon.awssdk.regions.Region | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient | ||
import software.amazon.awssdk.services.sqs.model.GetQueueUrlRequest | ||
|
||
import java.net.URI | ||
import scala.concurrent.duration.FiniteDuration | ||
|
||
class SQSClient private (client: SqsAsyncClient) extends QueueClient { | ||
|
||
private def getQueueUrl(name: String): IO[String] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.getQueueUrl(GetQueueUrlRequest.builder().queueName(name).build()) | ||
} | ||
}.map(_.queueUrl) | ||
|
||
override def administration: QueueAdministration = | ||
new SQSAdministration(client, getQueueUrl(_)) | ||
|
||
override def publisher[T: Serializer](name: String): Resource[IO, QueuePublisher[T]] = | ||
Resource.eval(getQueueUrl(name).map(new SQSPublisher(_, client))) | ||
|
||
override def subscriber[T: Deserializer](name: String, lockTTL: FiniteDuration): QueueSubscriber[T] = | ||
new SQSSubscriber[T](lockTTL, getQueueUrl(name), client) | ||
|
||
} | ||
|
||
object SQSClient { | ||
|
||
def apply( | ||
region: Region, | ||
credentials: AwsCredentialsProvider, | ||
endpoint: Option[URI] = None, | ||
httpClient: Option[SdkAsyncHttpClient] = None | ||
): Resource[IO, SQSClient] = | ||
Resource | ||
.fromAutoCloseable { | ||
IO { | ||
val builder = | ||
SqsAsyncClient.builder().region(region).credentialsProvider(credentials) | ||
|
||
endpoint.foreach(builder.endpointOverride(_)) | ||
|
||
httpClient.foreach(builder.httpClient(_)) | ||
|
||
builder.build() | ||
} | ||
} | ||
.map(new SQSClient(_)) | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
aws/sqs/src/main/scala/de/commercetools/queue/aws/sqs/SQSMessageContext.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package de.commercetools.queue.aws.sqs | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient | ||
import de.commercetools.queue.MessageContext | ||
import java.time.Instant | ||
import cats.effect.IO | ||
import software.amazon.awssdk.services.sqs.model.DeleteMessageRequest | ||
import software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest | ||
|
||
class SQSMessageContext[T]( | ||
val payload: T, | ||
val enqueuedAt: Instant, | ||
val metadata: Map[String, String], | ||
receiptHandle: String, | ||
lockTTL: FiniteDuration, | ||
queueUrl: String, | ||
client: SqsAsyncClient) | ||
extends MessageContext[T] { | ||
|
||
override def ack(): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.deleteMessage(DeleteMessageRequest.builder().queueUrl(queueUrl).receiptHandle(receiptHandle).build()) | ||
} | ||
}.void | ||
|
||
override def nack(): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.changeMessageVisibility( | ||
ChangeMessageVisibilityRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.receiptHandle(receiptHandle) | ||
.visibilityTimeout(0) | ||
.build()) | ||
} | ||
}.void | ||
|
||
override def extendLock(): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.changeMessageVisibility( | ||
ChangeMessageVisibilityRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.receiptHandle(receiptHandle) | ||
.visibilityTimeout(lockTTL.toSeconds.toInt) | ||
.build()) | ||
} | ||
}.void | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
aws/sqs/src/main/scala/de/commercetools/queue/aws/sqs/SQSPublisher.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package de.commercetools.queue.aws.sqs | ||
|
||
import cats.effect.IO | ||
import de.commercetools.queue.{QueuePublisher, Serializer} | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient | ||
import software.amazon.awssdk.services.sqs.model.SendMessageRequest | ||
|
||
import scala.concurrent.duration.FiniteDuration | ||
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequest | ||
import software.amazon.awssdk.services.sqs.model.SendMessageBatchRequestEntry | ||
|
||
class SQSPublisher[T](queueUrl: String, client: SqsAsyncClient)(implicit serializer: Serializer[T]) | ||
extends QueuePublisher[T] { | ||
|
||
override def publish(message: T, delay: Option[FiniteDuration]): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
client.sendMessage( | ||
SendMessageRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.messageBody(serializer.serialize(message)) | ||
.delaySeconds(delay.fold(0)(_.toSeconds.toInt)) | ||
.build()) | ||
} | ||
}.void | ||
|
||
override def publish(messages: List[T], delay: Option[FiniteDuration]): IO[Unit] = | ||
IO.fromCompletableFuture { | ||
IO { | ||
val delaySeconds = delay.fold(0)(_.toSeconds.toInt) | ||
client.sendMessageBatch( | ||
SendMessageBatchRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.entries(messages.map { message => | ||
SendMessageBatchRequestEntry | ||
.builder() | ||
.messageBody(serializer.serialize(message)) | ||
.delaySeconds(delaySeconds) | ||
.build() | ||
}: _*) | ||
.build()) | ||
} | ||
}.void | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
aws/sqs/src/main/scala/de/commercetools/queue/aws/sqs/SQSSubscriber.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package de.commercetools.queue.aws.sqs | ||
|
||
import cats.effect.IO | ||
import de.commercetools.queue.{Deserializer, MessageContext, QueueSubscriber} | ||
import fs2.{Chunk, Stream} | ||
import software.amazon.awssdk.services.sqs.SqsAsyncClient | ||
import software.amazon.awssdk.services.sqs.model.{MessageSystemAttributeName, ReceiveMessageRequest} | ||
|
||
import java.time.Instant | ||
import scala.concurrent.duration.FiniteDuration | ||
import scala.jdk.CollectionConverters._ | ||
|
||
class SQSSubscriber[T]( | ||
lockTTL: FiniteDuration, | ||
getQueueUrl: IO[String], | ||
client: SqsAsyncClient | ||
)(implicit deserializer: Deserializer[T]) | ||
extends QueueSubscriber[T] { | ||
|
||
override def messages(batchSize: Int, waitingTime: FiniteDuration): Stream[IO, MessageContext[T]] = | ||
Stream.eval(getQueueUrl).flatMap { queueUrl => | ||
Stream | ||
.repeatEval(IO.fromCompletableFuture { | ||
IO { | ||
client.receiveMessage( | ||
ReceiveMessageRequest | ||
.builder() | ||
.queueUrl(queueUrl) | ||
.maxNumberOfMessages(batchSize) | ||
.visibilityTimeout(lockTTL.toSeconds.toInt) | ||
.waitTimeSeconds(waitingTime.toSeconds.toInt) | ||
.attributeNamesWithStrings(MessageSystemAttributeName.SENT_TIMESTAMP.toString()) | ||
.build()) | ||
} | ||
}) | ||
.map { messages => | ||
Chunk.from(messages.messages().asScala) | ||
} | ||
.unchunks | ||
.evalMap { message => | ||
for { | ||
sentTimestamp <- IO( | ||
Instant.ofEpochMilli(message.attributes().get(MessageSystemAttributeName.SENT_TIMESTAMP).toLong)) | ||
data <- deserializer.deserialize(message.body()) | ||
} yield new SQSMessageContext( | ||
data, | ||
sentTimestamp, | ||
message.attributesAsStrings().asScala.toMap, | ||
message.receiptHandle(), | ||
lockTTL, | ||
queueUrl, | ||
client) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
core/src/main/scala/de/commercetools/queue/QueueSubscriber.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.