Getting Started tutorial improvements (#23210)
This commit is contained in:
parent
d87cf4aec4
commit
f38b928e13
67 changed files with 1451 additions and 1507 deletions
|
|
@ -1,10 +1,8 @@
|
|||
package jdocs.tutorial_1;
|
||||
//#print-refs
|
||||
package com.lightbend.akka.sample;
|
||||
|
||||
//#print-refs
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.actor.AbstractActor.Receive;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
import akka.testkit.javadsl.TestKit;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
|
@ -12,6 +10,12 @@ import org.junit.Test;
|
|||
import org.scalatest.junit.JUnitSuite;
|
||||
|
||||
//#print-refs
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.actor.AbstractActor.Receive;
|
||||
import akka.actor.ActorRef;
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.Props;
|
||||
|
||||
class PrintMyActorRefActor extends AbstractActor {
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
|
|
@ -106,6 +110,26 @@ class SupervisedActor extends AbstractActor {
|
|||
}
|
||||
//#supervise
|
||||
|
||||
//#print-refs
|
||||
public class ActorHierarchyExperiments {
|
||||
public static void main(String[] args) throws java.io.IOException {
|
||||
ActorSystem system = ActorSystem.create("test");
|
||||
|
||||
ActorRef firstRef = system.actorOf(Props.create(PrintMyActorRefActor.class), "first-actor");
|
||||
System.out.println("First: " + firstRef);
|
||||
firstRef.tell("printit", ActorRef.noSender());
|
||||
|
||||
System.out.println(">>> Press ENTER to exit <<<");
|
||||
try {
|
||||
System.in.read();
|
||||
} finally {
|
||||
system.terminate();
|
||||
}
|
||||
}
|
||||
}
|
||||
//#print-refs
|
||||
|
||||
|
||||
class ActorHierarchyExperimentsTest extends JUnitSuite {
|
||||
static ActorSystem system;
|
||||
|
||||
|
|
@ -120,28 +144,19 @@ class ActorHierarchyExperimentsTest extends JUnitSuite {
|
|||
system = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreateTopAndChildActor() {
|
||||
//#print-refs
|
||||
ActorRef firstRef = system.actorOf(Props.create(PrintMyActorRefActor.class), "first-actor");
|
||||
System.out.println("First : " + firstRef);
|
||||
firstRef.tell("printit", ActorRef.noSender());
|
||||
//#print-refs
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartAndStopActors() {
|
||||
//#start-stop
|
||||
//#start-stop-main
|
||||
ActorRef first = system.actorOf(Props.create(StartStopActor1.class), "first");
|
||||
first.tell("stop", ActorRef.noSender());
|
||||
//#start-stop
|
||||
//#start-stop-main
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuperviseActors() {
|
||||
//#supervise
|
||||
//#supervise-main
|
||||
ActorRef supervisingActor = system.actorOf(Props.create(SupervisingActor.class), "supervising-actor");
|
||||
supervisingActor.tell("failChild", ActorRef.noSender());
|
||||
//#supervise
|
||||
//#supervise-main
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,30 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.tutorial_1;
|
||||
|
||||
//#iot-app
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import akka.actor.ActorSystem;
|
||||
import akka.actor.ActorRef;
|
||||
|
||||
public class IotMain {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ActorSystem system = ActorSystem.create("iot-system");
|
||||
|
||||
try {
|
||||
// Create top level supervisor
|
||||
ActorRef supervisor = system.actorOf(IotSupervisor.props(), "iot-supervisor");
|
||||
|
||||
System.out.println("Press ENTER to exit the system");
|
||||
System.in.read();
|
||||
} finally {
|
||||
system.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//#iot-app
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
/**
|
||||
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
||||
*/
|
||||
package jdocs.tutorial_1;
|
||||
|
||||
//#iot-supervisor
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.actor.ActorLogging;
|
||||
import akka.actor.Props;
|
||||
import akka.event.Logging;
|
||||
import akka.event.LoggingAdapter;
|
||||
|
||||
public class IotSupervisor extends AbstractActor {
|
||||
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
|
||||
|
||||
public static Props props() {
|
||||
return Props.create(IotSupervisor.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preStart() {
|
||||
log.info("IoT Application started");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postStop() {
|
||||
log.info("IoT Application stopped");
|
||||
}
|
||||
|
||||
// No need to handle any messages
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
//#iot-supervisor
|
||||
Loading…
Add table
Add a link
Reference in a new issue