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

程序演示了父子结构的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...
时间: 2024-11-06 23:16:26

akka入门-调用子Actor处理消息的相关文章

Akka(1):Actor - 靠消息驱动的运算器

Akka是由各种角色和功能的Actor组成的,工作的主要原理是把一项大的计算任务分割成小环节,再按各环节的要求构建相应功能的Actor,然后把各环节的运算托付给相应的Actor去独立完成.Akka是个工具库(Tools-Library),不是一个软件架构(Software-Framework),我们不需要按照Akka的框架格式去编写程序,而是直接按需要构建Actor去异步运算一项完整的功能,这样让用户在不知不觉中自然的实现了多线程并发软件编程(concurrent programming).按这

akka入门-远程调用

akka远程调用有两种形式: 一种是查找远程Actors,一种是创建远程Actors. 公用的类: import java.io.Serializable; public class Op { public interface MathOp extends Serializable { } public interface MathResult extends Serializable { } static class Add implements MathOp { private static

Akka 编程: 什么是Actor

上一篇我们简单介绍了Actor系统,说明了Actor之间存在着层次关系,它也是构成Actor应用的最基本的单位.本篇介绍Actor本身的一些基本概念.一个Actor包含了State(状态),Behavior(行为),一个Mailbox(邮箱)和Supervisor Strategy (管理员策略),所有这些都封装在一个Actor引用之中(Actor Reference).Actor 引用一个Actor对象需要和外界隔离开来才能构成一个Actor模型,因此从外部看,一个Actor对象由一个Acto

Akka源码分析-Actor创建

上一篇博客我们介绍了ActorSystem的创建过程,下面我们就研究一下actor的创建过程. val system = ActorSystem("firstActorSystem",ConfigFactory.load()) val helloActor= system.actorOf(Props(new HelloActor),"HelloActor") helloActor ! "Hello" 普通情况下,我们一般使用ActorSystem

Android:主线程如何向子线程发送消息

今天讲一下,在Android中主线程如何向子线程中发送消息的问题. 或许回想无非就是创建一个Handler对象,然后一个线程发消息,另一个接收消息嘛-- 原理确实是这样,但是我们平时,是从子线程向主线程发消息,而主线程默认已经帮我们完成了Looper的操作,所以我们只需要简单的"创建一个Handler对象,然后一个线程发消息,另一个接收消息"-- 我们先说一下这个Looper是神马吧. 它就像一个消息队列(MessageQueue)的管家(Looper),一个消息队列只有一个管家,并且

akka入门-简介

为什么使用akka akka是Actor模型的实现.Actors为我们提供了以下优点: 1)对并发/并行程序的简单的.高级别的抽象. 2)异步.非阻塞.高性能的事件驱动编程模型. 3)非常轻量的事件驱动处理. akka提供了容错性 使用"let-it-crash"语义和监管者树形结构来实现容错.非常适合编写永不停机.自愈合的高容错系统.监管者树形结构可以跨多个JVM来提供真正的高容错系统. 位置透明性 Akka的所有元素都为分布式环境而设计:所有actor都仅通过发送消息进行互操作,所

akka入门-简单示例

以下程序演示了akka的一个简单的示例.创建Actor去处理一条命令,通过消息传递的方式进行交互. 我使用的akka版本和相关jar包参见pom文件: <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/

Android笔记(三十二) Android中线程之间的通信(四)主线程给子线程发送消息

之前的例子都是我们在子线程(WorkerThread)当中处理并发送消息,然后在主线程(UI线程)中获取消息并修改UI,那么可以不可以在由主线程发送消息,子线程接收呢?我们按照之前的思路写一下代码: package cn.lixyz.handlertest; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import andr

Akka源码分析-Actor&amp;ActorContext&amp;ActorRef&amp;ActorCell

分析源码的过程中我们发现,Akka出现了Actor.ActorRef.ActorCell.ActorContext等几个相似的概念,它们之间究竟有什么区别和联系呢? /** * Actor base trait that should be extended by or mixed to create an Actor with the semantics of the 'Actor Model': * <a href="http://en.wikipedia.org/wiki/Actor