■ IActor 인터페이스의 ReceiveAsync 메소드를 사용해 액터 수명주기 이벤트를 처리하는 방법을 보여준다.
▶ HelloMessage.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using TestProject; /// <summary> /// 헬로우 메시지 /// </summary> public class HelloMessage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 이름 /// </summary> public string Name; #endregion } |
▶ HelloActor.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 |
using Proto; namespace TestProject; /// <summary> /// 헬로우 액터 /// </summary> public class HelloActor : IActor { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 수신하기 (비동기) - ReceiveAsync(context) /// <summary> /// 수신하기 (비동기) /// </summary> /// <param name="context">컨텍스트</param> /// <returns>태스크</returns> public Task ReceiveAsync(IContext context) { switch(context.Message) { case HelloMessage helloMessage : Console.WriteLine($"안녕하세요, {helloMessage.Name}"); break; case Started _ : Console.WriteLine("시작했습니다, 여기서 액터를 초기화합니다."); break; case Stopping _ : Console.WriteLine("중단중입니다, 액터가 곧 셧 다운됩니다."); break; case Stopped _ : Console.WriteLine("중단했습니다, 액터와 그 자식들이 중단되었습니다."); break; case Restarting _ : Console.WriteLine("재시작하고 있습니다, 액터가 곧 재시작됩니다."); break; } return Task.CompletedTask; } #endregion } |
▶ 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 |
using Proto; using Proto.Extensions; namespace TestProject; /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 시작하기 - Main() /// <summary> /// 시작하기 /// </summary> /// <returns>태스크</returns> private static async Task Main() { ActorSystem actorSystem = new ActorSystem(); actorSystem.EventStream.Subscribe<DeadLetterEvent> ( deadLetter => Console.WriteLine ( $"DeadLetter from {deadLetter.Sender} to {deadLetter.Pid} : {deadLetter.Message.GetMessageTypeName()} = '{deadLetter.Message}'" ) ); RootContext rootContext = new RootContext(actorSystem); Props helloProps = Props.FromProducer(() => new HelloActor()); PID helloActorPID = rootContext.Spawn(helloProps); rootContext.Send(helloActorPID, new HelloMessage { Name = "홍길동" }); await actorSystem.Root.PoisonAsync(helloActorPID); } #endregion } |