score:28
In Akka 2.4.1 for scala 2.11 it appears to be different again.
system.awaitTermination()
is deprecated and the docs instruct us to use Await.result(system.whenTerminated, timeout)
instead.
As 203 said, system.terminate
is still the way to terminate the system.
Here is some example code I used:
val actorSystem = ActorSystem("ActorSystem")
val myActors = actorSystem.actorOf(Props[MyActor].withRouter(RoundRobinPool(10)), "MyActors")
rainbows.foreach(rainbow => myActors ! rainbow)
Await.ready(actorSystem.whenTerminated, Duration(1, TimeUnit.MINUTES))
Then in the MyActor class I have the line context.system.terminate()
score:0
in Akka 2.3.9, it seems that shutting down an actor system and waiting for it to shut down is a two step process:
- Initiate the shutdown:
actorSystem.shutdown
- Wait for its termination in a blocking manner:
actorSystem.awaitTermination
As an alternative to step (2), possibly (haven't tested these alternatives) you may alternatively poll on isTerminated
or use registerOnTermination
for running some code when it's terminated. So it is worth going through the comments of akka.actor.ActorSystem to fathom and choose between these methods for your implementation.
Perhaps I'm missing other options about the API (?) as Future
return values could have been nicer.
score:0
How about:
import scala.concurrent.Await
import scala.concurrent.duration._
Await.ready(system.terminate(), 5.seconds)
terminate returns a Future:
def terminate(): Future[Terminated]
and you can just await completion of this future.
score:4
I found the solution - just call system.shutdown from the master actor:
context.system.shutdown
score:4
You can also kill the ActorSystem
from the main thread by using system.shutdown
. But that operation is asynchronous. You only need to use system.awaitTermination
if you need to block the main thread until it completes. If your parallelRunners return something useful for the rest of the program, then it would probably be easiest to not block and continue your program.
score:6
Just a side note for those running into this question for its title: Apparently, Akka 2.5 does not support actorSystem.awaitTermination
anymore. Reason being why might be Akka's philosophy of avoiding any blocking calls. Instead, actorSystem.registerOnTermination(...)
can be used as a non-blocking way to do actions while an ActorSystem
is being shutdown.
Nonetheless, you can still wait for your actor system to complete via a Future
provided by ActorSystem.whenTerminated
:
val system = new ActorSystem("parallelRunners")
val master = system.actorOf(Props[Master])
master ! Start
import scala.concurrent.Await
import scala.concurrent.duration._
Await.ready(system.whenTerminated, 365.days)
score:7
As of Akka 2.4, you should use system.awaitTermination()
which returns a Future[Terminated]
you can wait for.
In order to terminate the system, you should use ActorSystem.terminate
(e.g. context.system.terminate()
from within an actor when it is finished.
Source: Release Notes
Source: stackoverflow.com
Related Query
- How to wait for Akka actor system to terminate?
- Wait for akka actor system to become available
- How to wait for file upload stream to complete in Akka actor
- How to terminate Akka actor system from a main method?
- How to wait for Akka actor to start during tests?
- Akka Actor - wait for some time to expect a message, otherwise send a message out
- How to stop gracefully the actor system for an akka-http server that must be deployed.
- How do I wait for an actor to stop during Play Framework shutdown?
- akka actor is suitable for online serving system such as seach?
- How to test controllers that has akka system injected for Play framework?
- Wait for an actor response indefinitely in a future akka java
- How to run a scheduled functions when akka actor system terminates
- Akka wait for Ack before stopping actor
- Akka TestKit how to wait for the message to be processed
- Failure to Bind to a Remote IP Address for Akka Actor System
- How to implement a REST API with akka typed actor system
- Akka test - wait for actor initialization
- How to Mock akka Actor for unit Test?
- How to wait for several Futures?
- How can I get the name of an Akka actor from within the actor itself?
- Akka - How many instances of an actor should you create?
- How to wait for N seconds between statements in Scala?
- How to get Akka actor by name as an ActorRef?
- How do I create a TestActorRef in Scala for an Actor with constructor params?
- How do I wait for asynchronous tasks to complete in scala?
- Scala: How can I install a package system wide for working with in the repl?
- How do I wait for a Scala future's onSuccess callback to complete?
- How to mock child Actors for testing an Akka system?
- How do I set a system property for my project in sbt?
- Wait for an Actor to exit()
More Query from same tag
- Converting from Long to List[Int]
- Moving Files from One HDfs Directory to Another using Pyspark
- Scala - not using null in a case statement guard
- scala/akka of case class with trait
- How to ensure spark not read same data twice from cassandra
- "/packages cannot be represented as URI"
- XGBoost4J-Spark Error - object dmlc is not a member of package org.apache.spark.ml
- How can I tell if an identifier is invalid in Scala?
- iterating through while loop in scala
- squeryl - saving bigDecimal to database
- scala infix function with additional parameters
- Converting empty string to a None in Scala
- Finagle quickstart client
- Why does a class body affect implicit resolution?
- How to handle domain changes in a event sourced application?
- scala event swing :: chicken or egg
- Stream URL to a local file with fs2
- how to solve this spark-scala sql error message
- Getting sbt-assembly working
- Spark Window Function rangeBetween producing incorrect results
- Workflow of the Higher Order Function in Scala
- AbstractMethodError while running finch/finagle application
- What's the standard way to work with dates and times in Scala? Should I use Java types or there are native Scala alternatives?
- Matching Lscala.Tuple2 in scala
- How to connect to Oracle 11g database using Slick via Play 2/TypeSafe Activator?
- Scala map method on option
- ScalaFX type mismatch on event.getCode()
- Download pdf file from s3 using akka-stream-alpakka and store it as an array of bytes
- Extract Dates and compare them from string in Scala
- Input type must be string type but got ArrayType(StringType,true) error in Spark using Scala