The actor model provides the core functionality of reactive systems, defined in the Reactive Manifesto as responsive, resilient, elastic, and message driven.
1 | static class Counter extends AbstractLoggingActor { |
send message
1 | counter.tell(new Counter.Message(), ActorRef.noSender()); |
change actor behaviour
1 | private void onEnable(Enable enable) { |
1 | ActorSystem system = ActorSystem.create(); |
Actors may create other actors. When one actor creates another actor, the creator is known as the supervisor and the created actor is known as the worker.
但是这种方式不被推荐?
If one actor does not have the means for dealing with a certain situation, it sends a corresponding failure message to its supervisor, asking for help. The supervisor has four different options for reacting to a failure:
- Resume the child, keeping its accumulated internal state but ignoring the message that lead to the failure.
- Restart the child, clearing out its accumulated internal state by starting a new instance.
- Stop the child permanently and send all future messages for the child to the Dead-Letter Office.
- Escalate the failure, thereby failing the supervisor itself
针对actor的策略?
1 | public class Supervisor extends AbstractLoggingActor { |
supervisor是child?
Akka provides two classes of supervision strategies: OneForOneStrategy
and AllForOneStrategy.
The difference between them is that the former applies the obtained directive only to the failed child, whereas the latter applies it to all siblings as well. Normally, you should use the OneForOneStrategy
, which is the default if none is explicitly specified. It is defined by overriding the SupervisorStrategy
method.
The output shows, that after four messages the exception gets escalated to the Supervisor
and the remaining messages are sent to the deadLetters
box. If the SupervisorStrategy
would have been defined to restart()
instead of stop()
, a new instance of the NonTrustWorthyChild
actor would have been started.
关于子actor的策略,是restart还是stop