码迷,mamicode.com
首页 > 其他好文 > 详细

akka入门-调用子Actor处理消息

时间:2015-05-17 21:56:16      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

程序演示了父子结构的Actor处理消息。父Actor接收到消息后调用子Actor处理。

1.创建父子Actor

import java.util.UUID;

import com.center.akka.simple.command.Command;
import com.center.akka.simple.event.Event;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class ParentActor extends UntypedActor {

     LoggingAdapter log = Logging.getLogger(getContext().system(), this);

     private final ActorRef childActor ;

     public ParentActor() {
           childActor = getContext().actorOf(Props.create(ChildActor. class), "child-actor");
     }

     @Override
     public void onReceive(Object msg ) throws Exception {

           log.info( "Received Command: " + msg );

           if (msg instanceof Command) {
               final String data = ((Command) msg).getData();
               final Event event = new Event(data, UUID.randomUUID().toString());

               childActor.tell(event , getSelf());
          } else if (msg .equals("echo" )) {
               log.info( "ECHO!");
          }
     }
}

import akka.actor.UntypedActor;
import akka.event.Logging;
import akka.event.LoggingAdapter;

public class ChildActor extends UntypedActor {

    LoggingAdapter log = Logging.getLogger(getContext().system(), this);

    @Override
    public void preStart() {
        log.info( "Starting");
    }

    @Override
    public void onReceive(Object msg ) {
        log.info( "Received Event: " + msg );
    }


}

2.测试类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.center.akka.parent_child.actor.ParentActor;
import com.center.akka.simple.command.Command;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;

public class System {

     public static final Logger log = LoggerFactory.getLogger(System.class);

     public static void main(String... args) throws Exception {

           final ActorSystem actorSystem = ActorSystem.create("actor-system");

          Thread. sleep(5000);

           final ActorRef actorRef = actorSystem.actorOf(Props.create(ParentActor. class), "parent-actor");

           actorRef.tell( new Command("CMD 1" ), null);
           actorRef.tell( new Command("CMD 2" ), null);
           actorRef.tell( new Command("CMD 3" ), null);
           actorRef.tell( "echo", null );
           actorRef.tell( new Command("CMD 4" ), null);
           actorRef.tell( new Command("CMD 5" ), null);

          Thread. sleep(5000);

           log.debug("Actor System Shutdown Starting..." );

           actorSystem.shutdown();
     }
}

3.结果输出

[INFO] [05/17/2015 17:52:05.981] [actor-system-akka.actor.default-dispatcher-4] [akka://actor-system/user/parent-actor/child-actor] Starting
[INFO] [05/17/2015 17:52:05.981] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: Command{data='CMD 1'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: Command{data='CMD 2'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-2] [akka://actor-system/user/parent-actor/child-actor] Received Event: Event{data='CMD 1', uuid='3423af06-f96c-430b-ad3a-6d16d86b3d91'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: Command{data='CMD 3'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-2] [akka://actor-system/user/parent-actor/child-actor] Received Event: Event{data='CMD 2', uuid='93061123-91ef-445f-8f99-29b558d84947'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: echo
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-2] [akka://actor-system/user/parent-actor/child-actor] Received Event: Event{data='CMD 3', uuid='35203fc0-f1cc-4d8c-bab2-75e77f515c03'}
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] ECHO!
[INFO] [05/17/2015 17:52:06.035] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: Command{data='CMD 4'}
[INFO] [05/17/2015 17:52:06.036] [actor-system-akka.actor.default-dispatcher-3] [akka://actor-system/user/parent-actor] Received Command: Command{data='CMD 5'}
[INFO] [05/17/2015 17:52:06.036] [actor-system-akka.actor.default-dispatcher-2] [akka://actor-system/user/parent-actor/child-actor] Received Event: Event{data='CMD 4', uuid='5df543a6-7290-43f6-ad85-74ea341e999b'}
[INFO] [05/17/2015 17:52:06.036] [actor-system-akka.actor.default-dispatcher-2] [akka://actor-system/user/parent-actor/child-actor] Received Event: Event{data='CMD 5', uuid='da49b323-59ba-49c5-96d5-707cbe5a4b9b'}
17:52:10.981 [main] DEBUG c.c.akka.parent_child.app.System - Actor System Shutdown Starting...


akka入门-调用子Actor处理消息

标签:

原文地址:http://blog.csdn.net/liuchangqing123/article/details/45796717

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!