Skip to content
zioPublic

Latest commit

 

History

737 Commits

Folders and files

Repository files navigation

ZIO SQS

ZIO SQS is a ZIO-powered client for AWS SQS. It is built on top of the AWS SDK for Java 2.0 via the automatically generated wrappers from zio-aws.

Production Ready CI Badge Sonatype Releases Sonatype Snapshots javadoc ZIO SQS

ZIO SQS enables us to produce and consume elements to/from the Amazon SQS service. It is integrated with ZIO Streams, so we can produce and consume elements in a streaming fashion, element by element or micro-batching.

Installation

ZIO SQS is published for Scala 2.13 and Scala 3 on the JVM only. To use it, we add the following to the build.sbt file:

libraryDependencies ++= Seq(
  // ZIO SQS client: producers, consumer streams and queue helpers (JVM only).
  // It brings the zio-aws SQS client and the Netty HTTP client as dependencies.
  "dev.zio" %% "zio-sqs" % "0.12.7"
)

Example

In this example we produce a stream of ten events to the MyQueue and then consume them from that queue (at-most-once delivery semantics):

import zio._
import zio.aws.core.config.AwsConfig
import zio.aws.netty.NettyHttpClient
import zio.aws.sqs.Sqs
import zio.sqs.producer.{Producer, ProducerEvent}
import zio.sqs.serialization.Serializer
import zio.sqs.{SqsStream, SqsStreamSettings, Utils}
import zio.stream.ZStream

object ProducerConsumerExample extends ZIOAppDefault {
  val queueName = "MyQueue"

  val stream: ZStream[Any, Nothing, ProducerEvent[String]] =
    ZStream.iterate(0)(_ + 1).map(_.toString).map(ProducerEvent(_)).take(10)

  val program: ZIO[Sqs, Throwable, Unit] = for {
    _        <- Utils.createQueue(queueName)
    queueUrl <- Utils.getQueueUrl(queueName)
    producer  = Producer.make(queueUrl, Serializer.serializeString)
    _        <- ZIO.scoped(producer.flatMap(_.sendStream(stream).runDrain))
    _        <- SqsStream(
                  queueUrl,
                  SqsStreamSettings.default
                    .withAutoDelete(true) // at-most-once delivery semantics
                    .withStopWhenQueueEmpty(true)
                    .withWaitTimeSeconds(3)
                ).foreach(msg => Console.printLine(msg.body))
  } yield ()

  def run =
    program.provide(
      Sqs.live,
      AwsConfig.default,
      NettyHttpClient.default
    )
}

To learn the library step by step, start with Getting Started. The examples folder in zio-sqs/src/test/scala/examples has additional examples for at-least-once and at-most-once delivery semantics.

Documentation

Learn more on the ZIO SQS homepage!

Contributing

For the general guidelines, see ZIO contributor's guide.

Code of Conduct

See the Code of Conduct

Support

Come chat with us on Badge-Discord.

License

License

Releases

Used by

Contributors

Languages