Stopping the Actor in Akka




What if we want to stop the actor's in Akka. There are various ways to stop the Actor which we will discuss in this tutorial. But the basic ways are calling the actorSystem.stop(anActor) and context.stop(childActor).
We will use the second one if we need to stop the actor from inside the another actor. An actor can also stop itself by calling the context.stop(self)

Eg:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package stop

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

/**
  * Created by yubrajpokharel on 12/13/17.
  */
object stopMessage //extends App
{
  val system = ActorSystem("normalStop")
  val actor = system.actorOf(Props[MyActor], name = "myactor")
  actor ! Message
  system.stop(actor)
  system.terminate()
}

case object Message

class MyActor extends Actor {
  def receive = {
    case Message => println(s"Actor get a call to act")
    case _ => println("Opps wrong number")
  }
}

The other ways to stop actors are PoisionPill or gracefulStop.
let us discuss in detail about these too

with stop method, the actor will continue to process its current message, if there is any, but no additional messages will be processed.

with the PoisonPill message, A PoisonPill message will stop an actor when the message is processed. A PoisonPill message is queued just like an ordinary message and will be handled after other messages queued ahead of it in its mailbox.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package stop

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

/**
  * Created by yubrajpokharel on 12/13/17.
  */

case object Message2

class PoisionPill extends Actor {
  override def receive = {
    case Message2 => println("get an call from director")
    case _ => println("Opps wrong number")
  }

  override def postStop() = {
    println("actor is stopped by poision pill")
  }

}

object runner
  //extends App
{
  val system  = ActorSystem("poisionPill")
  val actor = system.actorOf(Props[PoisionPill], name = "poisionPilledActor")
  actor ! "hi how are you"
  actor ! Message2
  actor ! PoisonPill
  system.stop(actor)
  system.terminate()
}

gracefulStop method Lets you attempt to terminate actors gracefully, waiting for them to timeout. The documentation states that this is a good way to terminate actors in a specific order.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package stop


import java.util.concurrent.TimeUnit

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

import scala.concurrent.{Await, Future}
import scala.concurrent.duration.Duration


/**
  * Created by yubrajpokharel on 12/13/17.
  */
class GraceFul extends Actor{
  def receive = {
    case _ => {
      Thread.sleep(5000)
      println(s"got a message  !!!")
    }
  }

  override def postStop() = println("Actor is Stopped")
}


object testActor
  extends App
{
  val system = ActorSystem("graceful")
  val actor = system.actorOf(Props[GraceFul], name = "graceful")
  actor ! "hi"
  actor ! "hellp"

  try{
    val stopped: Future[Boolean] = gracefulStop(actor, Duration.create(2, TimeUnit.SECONDS))
    Await.result(stopped, Duration.create(3, TimeUnit.SECONDS))
    println("testActor was stopped")
  }catch {
    case e:Exception => println(e.printStackTrace())
  }finally {
    system.terminate()
  }

}

Comments

Popular posts from this blog

Functional Programming in Java - Part 1

Communication between two Actor's In scala