Akka Actor LifeCycle

akka
Reading Time: 2 minutes

In this blog we’ll discuss about the Akka Actor Lifecycle. First, let’s understand the Akka actors.

Akka Actor

Actors are the unit of execution in Akka. The Actor model is an abstraction that makes it easier to write correct concurrent, parallel and distributed systems. Its only responsibility is to properly recognise the type of message it has received and take action accordingly.

Actor Lifecycle

The lifecycle of an actor starts automatically after its creation. The lifecycle of an actor begins when the actorOf() method is called either by the system or by the context. Akka provides various methods for Actor, which we can override and implement accordingly.

Actor lifecycle methods

preStart()

We can override preStart() method to provide a specific implementation for an Actor. Here we can allocate resources before an actor start like database connections, files, etc.





import akka.actor.{Actor, ActorSystem, Props}

object Application extends App{
  val system = ActorSystem("MyActor")
  val actor = system.actorOf(Props[Demo], "actor")
  actor ! "Amit"
}

class Demo extends Actor{
  override def receive: Receive ={
    case name => println("Hello " + name)
  }

  override def preStart(): Unit = {
    println("preStart() method called...")
  }
}

postStop()

We can override postStart() method to provide a specific implementation for an Actor. Once this method call, the actor will stop receiving any new messages and the current messages will redirect into the dead letter mailbox. This method is called after the stopping of actor to release resources.

import akka.actor.{Actor, ActorSystem, Props}


class Demo extends Actor{
  override def receive: Receive ={
    case name => println("Hello " + name)
  }

  override def postStop(): Unit = {
    println("postStop() method called...")
  }
}

object Application extends App{
  val system = ActorSystem("MyActorSystem")
  val actor = system.actorOf(Props[Demo], "actor")
  actor ! "Amit"
  system.stop(actor)
}

preRestart()

We can override preRestart() method to provide a specific implementation. This method calls when an actor is about to restart due to some exception. This method helps to handle in case of an actor fails due to some exception.

import akka.actor.{Actor, ActorSystem, Props}


class Demo extends Actor{
  override def receive: Receive ={
    case number:Int => number/0
    case name => println("Hello " + name)
  }

  override def preRestart(reason:Throwable, message: Option[Any]): Unit = {
    println("preRestart() method called...")
    println("Reason : " + reason)
  }
}

object Application extends App{
  val system = ActorSystem("MyActorSystem")
  val actor = system.actorOf(Props[Demo], "actor")
  actor ! 20
}

postRestart()

We can override postRestart() method as well to provide a specific implementation for an Actor when an actor has restarted due to some the exception. It allows an actor to reinitialise after an Actor fail or stopped working due to exception. Let’s take an example

package org.knoldus.bootstrap

import akka.actor.{Actor, ActorSystem, Props}


class Demo extends Actor{
  override def receive: Receive ={
    case number:Int => number/0
    case name => println("Hello " + name)
  }

  override def postRestart(reason:Throwable): Unit = {
    println("postRestart() method called...")
    println("Reason : " + reason)
  }
}

object Application extends App{
  val system = ActorSystem("MyActorSystem")
  val actor = system.actorOf(Props[Demo], "actor")
  actor ! 20
}

References

https://doc.akka.io/docs/akka/current/typed/actor-lifecycle.html