2014-02-21 12:43:30 +01:00
|
|
|
/**
|
2017-01-04 17:37:10 +01:00
|
|
|
* Copyright (C) 2009-2017 Lightbend Inc. <http://www.lightbend.com>
|
2014-02-21 12:43:30 +01:00
|
|
|
*/
|
2015-05-05 16:44:49 +02:00
|
|
|
package docs.actorlambda;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
2014-11-08 13:55:38 +01:00
|
|
|
import akka.actor.AbstractActor;
|
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
import akka.actor.ActorSystem;
|
|
|
|
|
import akka.actor.Props;
|
|
|
|
|
import akka.japi.pf.FI;
|
2014-02-21 12:43:30 +01:00
|
|
|
import akka.testkit.JavaTestKit;
|
2016-02-11 16:39:25 +01:00
|
|
|
import docs.AbstractJavaTest;
|
2014-02-21 12:43:30 +01:00
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
import org.junit.Test;
|
2014-08-25 15:49:28 +02:00
|
|
|
import scala.concurrent.Await;
|
2014-11-08 13:55:38 +01:00
|
|
|
import scala.concurrent.duration.Duration;
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
import java.util.Optional;
|
2014-02-21 12:43:30 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
public class InitializationDocTest extends AbstractJavaTest {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
static ActorSystem system = null;
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
public static void beforeClass() {
|
|
|
|
|
system = ActorSystem.create("InitializationDocTest");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
2014-08-25 15:49:28 +02:00
|
|
|
public static void afterClass() throws Exception {
|
2014-11-08 13:55:38 +01:00
|
|
|
Await.ready(system.terminate(), Duration.create("5 seconds"));
|
2014-02-21 12:43:30 +01:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
|
|
|
|
|
static public class PreStartInitExample extends AbstractActor {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return AbstractActor.emptyBehavior();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//#preStartInit
|
|
|
|
|
@Override
|
|
|
|
|
public void preStart() {
|
|
|
|
|
// Initialize children here
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Overriding postRestart to disable the call to preStart()
|
|
|
|
|
// after restarts
|
|
|
|
|
@Override
|
|
|
|
|
public void postRestart(Throwable reason) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The default implementation of preRestart() stops all the children
|
|
|
|
|
// of the actor. To opt-out from stopping the children, we
|
|
|
|
|
// have to override preRestart()
|
|
|
|
|
@Override
|
|
|
|
|
public void preRestart(Throwable reason, Optional<Object> message)
|
|
|
|
|
throws Exception {
|
|
|
|
|
// Keep the call to postStop(), but no stopping of children
|
|
|
|
|
postStop();
|
|
|
|
|
}
|
|
|
|
|
//#preStartInit
|
|
|
|
|
|
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
public static class MessageInitExample extends AbstractActor {
|
|
|
|
|
private String initializeMe = null;
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
//#messageInit
|
|
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchEquals("init", m1 -> {
|
|
|
|
|
initializeMe = "Up and running";
|
|
|
|
|
getContext().become(receiveBuilder()
|
|
|
|
|
.matchEquals("U OK?", m2 -> {
|
|
|
|
|
sender().tell(initializeMe, self());
|
|
|
|
|
})
|
|
|
|
|
.build());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
}
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
//#messageInit
|
2014-02-21 12:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-08 13:55:38 +01:00
|
|
|
public class GenericMessage<T> {
|
|
|
|
|
T value;
|
|
|
|
|
|
|
|
|
|
public GenericMessage(T value) {
|
|
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class GenericActor extends AbstractActor {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchUnchecked(GenericMessage.class, (GenericMessage<String> msg) -> {
|
|
|
|
|
GenericMessage<String> message = msg;
|
|
|
|
|
sender().tell(message.value.toUpperCase(), self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2014-11-08 13:55:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static class GenericActorWithPredicate extends AbstractActor {
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
@Override
|
|
|
|
|
public Receive createReceive() {
|
2014-11-08 13:55:38 +01:00
|
|
|
FI.TypedPredicate<GenericMessage<String>> typedPredicate = s -> !s.value.isEmpty();
|
|
|
|
|
|
improve AbstractActor, #21717
* Receive class that wraps PartialFunction, to avoid
scary scala types
* move AbstractActorContext to AbstractActor.ActorContext
* converting docs, many, many UntypedActor
* removing UntypedActor docs
* add unit test for ReceiveBuilder
* MiMa filters
* consistent use of getContext(), self(), sender()
* rename cross references
* migration guide
* skip samples for now
* improve match type safetyi, add matchUnchecked
* the `? extends P` caused code like this to compile:
`match(String.class, (Integer i) -> {})`
* added matchUnchecked, since it can still be useful (um, convenient)
to be able to do:
`matchUnchecked(List.class, (List<String> list) -> {})`
* eleminate some scala.Option
* preRestart
* findChild
* ActorIdentity.getActorRef
2016-12-13 10:59:29 +01:00
|
|
|
return receiveBuilder()
|
|
|
|
|
.matchUnchecked(GenericMessage.class, typedPredicate, (GenericMessage<String> msg) -> {
|
|
|
|
|
sender().tell(msg.value.toUpperCase(), self());
|
|
|
|
|
})
|
|
|
|
|
.build();
|
2014-11-08 13:55:38 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
@Test
|
|
|
|
|
public void testIt() {
|
|
|
|
|
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
ActorRef testactor = system.actorOf(Props.create(MessageInitExample.class), "testactor");
|
|
|
|
|
String msg = "U OK?";
|
|
|
|
|
|
|
|
|
|
testactor.tell(msg, getRef());
|
|
|
|
|
expectNoMsg(Duration.create(1, TimeUnit.SECONDS));
|
|
|
|
|
|
|
|
|
|
testactor.tell("init", getRef());
|
|
|
|
|
testactor.tell(msg, getRef());
|
|
|
|
|
expectMsgEquals("Up and running");
|
|
|
|
|
}};
|
|
|
|
|
}
|
2014-11-08 13:55:38 +01:00
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void testGenericActor() {
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
ActorRef genericTestActor = system.actorOf(Props.create(GenericActor.class), "genericActor");
|
2014-11-20 16:57:49 +01:00
|
|
|
GenericMessage<String> genericMessage = new GenericMessage<String>("a");
|
2014-11-08 13:55:38 +01:00
|
|
|
|
|
|
|
|
genericTestActor.tell(genericMessage, getRef());
|
|
|
|
|
expectMsgEquals("A");
|
|
|
|
|
}};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
public void actorShouldNotRespondForEmptyMessage() {
|
|
|
|
|
new JavaTestKit(system) {{
|
|
|
|
|
ActorRef genericTestActor = system.actorOf(Props.create(GenericActorWithPredicate.class), "genericActorWithPredicate");
|
2014-11-20 16:57:49 +01:00
|
|
|
GenericMessage<String> emptyGenericMessage = new GenericMessage<String>("");
|
|
|
|
|
GenericMessage<String> nonEmptyGenericMessage = new GenericMessage<String>("a");
|
2014-11-08 13:55:38 +01:00
|
|
|
|
|
|
|
|
genericTestActor.tell(emptyGenericMessage, getRef());
|
|
|
|
|
expectNoMsg();
|
|
|
|
|
|
|
|
|
|
genericTestActor.tell(nonEmptyGenericMessage, getRef());
|
|
|
|
|
expectMsgEquals("A");
|
|
|
|
|
}};
|
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
}
|