Skip to content

Commit 31522d0

Browse files
cleaned up ReceiveActor documentation (#4958)
* removed confusing and conflicting examples in the `ReceiveActor` documentation * Embedded reference to "how actors restart" YouTube video in supervision docs
1 parent 3c2f098 commit 31522d0

File tree

2 files changed

+24
-51
lines changed

2 files changed

+24
-51
lines changed

docs/articles/actors/receive-actor-api.md

Lines changed: 22 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,28 @@ system.ActorOf(DemoActor.Props(42), "demo");
6868

6969
Another good practice is to declare local messages (messages that are sent in process) within the Actor, which makes it easier to know what messages are generally being sent over the wire vs in process.:
7070
```csharp
71-
public class DemoActor : UntypedActor
71+
public class DemoActor : ReceiveActor
7272
{
73-
protected override void OnReceive(object message)
73+
public DemoActor()
7474
{
75-
switch (message)
75+
Receive<DemoActor.DemoActorLocalMessage1>(x =>
7676
{
77-
case DemoActorLocalMessages.DemoActorLocalMessage1 msg1:
78-
// Handle message here...
79-
break;
80-
case DemoActorLocalMessages.DemoActorLocalMessage2 msg2:
81-
// Handle message here...
82-
break;
83-
default:
84-
break;
85-
}
86-
}
77+
// Handle message here...
78+
});
8779

88-
class DemoActorLocalMessages
80+
Receive<DemoActor.DemoActorLocalMessage2>(x =>
8981
{
90-
public class DemoActorLocalMessage1
91-
{
92-
}
82+
// Handle message here...
83+
});
84+
}
9385

94-
public class DemoActorLocalMessage2
95-
{
96-
}
97-
}
86+
public class DemoActorLocalMessage1
87+
{
88+
}
89+
90+
public class DemoActorLocalMessage2
91+
{
92+
}
9893
}
9994
```
10095

@@ -502,32 +497,6 @@ ReceiveAny(o => Console.WriteLine("Received object: " + o);
502497
Receive<object>(0 => Console.WriteLine("Received object: " + o);
503498
```
504499

505-
### Non generic overloads
506-
Receive has non generic overloads:
507-
```csharp
508-
Receive(typeof(string), obj => Console.WriteLine(obj.ToString()) );
509-
```
510-
Predicates can go before or after the handler:
511-
512-
```csharp
513-
Receive(typeof(string), obj => ((string) obj).Length > 5, obj => Console.WriteLine(obj.ToString()) );
514-
Receive(typeof(string), obj => Console.WriteLine(obj.ToString()), obj => ((string) obj).Length > 5 );
515-
```
516-
And the non generic Func
517-
518-
```csharp
519-
Receive(typeof(string), obj =>
520-
{
521-
var s = (string)obj;
522-
if (s.Length > 5)
523-
{
524-
Console.WriteLine("1: " + s);
525-
return true;
526-
}
527-
return false;
528-
});
529-
```
530-
531500
## Reply to messages
532501
If you want to have a handle for replying to a message, you can use `Sender`, which gives you an `IActorRef`. You can reply by sending to that `IActorRef` with `Sender.Tell(replyMsg, Self)`. You can also store the `IActorRef` for replying later, or passing on to other actors. If there is no sender (a message was sent without an actor or task context) then the sender defaults to a 'dead-letter' actor ref.
533502

@@ -703,7 +672,7 @@ public class HotSwapActor : ReceiveActor
703672
});
704673
}
705674

706-
private void Angry(object message)
675+
private void Angry()
707676
{
708677
Receive<string>(s => s.Equals("foo"), msg =>
709678
{
@@ -716,7 +685,7 @@ public class HotSwapActor : ReceiveActor
716685
});
717686
}
718687

719-
private void Happy(object message)
688+
private void Happy()
720689
{
721690
Receive<string>(s => s.Equals("foo"), msg =>
722691
{
@@ -834,8 +803,10 @@ Use `Kill` like this:
834803
victim.Tell(Akka.Actor.Kill.Instance, ActorRefs.NoSender);
835804
```
836805

837-
## Actors and exceptions
838-
It can happen that while a message is being processed by an actor, that some kind of exception is thrown, e.g. a database exception.
806+
## Actors and Exceptions
807+
An exception can be thrown while a message is being processed by an actor, e.g. a database exception or some other type of runtime exception.
808+
809+
When this occurs and the exception is not handled via a `try` / `catch` block, the actor's parent will be notified that its child failed with a specific exception type and will use its [supervision strategy](xref:supervision#what-supervision-means) to restart that child.
839810

840811
### What happens to the Message
841812
If an exception is thrown while a message is being processed (i.e. taken out of its mailbox and handed over to the current behavior), then this message will be lost. It is important to understand that it is not put back on the mailbox. So if you want to retry processing of a message, you need to deal with it yourself by catching the exception and retry your flow. Make sure that you put a bound on the number of retries since you don't want a system to livelock (so consuming a lot of cpu cycles without making progress).

docs/articles/concepts/supervision.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ title: Supervision
77

88
This document outlines the concept behind supervision and what that means for your Akka.NET actors at run-time.
99

10+
<iframe width="560" height="315" src="https://www.youtube.com/embed/CC2XdYlpmvo" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
11+
1012
## What Supervision Means
1113
As described in [Actor Systems](xref:actor-systems) supervision describes a dependency relationship between actors: the supervisor delegates tasks to subordinates and therefore must respond to their failures. When a subordinate detects a failure (i.e. throws an exception), it suspends itself and all its subordinates and sends a message to its supervisor, signaling failure. Depending on the nature of the work to be supervised and the nature of the failure, the supervisor has a choice of the following four options:
1214

0 commit comments

Comments
 (0)