Created
September 15, 2020 08:11
-
-
Save Vilkina/d8752bbe448649b6f1a42dd0bfcaf179 to your computer and use it in GitHub Desktop.
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
import zio._ | |
import zio.console._ | |
import zio.kafka.consumer._ | |
import zio.kafka.serde._ | |
val subscription: Subscription = Subscription.topics("topic") | |
val readKafka: RIO[Console with Blocking with Clock, Unit] = | |
Consumer.consumeWith(settings, subscription, Serde.string, Serde.string) { | |
case (key, value) => | |
putStrLn(s"Received message ${key}: ${value}") | |
// Perform an effect with the received message | |
} | |
private val readKafka2: RIO[ZEnv with Consumer, Unit] = | |
zio.kafka.consumer.Consumer.withConsumerService( | |
_.consumeWith(subscription, Serde.string, Serde.string)({ | |
case (key, value) => | |
putStrLn(s"Received message ${key}: ${value}") | |
}) | |
) | |
private val service: ZManaged[Clock with Blocking, Throwable, Consumer.Service] = | |
Consumer.make(settings) | |
private val layer: ZLayer[Clock with Blocking, Throwable, Consumer] = ZLayer( | |
service | |
) | |
val readKafkaPrg: URIO[ZEnv, ExitCode] = | |
readKafka2.provideCustomLayer(layer).exitCode | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment