Last active
July 29, 2019 23:52
-
-
Save ghostdogpr/aefca7373fc53c274c9e620c7d1824d6 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
def listen(actorSystem: ActorSystem, topic: String): Task[Queue[String]] = | |
for { | |
queue <- Queue.bounded[String](1000) | |
rts <- Task.runtime[Any] | |
_ <- Task(actorSystem.actorOf(Props(new SubscriberActor(topic, rts, queue)))) | |
} yield queue | |
case class MessageEnvelope(msg: String) | |
class SubscriberActor(topic: String, rts: Runtime[Any], queue: Queue[String]) extends Actor { | |
DistributedPubSub(actorSystem).mediator ! Subscribe(topic, self) | |
def receive: PartialFunction[Any, Unit] = { | |
case MessageEnvelope(s) => rts.unsafeRunSync(queue.offer(s)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment