DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Supervision and Monitoring in Akka

Supervision and Monitoring in Akka

It's important to know your Akka actor hierarchy so you know how to react to failures. This guide covers the actor system and how you can supervise parents and children.

Prabhat Kashyap user avatar by
Prabhat Kashyap
·
May. 03, 17 · Java Zone · Tutorial
Like (8)
Save
Tweet
8.80K Views

Join the DZone community and get the full member experience.

Join For Free

Supervision describes a dependency relationship between actors: the parent and child relationship. A parent is unique because it created the child actor, so the parent is responsible for reacting when failures happen in the child.

And the parent decides which choice needs to be selected. When a parent receives the failure signal from its child, depending on the nature of the failure, the parent decides from following options:

  • Resume: Parent starts the child actor, keeping its internal state.

  • Restart: Parent starts the child actor by clearing its internal state.

  • Stop: Stop the child permanently.

  • Escalate: Escalate the failure by failing itself and propagating the failure to its parent.

akka-life-cycle

Akka Actor Lifecycle

It is always important to view all parts of the supervision hierarchy, which explains the escalate option. Each supervisor should cover all possible failure cases.

Actor System

Actor System

/user: The User Guardian Actor

An actor created using system.actorOf() are children of the user guardian actor. Whenever a user guardian terminates, all user-created actors will be terminated, too. Top-level user-created actors are determined by the user guardian actor as to how they will be supervised. Root Guardian is the supervisor of the user guardian.

/root: The Root Guardian

The root guardian actor is the father of the actor system. It supervises the user guardian actor and system guardian actor.

Supervision Strategies

There are two types of supervision strategies that we follow to supervise any actor:

  1. One-For-One

  2. One-For-All

case object ResumeException extends Exception
case object StopException extends Exception

case object RestartException extends Exception

override val supervisorStrategy =

    OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 second){

    case ResumeException => Resume

    case RestartException => Restart

    case StopException => Stop

    case _: Exception => Escalate 

}



Example of One-For-One Strategy

What is Monitoring?

Lifecycle Monitoring in Akka is usually referred to as DeathWatch

Monitoring is thus used to tie one actor to another so that it may react to the other actor’s termination, in contrast to supervision which reacts to failure.

Monitoring

Monitoring is particularly useful if a supervisor cannot simply restart its children and has to terminate them, such as in the case of errors during actor initialization. In that case, it should monitor those children and re-create them or schedule itself to retry this at a later time.

Code Repository: Supervision and Monitoring

Akka (toolkit)

Published at DZone with permission of Prabhat Kashyap, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Flask vs. Django: Which Python Framework to Choose?
  • My Sentiments, Erm… Not Exactly
  • Change Data Capture to Accelerate Real-Time Analytics
  • How to Translate Value to Executives Using an Outcome-Driven Mindset

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo