score:1
one solution is to add an actor to your actorsystem that listens for a particular signal and calls shutdown:
import akka.actor.{actor, props}
object shutdownmessage
object killswitchactor {
def props : props = props[killswitchactor]
}
class killswitchactor extends actor {
def receive = {
case shutdownmessage => context.system.shutdown()
case _ => {}
}
}//end class killswitchactor
then you simply setup your killswitchactor:
import akka.actor.actorsystem
val actorsystem : actorsystem = actorsystem("testkillswitch")
val killref = actorsystem actorof killswitchactor.props
//"say hello to my little friend!" - tony montana
if(someterminatingcondition) { killref ! shutdownmessage }
score:4
you could add to your main method
runtime.getruntime.addshutdownhook(new thread() {
override def run() {
system.shutdown()
system.awaittermination()
}
})
your app will wait until actor system will be shutt down and all poststop
callbacks in your actors will be executed.
score:31
you can add shutdown hook:
// import scala.concurrent.duration._
//shutdown hook
scala.sys.addshutdownhook {
logger.info("terminating...")
actorsystem.terminate()
await.result(actorsystem.whenterminated, 30 seconds)
logger.info("terminated... bye")
}
Source: stackoverflow.com
Related Query
- How to stop gracefully the actor system for an akka-http server that must be deployed.
- How to wait for Akka actor system to terminate?
- How to get a Promise (or Future) by asking an Akka Actor considering that the result is NOT available as a response from the same message
- How to test controllers that has akka system injected for Play framework?
- Akka HTTP WebSocket Server How to declare Source based on Sink content when building Flow for message handlement?
- How to get the current server request timeout in Akka HTTP
- How to use http client in an Akka actor that has a service?
- How can I get the name of an Akka actor from within the actor itself?
- Scala: How can I install a package system wide for working with in the repl?
- How do I write a JSON Format for an object in the Java library that doesn't have an apply method?
- How is "become" implemented in languages that support the actor model?
- How to get the actor system reference from inside the actor
- How to test that Akka actor was created in Scala
- How do I test an Akka actor that sends a message to another actor?
- How to properly stop Akka streams from the outside
- Akka HTTP Websocket, how to identify connections inside of actor
- Akka Actor how to only process the latest message
- How to unit test an Akka actor that sends a message to itself, without using Thread.sleep
- How to make a scala actor 'wait for the signal' but don't loose any messages?
- How to create an Akka Actor given the class name
- How to instantiate a materializer for AkkaStreams when I have a reference to the typed actor system?
- How does the Scala type system know that cons + Nil is exhaustive?
- How to stop Spray server with routing DSL without upgrading to Akka HTTP?
- How to get http request header info from the server side with spray RestAPI
- How do I get the CPU utilization of an akka actor
- Akka actor system with HTTP interface
- In the scala spray framework, how can I create multiple http clients that have different configurations (e.g., timeouts, retries)
- How to create a data frame in a for loop with the variable that is iterating in loop
- How do I wait for an actor to stop during Play Framework shutdown?
- How can I resolve conflicting actor systems while testing akka-http and akka actors at the same spec file?
More Query from same tag
- Why I cant add field to scala implicit class?
- How to pass differnt filenames to spark using scala
- How to keep all elements when aggregating on AggregateMessages on a GraphFrame?
- Implement trait methods using subclasses' types
- scala (scanLeft) - how to (functionally) get a Map with cumulated values / frequency
- Scala convert tuple to JSON using Jerkson
- Handle Akka stream's first element specially
- Scala - tuple type inference fail?
- Deleting Empty Cells from Google SpreadSheet Programatically to avoid 5000000 cell limit error
- ClassNotFoundException spark-submit scala
- Is there any special support for environment variable access in Ammonite?
- sbt migration from 0.13.0 to 1.3.0
- How to convert case class into string
- apache-spark regex extract words from rdd
- ScalaWS in play framework 2.5 and dependency injection of WSClient in custom class
- Navigating between panels in scala
- Split one row into several rows in Spark Scala
- Caused by: java.lang.ClassCastException: Person cannot be cast to Person
- How to parse json to case class with map by jsonter, plokhotnyuk
- Sbt: call subproject's source code from root project
- Pivoting DataFrame - Spark SQL
- Scala jar read external properties file
- How to use operator LIKE between two columns in Scala?
- Case insensitive search in array type column spark dataframe
- calculating the mean ,variation and standard deviation from a training data Set using Spark Scala
- Functor for scala.collection.Map[Int, T]
- " java.lang.NullPointerException" in Clustering using Spark
- Scala: filtering a collection of Options
- Why do I need Scala Enumeration
- Usage of Type parameterization, variance vs Inheritance base class