-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
99 lines (86 loc) · 2.82 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using Akka.Actor;
using Akka.Event;
using Akka.Logger.Serilog;
using Akka.Routing;
using Serilog;
namespace AkkaActorSystem
{
class Program
{
static void Main(string[] args)
{
var logger = new LoggerConfiguration()
.ReadFrom.AppSettings()
.Enrich.WithProperty("Version", Assembly.GetEntryAssembly().GetName().Version)
.CreateLogger();
Serilog.Log.Logger = logger;
var config = new StreamReader("akkaconfig.json").ReadToEnd();
var system = ActorSystem.Create("ActorSystem", config);
var coordinator = system.ActorOf<CoordinatorActor>("Coordinator");
coordinator.Tell(1);
system.AwaitTermination();
}
}
public class CoordinatorActor : ReceiveActor
{
private readonly ILoggingAdapter _log = Context.GetLogger(new SerilogLogMessageFormatter());
private readonly IActorRef _workerActor;
public CoordinatorActor()
{
var props =
Props.Create<WorkerActor>()
.WithRouter(FromConfig.Instance)
.WithSupervisorStrategy(new OneForOneStrategy(ex => Directive.Restart));
_workerActor = Context.ActorOf(props, "Worker");
Receive<int>(info => HandleRequest(info));
}
protected override void PreStart()
{
_log.Debug("Prestart");
}
private void HandleRequest(int info)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
_log.Info("*************************************");
_log.Info($"Coordinator Recieved {info}");
_workerActor.Tell(info);
}
}
public class WorkerActor : ReceiveActor
{
private readonly ILoggingAdapter _log = Context.GetLogger(new SerilogLogMessageFormatter());
public WorkerActor()
{
Receive<int>(info => Handle(info));
}
private void Handle(int info)
{
try
{
_log.Info($"{Context.Self.Path} Recieved {info}");
if (info % 2 == 0)
{
_log.Info("even");
Thread.Sleep(TimeSpan.FromSeconds(3));
_log.Info($"{Context.Self.Path} done");
}
else
{
_log.Info("odd");
Thread.Sleep(TimeSpan.FromSeconds(1));
throw new Exception("Invalid data");
}
_log.Info("*************************************");
}
catch (Exception ex)
{
_log.Error(ex.Message);
throw;
}
}
}
}