Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Supervision and Monitoring in Akka

DZone's Guide to

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.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

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

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
akka ,monitoring ,scala ,java ,tutorial

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

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}