Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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 " protected override void PostStop()+ message.ToString()); { Sender.Tell("OK"); } protected override void PostStop() { } protected override void PostRestart(Exception reason) { } } |
메시지 수신처리외에 다양한 이벤트에 대한 처리기를 등록가능하며
Actor전략에 따라 활용할수 있습니다.
Monitoring
어떠한 액터에게, 특정액트를 감시하게 하거나 kill을 할수 있는 권한을 준다고 했을시
Context.Watch에 감시대상 액트를 등록함으로 , 대상이 사라질지 감지가 가능합니다.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
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 | ||||||
---|---|---|---|---|---|---|
| ||||||
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); |
Expand | ||
---|---|---|
| ||
[INFO][2017-09-06 오전 4:53:45][Thread 0006][[akka://ServiceA/user/myactor#14600 [INFO][2017-09-06 오전 4:53:45][Thread 0004][[akka://ServiceA/user/watcher#11499 [INFO][2017-09-06 오전 4:53:45][Thread 0004][[akka://ServiceA/user/watcher#11499 |
액트접근 주소체계
...
잠시 실습없이, Actor를 접근하는 주소체계에대해 설명 드리겠습니다. ActorPath는 총 4가지로 구분이 되며
...