ITPub博客

首页 > 应用开发 > Java > MasterActor详解

MasterActor详解

原创 Java 作者:541732025 时间:2016-04-28 10:58:47 0 删除 编辑
MasterActor作用:
1由ActorContext实例化MapActor、ReduceActor、AggregateActor
2,接收消息,以及消息转发给各个Actor

点击(此处)折叠或打开

  1. /**
  2.  * 管理各个Actor,以及作为消息的入口、进行消息的转发
  3.  *
  4.  */
  5. public class MasterActor extends UntypedActor {
  6.     //由ActorContext创建Actor
  7.     private ActorRef aggregateActor = getContext().actorOf(new Props(AggregateActor.class),"aggregate");
  8.     private ActorRef reduceActor = getContext().actorOf(new Props(new UntypedActorFactory(){public Actor create() {return new ReduceActor(aggregateActor);}}),"reduce");
  9.     private ActorRef mapActor = getContext().actorOf(new Props(new UntypedActorFactory(){public Actor create() {return new MapActor(reduceActor);}}),"map");

  10.     @Override
  11.     public void onReceive(Object message) throws Exception {
  12.         if(message instanceof String){
  13.             mapActor.tell(message);//fire-and-forget.非阻塞
  14.         }else if(message instanceof Result){
  15.             aggregateActor.tell(message);//让aggregateActor返回最终结果
  16.         }else{
  17.             unhandled(message);
  18.         }
  19.     }
  20. }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28912557/viewspace-2089928/,如需转载,请注明出处,否则将追究法律责任。

上一篇: Akka之WordCount
请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    479154