Use Props factory methods in getting started tutorial (#25713)

* Use Props factory methods in getting started tutorial

* deprecate Props.create without actorClass parameter, #25718
This commit is contained in:
Patrik Nordwall 2018-10-15 18:12:41 +02:00 committed by Christopher Batey
parent 4b012cc306
commit b89a7e5df5
17 changed files with 77 additions and 34 deletions

View file

@ -201,7 +201,7 @@ public class ActorCreationTest extends JUnitSuite {
Props.create(new G());
assert false;
} catch (IllegalArgumentException e) {
assertEquals("erased Creator types are unsupported, use Props.create(actorClass, creator) instead", e.getMessage());
assertEquals("erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead", e.getMessage());
}
Props.create(AbstractActor.class, new G());
}
@ -305,7 +305,7 @@ public class ActorCreationTest extends JUnitSuite {
TestActor.propsUsingLamdaWithoutClass(17);
org.junit.Assert.fail("Should have detected lambda erasure, and thrown");
} catch (IllegalArgumentException e) {
assertEquals("erased Creator types are unsupported, use Props.create(actorClass, creator) instead",
assertEquals("erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead",
e.getMessage());
}
}

View file

@ -60,14 +60,6 @@ public class JavaAPI extends JUnitSuite {
assertNotNull(ref);
}
public static Props mkProps() {
return Props.create(new Creator<Actor>() {
public Actor create() {
return new JavaAPITestActor();
}
});
}
@SuppressWarnings("unchecked")
public static Props mkErasedProps() {
return Props.create(JavaAPITestActor.class, new Creator() {
@ -77,9 +69,13 @@ public class JavaAPI extends JUnitSuite {
});
}
public static Props mkPropsWithLambda() {
return Props.create(JavaAPITestActor.class, JavaAPITestActor::new);
}
@Test
public void mustBeAbleToCreateActorRefFromFactory() {
ActorRef ref = system.actorOf(mkProps());
ActorRef ref = system.actorOf(mkPropsWithLambda());
assertNotNull(ref);
}

View file

@ -38,6 +38,7 @@ private[akka] trait AbstractProps {
*
* Use the Props.create(actorClass, creator) instead.
*/
@deprecated("Use Props.create(actorClass, creator) instead, since this can't be used with Java 8 lambda.", "2.5.18")
def create[T <: Actor](creator: Creator[T]): Props = {
val cc = creator.getClass
checkCreatorClosingOver(cc)
@ -53,7 +54,7 @@ private[akka] trait AbstractProps {
case x throw new IllegalArgumentException(s"unsupported type found in Creator argument [$x]")
}
case c: Class[_] if (c == coc)
throw new IllegalArgumentException(s"erased Creator types are unsupported, use Props.create(actorClass, creator) instead")
throw new IllegalArgumentException("erased Creator types (e.g. lambdas) are unsupported, use Props.create(actorClass, creator) instead")
}
create(classOf[CreatorConsumer], actorClass, creator)
}

View file

@ -72,7 +72,7 @@ class MyEventsByTagJavaPublisher extends AbstractActorPublisher<EventEnvelope> {
public static Props props(Connection conn, String tag, Long offset,
Duration refreshInterval) {
return Props.create(() ->
return Props.create(MyEventsByTagJavaPublisher.class, () ->
new MyEventsByTagJavaPublisher(conn, tag, offset, refreshInterval));
}

View file

@ -195,7 +195,7 @@ public class ParentChildTest extends AbstractJavaTest {
// didn't put final on these in order to make the parent fit in one line in the html docs
//#test-fabricated-parent
TestProbe proxy = new TestProbe(system);
ActorRef parent = system.actorOf(Props.create(new FabricatedParentCreator(proxy)));
ActorRef parent = system.actorOf(Props.create(Actor.class, new FabricatedParentCreator(proxy)));
proxy.send(parent, "ping");
proxy.expectMsg("pong");

View file

@ -21,6 +21,10 @@ import akka.actor.ActorSystem;
import akka.actor.Props;
class PrintMyActorRefActor extends AbstractActor {
static Props props() {
return Props.create(PrintMyActorRefActor.class, PrintMyActorRefActor::new);
}
@Override
public Receive createReceive() {
return receiveBuilder()
@ -35,10 +39,14 @@ class PrintMyActorRefActor extends AbstractActor {
//#start-stop
class StartStopActor1 extends AbstractActor {
static Props props() {
return Props.create(StartStopActor1.class, StartStopActor1::new);
}
@Override
public void preStart() {
System.out.println("first started");
getContext().actorOf(Props.create(StartStopActor2.class), "second");
getContext().actorOf(StartStopActor2.props(), "second");
}
@Override
@ -57,6 +65,11 @@ class StartStopActor1 extends AbstractActor {
}
class StartStopActor2 extends AbstractActor {
static Props props() {
return Props.create(StartStopActor2.class, StartStopActor2::new);
}
@Override
public void preStart() {
System.out.println("second started");
@ -79,7 +92,11 @@ class StartStopActor2 extends AbstractActor {
//#supervise
class SupervisingActor extends AbstractActor {
ActorRef child = getContext().actorOf(Props.create(SupervisedActor.class), "supervised-actor");
static Props props() {
return Props.create(SupervisingActor.class, SupervisingActor::new);
}
ActorRef child = getContext().actorOf(SupervisedActor.props(), "supervised-actor");
@Override
public Receive createReceive() {
@ -92,6 +109,10 @@ class SupervisingActor extends AbstractActor {
}
class SupervisedActor extends AbstractActor {
static Props props() {
return Props.create(SupervisedActor.class, SupervisedActor::new);
}
@Override
public void preStart() {
System.out.println("supervised actor started");
@ -119,7 +140,7 @@ public class ActorHierarchyExperiments {
public static void main(String[] args) throws java.io.IOException {
ActorSystem system = ActorSystem.create("testSystem");
ActorRef firstRef = system.actorOf(Props.create(PrintMyActorRefActor.class), "first-actor");
ActorRef firstRef = system.actorOf(PrintMyActorRefActor.props(), "first-actor");
System.out.println("First: " + firstRef);
firstRef.tell("printit", ActorRef.noSender());
@ -151,7 +172,7 @@ class ActorHierarchyExperimentsTest extends JUnitSuite {
@Test
public void testStartAndStopActors() {
//#start-stop-main
ActorRef first = system.actorOf(Props.create(StartStopActor1.class), "first");
ActorRef first = system.actorOf(StartStopActor1.props(), "first");
first.tell("stop", ActorRef.noSender());
//#start-stop-main
}
@ -159,7 +180,7 @@ class ActorHierarchyExperimentsTest extends JUnitSuite {
@Test
public void testSuperviseActors() {
//#supervise-main
ActorRef supervisingActor = system.actorOf(Props.create(SupervisingActor.class), "supervising-actor");
ActorRef supervisingActor = system.actorOf(SupervisingActor.props(), "supervising-actor");
supervisingActor.tell("failChild", ActorRef.noSender());
//#supervise-main
}

View file

@ -15,7 +15,7 @@ public class IotSupervisor extends AbstractActor {
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
public static Props props() {
return Props.create(IotSupervisor.class);
return Props.create(IotSupervisor.class, IotSupervisor::new);
}
@Override

View file

@ -27,7 +27,7 @@ public class Device extends AbstractActor {
}
public static Props props(String groupId, String deviceId) {
return Props.create(Device.class, groupId, deviceId);
return Props.create(Device.class, () -> new Device(groupId, deviceId));
}
public static final class RecordTemperature {

View file

@ -26,7 +26,7 @@ class Device extends AbstractActor {
}
public static Props props(String groupId, String deviceId) {
return Props.create(Device.class, groupId, deviceId);
return Props.create(Device.class, () -> new Device(groupId, deviceId));
}
//#read-protocol-2

View file

@ -29,7 +29,7 @@ public class Device extends AbstractActor {
}
public static Props props(String groupId, String deviceId) {
return Props.create(Device.class, groupId, deviceId);
return Props.create(Device.class, () -> new Device(groupId, deviceId));
}
public static final class RecordTemperature {

View file

@ -31,7 +31,7 @@ public class DeviceGroup extends AbstractActor {
}
public static Props props(String groupId) {
return Props.create(DeviceGroup.class, groupId);
return Props.create(DeviceGroup.class, () -> new DeviceGroup(groupId));
}
//#device-group-register
//#device-group-remove

View file

@ -19,7 +19,7 @@ public class DeviceManager extends AbstractActor {
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
public static Props props() {
return Props.create(DeviceManager.class);
return Props.create(DeviceManager.class, DeviceManager::new);
}
//#device-manager-msgs

View file

@ -27,7 +27,7 @@ public class Device extends AbstractActor {
}
public static Props props(String groupId, String deviceId) {
return Props.create(Device.class, groupId, deviceId);
return Props.create(Device.class, () -> new Device(groupId, deviceId));
}
public static final class RecordTemperature {

View file

@ -28,7 +28,7 @@ public class DeviceGroup extends AbstractActor {
}
public static Props props(String groupId) {
return Props.create(DeviceGroup.class, groupId);
return Props.create(DeviceGroup.class, () -> new DeviceGroup(groupId));
}
public static final class RequestDeviceList {

View file

@ -45,7 +45,7 @@ public class DeviceGroupQuery extends AbstractActor {
}
public static Props props(Map<ActorRef, String> actorToDeviceId, long requestId, ActorRef requester, FiniteDuration timeout) {
return Props.create(DeviceGroupQuery.class, actorToDeviceId, requestId, requester, timeout);
return Props.create(DeviceGroupQuery.class, () -> new DeviceGroupQuery(actorToDeviceId, requestId, requester, timeout));
}
@Override

View file

@ -18,7 +18,7 @@ public class DeviceManager extends AbstractActor {
private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);
public static Props props() {
return Props.create(DeviceManager.class);
return Props.create(DeviceManager.class, DeviceManager::new);
}
public static final class RequestTrackDevice {

View file

@ -11,6 +11,11 @@ package com.lightbend.akka.sample
import akka.actor.{ Actor, Props, ActorSystem }
import scala.io.StdIn
object PrintMyActorRefActor {
def props: Props =
Props(new PrintMyActorRefActor)
}
class PrintMyActorRefActor extends Actor {
override def receive: Receive = {
case "printit"
@ -22,11 +27,16 @@ class PrintMyActorRefActor extends Actor {
import akka.testkit.AkkaSpec
object StartStopActor1 {
def props: Props =
Props(new StartStopActor1)
}
//#start-stop
class StartStopActor1 extends Actor {
override def preStart(): Unit = {
println("first started")
context.actorOf(Props[StartStopActor2], "second")
context.actorOf(StartStopActor2.props, "second")
}
override def postStop(): Unit = println("first stopped")
@ -35,6 +45,11 @@ class StartStopActor1 extends Actor {
}
}
object StartStopActor2 {
def props: Props =
Props(new StartStopActor2)
}
class StartStopActor2 extends Actor {
override def preStart(): Unit = println("second started")
override def postStop(): Unit = println("second stopped")
@ -45,15 +60,25 @@ class StartStopActor2 extends Actor {
}
//#start-stop
object SupervisingActor {
def props: Props =
Props(new SupervisingActor)
}
//#supervise
class SupervisingActor extends Actor {
val child = context.actorOf(Props[SupervisedActor], "supervised-actor")
val child = context.actorOf(SupervisedActor.props, "supervised-actor")
override def receive: Receive = {
case "failChild" child ! "fail"
}
}
object SupervisedActor {
def props: Props =
Props(new SupervisedActor)
}
class SupervisedActor extends Actor {
override def preStart(): Unit = println("supervised actor started")
override def postStop(): Unit = println("supervised actor stopped")
@ -74,7 +99,7 @@ class ActorHierarchyExperiments extends AkkaSpec {
object ActorHierarchyExperiments extends App {
val system = ActorSystem("testSystem")
val firstRef = system.actorOf(Props[PrintMyActorRefActor], "first-actor")
val firstRef = system.actorOf(PrintMyActorRefActor.props, "first-actor")
println(s"First: $firstRef")
firstRef ! "printit"
@ -90,7 +115,7 @@ object ActorHierarchyExperiments extends App {
// format: OFF
//#start-stop-main
val first = system.actorOf(Props[StartStopActor1], "first")
val first = system.actorOf(StartStopActor1.props, "first")
first ! "stop"
//#start-stop-main
// format: ON
@ -100,7 +125,7 @@ first ! "stop"
// format: OFF
//#supervise-main
val supervisingActor = system.actorOf(Props[SupervisingActor], "supervising-actor")
val supervisingActor = system.actorOf(SupervisingActor.props, "supervising-actor")
supervisingActor ! "failChild"
//#supervise-main
// format: ON