-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathProgram.cs
45 lines (35 loc) · 1.43 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Akka.Actor;
using Akka.Event;
using AkkaWordCounter;
ActorSystem myActorSystem = ActorSystem.Create("LocalSystem");
myActorSystem.Log.Info("Hello from the ActorSystem");
// Props == formula for creating an actor
Props myProps = Props.Create<HelloActor>();
// IActorRef == handle for messaging an actor
// Survives actor restarts, is serializable
IActorRef myActor = myActorSystem.ActorOf(myProps, "MyActor");
// tell my actor to display a message via Fire-and-Forget messaging
myActor.Tell("Hello, World!");
// use Ask<T> to do request-response messaging
string whatsUp = await myActor.Ask<string>("What's up?");
Console.WriteLine(whatsUp);
var counterActor = myActorSystem.ActorOf(Props.Create<CounterActor>(),
"CounterActor");
var parserActor = myActorSystem.ActorOf(Props.Create(() => new ParserActor(counterActor)),
"ParserActor");
Task<IDictionary<string, int>> completionPromise = counterActor
.Ask<IDictionary<string, int>>(@ref => new CounterQueries.FetchCounts(@ref), null,
CancellationToken.None);
parserActor.Tell(new DocumentCommands.ProcessDocument(
"""
This is a test of the Akka.NET Word Counter.
I would go
"""
));
IDictionary<string, int> counts = await completionPromise;
foreach(var kvp in counts)
{
// going to use string interpolation here because we don't care about perf
myActorSystem.Log.Info($"{kvp.Key}: {kvp.Value} instances");
}
await myActorSystem.Terminate();