Akka Actor life cycle
Akka provides life cycle methods for Actor. There are following methods to which you can override and provide specific implementation accordingly.
Akka Actor life cycle methods
- preStart()
- postStop()
- preRestart(reason: Throwable, message: Option[Any])
- postRestart(reason: Throwable)
1) preStart()
This is overridable method, so you can override preStart() method to provide specific implementation for an Actor. It is invoked right after the starting of Actor and when an Actor is first created. In case of restart, it is called by postRestart() method.
Akka Actor preStart() Method Example
Output:
preStart method is called Hello RootActor
2) postStop()
You can override this method also. After stopping an actor, postStop() method is called. It is an asynchronous method. This method is used to release resources after stopping the Actor. It may be used for deregistering this Actor. Messages sent to a stopped actor will be redirected to the deadLetters of the ActorSystem.
Akka Actor PostStop() Method Example
Output:
Hello RootActor stopping Actor postStop method is called
3) preRestart()
Actor may be restarted in case an exception is thrown. When an actor is restarted, preRestart() method is invoked. The preRestart() method is called with the exception that caused the restart. By default it disposes of all children Actors and then calls postStop() method.
Akka Actor preRestart() Method Example
Output:
Hello RootActor preRestart method is called Reason: java.lang.ArithmeticException: / by zero
4) postRestart()
This method is invoked right after restarting of newly created Actor. It is used to allow reinitialization after an Actor crash due to exception.
Akka Actor postRestart() Method Example
Output
Hello RootActor postRestart method is called Reason: java.lang.ArithmeticException: / by zero
Akka Complete LifeCyle Example
Output:
preStart method is called Message received: Hello Stoping actor postStop method is called preRestart method is called Reason: java.lang.ArithmeticException: / by zero preStart method is called postRestart is called Reason: java.lang.ArithmeticException: / by zero [ERROR] [01/12/2017 15:22:18.475] [ActorSystem-akka.actor.default-dispatcher-5] [akka://ActorSystem/user/RootActor] / by zero java.lang.ArithmeticException: / by zero at RootActor$$anonfun$receive$1.applyOrElse(ActorLifeCyle.scala:6) at akka.actor.Actor$class.aroundReceive(Actor.scala:496) at RootActor.aroundReceive(ActorLifeCyle.scala:3) at akka.actor.ActorCell.receiveMessage(ActorCell.scala:526) at akka.actor.ActorCell.invoke(ActorCell.scala:495) at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:257) at akka.dispatch.Mailbox.run(Mailbox.scala:224) at akka.dispatch.Mailbox.exec(Mailbox.scala:234) at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) postStop method is called