Created
August 6, 2018 09:50
-
-
Save robvadai/cfa1b05095ff64855d5571f09cd5a513 to your computer and use it in GitHub Desktop.
Non-blocking Actor calls
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
[INFO] [08/06/2018 10:46:59.314] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 100 | |
[INFO] [08/06/2018 10:47:01.320] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 100 | |
[INFO] [08/06/2018 10:47:01.320] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 200 | |
[INFO] [08/06/2018 10:47:03.324] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 200 | |
[INFO] [08/06/2018 10:47:03.324] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 300 | |
[INFO] [08/06/2018 10:47:05.328] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 300 |
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 akka.actor.{Actor, ActorLogging} | |
class BlockingActor extends Actor with ActorLogging { | |
override def receive: Receive = { | |
case someNum: Int => | |
log.info(s"Blocking actor received: $someNum") | |
Thread.sleep(2000L) | |
log.info(s"Blocking actor finished: $someNum") | |
} | |
} |
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 akka.actor.{ActorSystem, Props} | |
object BlockingActorTest extends App { | |
implicit val system = ActorSystem("test") | |
implicit val executionContext = system.dispatcher | |
val blocker = system.actorOf(Props[BlockingActor]) | |
blocker ! 100 | |
blocker ! 200 | |
blocker ! 300 | |
} |
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
[INFO] [08/06/2018 10:50:25.708] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor received: 100 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor finished: 100 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor received: 200 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor finished: 200 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 100 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor received: 300 | |
[INFO] [08/06/2018 10:50:25.709] [test-akka.actor.default-dispatcher-4] [akka://test/user/$b] Non-blocking actor finished: 300 | |
[INFO] [08/06/2018 10:50:27.714] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 100 | |
[INFO] [08/06/2018 10:50:27.714] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 200 | |
[INFO] [08/06/2018 10:50:29.718] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 200 | |
[INFO] [08/06/2018 10:50:29.719] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor received: 300 | |
[INFO] [08/06/2018 10:50:31.721] [test-akka.actor.default-dispatcher-3] [akka://test/user/$a] Blocking actor finished: 300 |
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 akka.actor.{Actor, ActorLogging, ActorRef} | |
class NonBlockingActor(blocker: ActorRef) extends Actor with ActorLogging { | |
override def receive: Receive = { | |
case someNum: Int => | |
log.info(s"Non-blocking actor received: $someNum") | |
blocker ! someNum | |
log.info(s"Non-blocking actor finished: $someNum") | |
} | |
} |
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 akka.actor.{ActorSystem, Props} | |
object NonBlockingActorTest extends App { | |
implicit val system = ActorSystem("test") | |
implicit val executionContext = system.dispatcher | |
val blocker = system.actorOf(Props[BlockingActor]) | |
val nonBlocker = system.actorOf(Props(new NonBlockingActor(blocker))) | |
nonBlocker ! 100 | |
nonBlocker ! 200 | |
nonBlocker ! 300 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment