Book Image

Akka Cookbook

By : Vivek Mishra, Héctor Veiga Ortiz
Book Image

Akka Cookbook

By: Vivek Mishra, Héctor Veiga Ortiz

Overview of this book

Akka is an open source toolkit that simplifies the construction of distributed and concurrent applications on the JVM. This book will teach you how to develop reactive applications in Scala using the Akka framework. This book will show you how to build concurrent, scalable, and reactive applications in Akka. You will see how to create high performance applications, extend applications, build microservices with Lagom, and more. We will explore Akka's actor model and show you how to incorporate concurrency into your applications. The book puts a special emphasis on performance improvement and how to make an application available for users. We also make a special mention of message routing and construction. By the end of this book, you will be able to create a high-performing Scala application using the Akka framework.
Table of Contents (18 chapters)
Title Page
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Stopping an actor


It is obvious that an actor has to be shut down gracefully after it has processed all the messages or on application shutdown.

Getting ready

To step through this recipe, we need import the Hello-Akka project in an IDE like IntelliJ Idea. Prerequisites are the same as those in previous recipes.

How to do it...

  1. Create a file, Shutdown.scala, in package com.packt.chapter1.
  2. Add the following imports to the top of file:
        import akka.actor.{PoisonPill, Props, ActorSystem, Actor} 
  1. Create a case object, Stop, as the message:
        case object Stop 
  1. Define an actor, ShutdownActor, as follows:
        class ShutdownActor extends Actor { 
          override def receive: Receive = { 
            case msg:String => println(s"$msg") 
            case Stop => context.stop(self) 
          } 
        } 
  1. There are two ways we can stop the actor:
  • Using PoisonPill
  • Using context.self(actorRef)

Create an actor and send it a message as shown in the following code:

        object ShutdownApp extends App{ 
          val actorSystem = ActorSystem("HelloAkka") 
          val shutdownActor1 = 
           actorSystem.actorOf(Props[ShutdownActor], 
           "shutdownActor1") 
          shutdownActor1 ! "hello" 
          shutdownActor1 ! PoisonPill 
          shutdownActor1 ! "Are you there?" 
          val shutdownActor2 = 
           actorSystem.actorOf(Props[ShutdownActor], 
           "shutdownActor2") 
          shutdownActor2 ! "hello" 
          shutdownActor2 ! Stop 
          shutdownActor2 ! "Are you there?" 
        } 
  1. Run the preceding application, and you will get the following output:
      hello
      hello
      [INFO] [05/22/2016 20:39:53.137] [HelloAkka-akka.actor.default-
      dispatcher-4] [akka://HelloAkka/user/shutdownActor1] Message
      [java.lang.String] from Actor[akka://HelloAkka/deadLetters] to
      Actor[akka://HelloAkka/user/shutdownActor1#417818231] was not
      delivered. [1] dead letters encountered.
      [INFO] [05/22/2016 20:39:53.138] [HelloAkka-
      akka.actor.default-dispatcher-4]
      [akka://HelloAkka/user/shutdownActor2] Message
      [java.lang.String] from Actor[akka://HelloAkka/deadLetters]
      to Actor[akka://HelloAkka/user/shutdownActor2#788021817] was
      not delivered. [2] dead letters encountered.

How it works...

In step three, we create a Stop message. Upon receiving this message, the actor will stop using context.stop(self).

In step four, we define an actor which handles the Stop message.

In step five, we create two actors of the same class, shutdownActor1 and shutdownActor2. We shut down shutdownActor1 using PoisonPill and shutdownActor2 using context.stop(self).

PoisonPill and context.stop(self) are the two ways to kill an actor. PoisonPill is the inbuilt message that is handled after all the messages that were already queued in the mailbox.

Context.stop is basically used for an ordered shutdown of actors when you want the child actors to stop first, then the parent actor, followed by the ActorSystem to stop top-level actors.