mitrakov

FS2 + RabbitMQ

Apr 17th, 2020
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.68 KB | None | 0 0
  1. import java.util.concurrent.Executors
  2. import cats.effect.{Blocker, ExitCode, IO, IOApp, Resource, Sync}
  3. import dev.profunktor.fs2rabbit.config.Fs2RabbitConfig
  4. import dev.profunktor.fs2rabbit.interpreter.RabbitClient
  5. import dev.profunktor.fs2rabbit.model.{ExchangeName, RoutingKey}
  6.  
  7. object Demo extends IOApp {
  8.  
  9.   val config: Fs2RabbitConfig = Fs2RabbitConfig(
  10.     virtualHost = "/",
  11.     host = "127.0.0.1",
  12.     username = Some("guest"),
  13.     password = Some("guest"),
  14.     port = 5672,
  15.     ssl = false,
  16.     connectionTimeout = 3,
  17.     requeueOnNack = false,
  18.     internalQueueSize = Some(500)
  19.   )
  20.  
  21.   val blockerResource: Resource[IO, Blocker] =
  22.     Resource
  23.       .make(IO(Executors.newCachedThreadPool()))(es => IO(es.shutdown()))
  24.       .map(Blocker.liftExecutorService)
  25.  
  26.   override def run(args: List[String]): IO[ExitCode] = {
  27.     import cats.syntax.functor.toFunctorOps
  28.  
  29.     blockerResource.use { blocker =>
  30.       RabbitClient[IO](config, blocker).flatMap { client =>
  31.         Program.foo[IO](client).as(ExitCode.Success)
  32.       }
  33.     }
  34.   }
  35. }
  36.  
  37. object Program {
  38.   import cats.syntax.flatMap.toFlatMapOps
  39.  
  40.   def foo[F[_]: Sync](client: RabbitClient[F]): F[Unit] = {
  41.     client.createConnectionChannel.use { implicit channel =>
  42.       client.createPublisher(ExchangeName("test"), RoutingKey("")).flatMap { publish =>
  43.         publish("Hello")
  44.       }
  45.     }
  46.   }
  47. }
Add Comment
Please, Sign In to add comment