Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagec#
themeEmacs
linenumberstrue
    public class BasicActor : UntypedActor
    {
        private ILoggingAdapter log = Context.GetLogger();

        protected override void PreStart()
        {

        }

        protected override void PreRestart(Exception reason, object message)
        {

        }

        protected override void OnReceive(object message)
        {
            //handle messages here
            log.Info("BasicActor:GetSomeMessage " + message.ToString());
            Sender.Tell("OK");
        }

        protected override void PostStop()
        {

        }

        protected override void PostRestart(Exception reason)
        {

        }
    }

 메시지 수신처리외에 다양한 이벤트에 대한 처리기를 등록가능하며Actor전략에 따라 활용할수 있습니다처리기핸들링 가능합니다.


Monitoring

 어떠한 액터에게, 특정액트를 감시하게 하거나 kill을 할수 있는 권한을 준다고 했을시

...

Code Block
languagec#
themeEmacs
title심플한 감시자 액터
linenumberstrue
    public class WatchActor : UntypedActor
    {
        private ILoggingAdapter log = Context.GetLogger();
        private IActorRef child;
        private IActorRef lastSender = Context.System.DeadLetters;

        public WatchActor(IActorRef targetActor)
        {
            child = targetActor;
            Context.Watch(targetActor); // <-- this is the only call needed for registration
        }

        protected override void OnReceive(object message)
        {
            log.Info("WatchActor:GetSomeMessage " + message.ToString());

            if (message is string)
            {
                switch(message as string)
                {
                    case "kill":
                        Context.Stop(child);
                        lastSender = Sender;
                     break;
                }
            }

            if(message is Terminated)
            {
                if( ((Terminated)message).ActorRef.Equals(child))
                {
                    log.Info("감시대상이 사라짐");
                }
            }
        }

        public static Props Props(IActorRef stoking)
        {
            return Akka.Actor.Props.Create(() => new WatchActor(stoking));
        }

    }
Code Block
languagec#
themeEmacs
title테스트
linenumberstrue
            IActorRef myActor = actorSystem.ActorOf<BasicActor>("myactor");
            Props watchProps = WatchActor.Props(myActor);
            IActorRef watcher = actorSystem.ActorOf(watchProps,"watcher");

            var result = myActor.Ask("나는 살아있다.").Result;
            actorSystem.Stop(myActor);

...