Fault Tolerant of akka

Source: Internet
Author: User

What should I do if I want to fault tolerance?

The parent actor first needs to know the failure status of the sub-actor, and then determine what to do. What to do is called supervisorstrategy ".

 

// Restart the storage child when storageexception is thrown.
// After 3 restarts within 5 seconds it will be stopped.
Override Val supervisorstrategy = oneforonestrategy (maxnrofretries = 3,
Withintimerange = 5 seconds ){
Case _: storage. storageexception => restart
} What does supervisorstrategy do?An akka supervisor strategy is the policy to apply for crashing children. What supervisorstrategy functions can be used?1. allforonestrategy: when a child fails, apply all Fault Handling "directive" to all children. 2. oneforonestrategy: when a child fails, apply Fault Handling "directive" to the child that fails. What is "directive?First, what should I do after the child failure? What should I do? akka provides several implementations, represented in the subclass of Directive: 1. resume continues to let the failed actor process the message. restart discards the old actor, creates a new actor, and resumes message processing. stop stop sub-actor4. escalate to raise the failure to the parent actor of the parent actor. By re-running the failed cause, the parent actor fails due to the same exception of the sub-actor. From this perspective, The so-called failure is an exception.Is throwable. How should I pass direve ve to strategy?Oneforonestrategy and allforonestrategy are colized functions, and the second parameter block is a decider. It is a partialfunction that maps exceptions to Directive. Type decider = partialfunction [throwable, Directive] In the xxxstrategy parameter, the parameters in the first parameter block can be omitted, so that the number of retries is not limited, or, you can only limit the total number of times without limit the frequency. What is the default behavior when no supervisor strategy is specified?• Actorinitializationexception will stop the failing Child Actor
• Actorkilledexception will stop the failing Child Actor
• Exception will restart the failing Child Actor
• Other Types of throwable will be escalated to parent actor Strategy behaviors can be customized: ExampleImport akka. Actor. oneforonestrategy
Import akka. Actor. supervisorstrategy ._
Import scala. Concurrent. Duration ._
Override Val supervisorstrategy =
Oneforonestrategy (maxnrofretries = 10, withintimerange = 1 minute ){
Case _: arithmeticexception => resume
Case T =>
Super. supervisorstrategy. Decider. applyorelse (T, (_: Any) => escalate)
} Supervision of top-level actorsAll top-level actors are sub-actors of user guardian. Therefore, the failures are handled by guardian according to the strategy it configures.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.