This library is a ZIO-powered client for AWS SQS. It is built on top of the AWS SDK for Java 2.0.
To use zio-sqs
, add the following line in your build.sbt
file:
libraryDependencies += "dev.zio" %% "zio-sqs" % "0.1.8"
In order to use the connector, you need a SqsAsyncClient
. Refer to the AWS SDK Documentation if you need help.
Use SqsPublisher.send
to publish messages to a queue.
def send(
client: SqsAsyncClient,
queueUrl: String,
msg: String,
settings: SqsPublisherSettings = SqsPublisherSettings()
): Task[Unit]
SqsPublisherSettings
allows your to configure a number of things:
delaySeconds
: see the related page on AWS docs (defaultNone
). This parameter should beNone
for a FIFO queue.messageDeduplicationId
: see the related page on AWS docsmessageGroupId
: see the related page on AWS docsmessageAttributes
: see the related page on AWS docs
import zio.sqs.SqsPublisher
SqsPublisher.send(client, queueUrl, msg)
Use SqsStream.apply
to get a stream of messages from a queue. It returns a ZIO Stream
that you can consume with all the operators available.
def apply(
client: SqsAsyncClient,
queueUrl: String,
settings: SqsStreamSettings = SqsStreamSettings()
): Stream[Throwable, Message]
SqsStreamSettings
allows your to configure a number of things:
autoDelete
: iftrue
, messages will be automatically deleted from the queue when they're consumed by the stream, iffalse
you have to delete them explicitly by callingSqsStream.deleteMessage
(defaulttrue
)stopWhenQueueEmpty
: iftrue
the stream will close when there the queue is empty, iffalse
the stream will go on forever (defaultfalse
)attributeNames
: see the related page on AWS docsmaxNumberOfMessages
: number of messages to query at once from SQS (default1
)messageAttributeNames
: see the related page on AWS docsvisibilityTimeout
: see the related page on AWS docs (default30
)waitTimeSeconds
: see the related page on AWS docs (default20
),
Example:
import zio.sqs.{SqsStream, SqsStreamSettings}
SqsStream(
client,
queueUrl,
SqsStreamSettings(stopWhenQueueEmpty = true, waitTimeSeconds = 3)
).foreach(msg => UIO(println(msg.body)))
The zio.sqs.Utils
object provides a couple helpful functions to create a queue and find a queue URL from its name.
def createQueue(
client: SqsAsyncClient,
name: String,
attributes: Map[QueueAttributeName, String] = Map()
): Task[Unit]
def getQueueUrl(
client: SqsAsyncClient,
name: String
): Task[String]
import java.net.URI
import software.amazon.awssdk.auth.credentials.{ AwsBasicCredentials, StaticCredentialsProvider }
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.sqs.SqsAsyncClient
import zio.{ App, IO, Task, UIO, ZIO }
import zio.sqs.{ SqsPublisher, SqsStream, SqsStreamSettings, Utils }
object TestApp extends App {
override def run(args: List[String]): ZIO[Environment, Nothing, Int] =
(for {
client <- Task {
SqsAsyncClient
.builder()
.region(Region.of("ap-northeast-2"))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create("key", "key"))
)
.build()
}
queueName = "TestQueue"
_ <- Utils.createQueue(client, queueName)
queueUrl <- Utils.getQueueUrl(client, queueName)
_ <- SqsPublisher.send(client, queueUrl, "hello")
_ <- SqsStream(
client,
queueUrl,
SqsStreamSettings(stopWhenQueueEmpty = true, waitTimeSeconds = 3)
).foreach(msg => UIO(println(msg.body)))
} yield 0).foldM(e => UIO(println(e.toString())).as(1), IO.succeed)
}