You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have chosen a few well-known exception types in order to demonstrate the application of the fault handling directives described in [Supervision and Monitoring](xref:supervision). First off, it is a one-for-one strategy, meaning that each child is treated separately (an all-for-one strategy works very similarly, the only difference is that any decision is applied to all children of the supervisor, not only the failing one). There are limits set on the restart frequency, namely maximum 10 restarts per minute; each of these settings could be left out, which means that the respective limit does not apply, leaving the possibility to specify an absolute upper limit on the restarts or to make the restarts work infinitely. The child actor is stopped if the limit is exceeded.
50
-
51
-
This is the piece which maps child failure types to their corresponding directives.
40
+
We will handle a few exception types to demonstrate some fault handling directives described in [Supervision and Monitoring](xref:supervision). This strategy is "one-for-one", meaning that each child is treated separately. The alternative is an "all-for-one" strategy, where a decision is applied to _all_ children of the supervisor, not only the failing one. We have chosen to set a limit of maximum 10 restarts per minute; The child actor is stopped if the limit is exceeded. We could have chosen to leave this argument out, which would have created a strategy where the child actor would restart indefinitely.
52
41
53
42
> [!NOTE]
54
43
> If the strategy is declared inside the supervising actor (as opposed to
@@ -65,10 +54,7 @@ exceptions are handled by default:
65
54
*`ActorKilledException` will stop the failing child actor; and
66
55
* Any other type of `Exception` will restart the failing child actor.
67
56
68
-
If the exception escalate all the way up to the root guardian it will handle it
69
-
in the same way as the default strategy defined above.
70
-
71
-
You can combine your own strategy with the default strategy:
57
+
You can combine your own strategy with the default strategy like this:
varchild=ExpectMsg<IActorRef>(); // retrieve answer from TestKit’s TestActor
196
177
```
197
178
198
-
The first test shall demonstrate the `Resume` directive, so we try it out by
199
-
setting some non-initial state in the actor and have it fail:
179
+
Our first test will demonstrate `Directive.Resume`, so we set some non-initial state in the child actor and cause it to fail:
200
180
201
181
```csharp
202
182
child.Tell(42); // set state to 42
@@ -208,30 +188,27 @@ child.Tell("get");
208
188
ExpectMsg(42);
209
189
```
210
190
211
-
As you can see the value 42 survives the fault handling directive because we're using the `Resume` directive, which does not cause the actor to restart. Now, if we
212
-
change the failure to a more serious `NullReferenceException`, that will no
213
-
longer be the case:
191
+
As you can see the value 42 survives the fault handling directive because we're using the `Resume` directive, which does not cause the actor to restart.
192
+
193
+
If we change the failure to a more serious `NullReferenceException`, which we defined above to result in a `Restart` directive, that will no longer be the case:
214
194
215
195
```csharp
216
196
child.Tell(newNullReferenceException());
217
197
child.Tell("get");
218
198
ExpectMsg(0);
219
199
```
220
200
221
-
This is because the actor has restarted and the original `Child` actor instance that was processing messages will be destroyed and replaced by a brand-new instance defined using the original`Props` passed to its parent.
201
+
This is because the actor has restarted and the original `Child` actor instance that was processing messages will be destroyed and replaced by a brand-new instance defined using the same`Props`.
222
202
223
-
And finally in case of the fatal `IllegalArgumentException` the child will be
224
-
terminated by the supervisor:
203
+
And finally in case of the fatal `ArgumentException`, our strategy will return a stop directive, and the child will be terminated by the supervisor:
Up to now the supervisor was completely unaffected by the child's failure,
233
-
because the directives set did handle it. In case of an `Exception`, this is not
234
-
true anymore and the supervisor escalates the failure.
211
+
Up to now the supervisor was completely unaffected by the child's failure, because the directives in our strategy handled the exception. However, if we cause an `Exception`, none of our handlers are invoked and the supervisor escalates the failure.
235
212
236
213
```csharp
237
214
supervisor.Tell(Props.Create<Child>()); // create new child
0 commit comments