程序演示了父子结构的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