2018-10-29 17:19:37 +08:00
|
|
|
|
/*
|
2020-01-02 07:24:59 -05:00
|
|
|
|
* Copyright (C) 2009-2020 Lightbend Inc. <https://www.lightbend.com>
|
2014-02-21 12:43:30 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2017-03-16 09:30:00 +01:00
|
|
|
|
package jdocs.actor;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
import akka.actor.*;
|
2017-03-17 03:02:47 +08:00
|
|
|
|
import akka.testkit.javadsl.TestKit;
|
2014-03-20 12:05:32 +01:00
|
|
|
|
import com.typesafe.config.Config;
|
|
|
|
|
|
import com.typesafe.config.ConfigFactory;
|
2017-03-16 09:30:00 +01:00
|
|
|
|
import jdocs.AbstractJavaTest;
|
|
|
|
|
|
import static jdocs.actor.Messages.Swap.Swap;
|
|
|
|
|
|
import static jdocs.actor.Messages.*;
|
2016-12-01 18:49:38 +01:00
|
|
|
|
import akka.actor.CoordinatedShutdown;
|
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
|
|
|
|
|
2016-12-01 18:49:38 +01:00
|
|
|
|
import akka.Done;
|
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
|
|
|
|
import java.util.Optional;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-05-14 10:22:02 +02:00
|
|
|
|
import java.time.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 akka.testkit.TestActors;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
import org.junit.AfterClass;
|
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-props
|
2014-02-21 12:43:30 +01:00
|
|
|
|
import akka.actor.Props;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-props
|
|
|
|
|
|
// #import-actorRef
|
2014-02-21 12:43:30 +01:00
|
|
|
|
import akka.actor.ActorRef;
|
|
|
|
|
|
import akka.actor.ActorSystem;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-actorRef
|
|
|
|
|
|
// #import-identify
|
2014-02-21 12:43:30 +01:00
|
|
|
|
import akka.actor.ActorIdentity;
|
|
|
|
|
|
import akka.actor.ActorSelection;
|
|
|
|
|
|
import akka.actor.Identify;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-identify
|
|
|
|
|
|
// #import-ask
|
2018-12-06 22:40:43 +08:00
|
|
|
|
import static akka.pattern.Patterns.ask;
|
|
|
|
|
|
import static akka.pattern.Patterns.pipe;
|
2017-03-16 09:30:00 +01:00
|
|
|
|
|
2017-03-14 15:51:44 +05:00
|
|
|
|
import java.util.concurrent.CompletableFuture;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-ask
|
|
|
|
|
|
// #import-gracefulStop
|
2018-12-06 22:40:43 +08:00
|
|
|
|
import static akka.pattern.Patterns.gracefulStop;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
import akka.pattern.AskTimeoutException;
|
2017-03-14 15:51:44 +05:00
|
|
|
|
import java.util.concurrent.CompletionStage;
|
2017-07-26 04:12:16 +09:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-gracefulStop
|
|
|
|
|
|
// #import-terminated
|
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 akka.actor.Terminated;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #import-terminated
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2016-02-11 16:39:25 +01:00
|
|
|
|
public class ActorDocTest extends AbstractJavaTest {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public static Config config =
|
|
|
|
|
|
ConfigFactory.parseString(
|
|
|
|
|
|
"akka {\n"
|
|
|
|
|
|
+ " loggers = [\"akka.testkit.TestEventListener\"]\n"
|
|
|
|
|
|
+ " loglevel = \"WARNING\"\n"
|
|
|
|
|
|
+ " stdout-loglevel = \"WARNING\"\n"
|
|
|
|
|
|
+ "}\n");
|
2014-03-20 12:05:32 +01:00
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
|
static ActorSystem system = null;
|
|
|
|
|
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
|
|
public static void beforeClass() {
|
2014-03-20 12:05:32 +01:00
|
|
|
|
system = ActorSystem.create("ActorDocTest", config);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@AfterClass
|
2018-05-14 10:22:02 +02:00
|
|
|
|
public static void afterClass() {
|
|
|
|
|
|
TestKit.shutdownActorSystem(system);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #context-actorOf
|
|
|
|
|
|
static class FirstActor 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
|
|
|
|
final ActorRef child = getContext().actorOf(Props.create(MyActor.class), "myChild");
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #plus-some-behavior
|
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() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().matchAny(x -> getSender().tell(x, getSelf())).build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #plus-some-behavior
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #context-actorOf
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public static class SomeActor extends AbstractActor {
|
|
|
|
|
|
// #createReceive
|
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() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().match(String.class, s -> System.out.println(s.toLowerCase())).build();
|
2014-03-20 12:05:32 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #createReceive
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #well-structured
|
|
|
|
|
|
static class WellStructuredActor extends AbstractActor {
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public static class Msg1 {}
|
2019-01-12 04:00:53 +08: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
|
|
|
|
public static class Msg2 {}
|
2019-01-12 04:00:53 +08: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
|
|
|
|
public static class Msg3 {}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.match(Msg1.class, this::receiveMsg1)
|
|
|
|
|
|
.match(Msg2.class, this::receiveMsg2)
|
|
|
|
|
|
.match(Msg3.class, this::receiveMsg3)
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg1(Msg1 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg2(Msg2 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg3(Msg3 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #well-structured
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #optimized
|
|
|
|
|
|
static class OptimizedActor extends UntypedAbstractActor {
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public static class Msg1 {}
|
2019-01-12 04:00:53 +08: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
|
|
|
|
public static class Msg2 {}
|
2019-01-12 04:00:53 +08: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
|
|
|
|
public static class Msg3 {}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
|
@Override
|
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
|
|
|
|
public void onReceive(Object msg) throws Exception {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
if (msg instanceof Msg1) receiveMsg1((Msg1) msg);
|
|
|
|
|
|
else if (msg instanceof Msg2) receiveMsg2((Msg2) msg);
|
|
|
|
|
|
else if (msg instanceof Msg3) receiveMsg3((Msg3) msg);
|
|
|
|
|
|
else unhandled(msg);
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg1(Msg1 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg2(Msg2 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
private void receiveMsg3(Msg3 msg) {
|
|
|
|
|
|
// actual work
|
|
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #optimized
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public static class ActorWithArgs extends AbstractActor {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
private final String args;
|
|
|
|
|
|
|
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
|
|
|
|
public ActorWithArgs(String args) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
this.args = args;
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().matchAny(x -> {}).build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #props-factory
|
|
|
|
|
|
static class DemoActor extends AbstractActor {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
/**
|
|
|
|
|
|
* Create Props for an actor of this type.
|
2019-01-12 04:00:53 +08:00
|
|
|
|
*
|
2014-02-21 12:43:30 +01:00
|
|
|
|
* @param magicNumber The magic number to be passed to this actor’s constructor.
|
2019-01-12 04:00:53 +08:00
|
|
|
|
* @return a Props for creating this actor, which can then be further configured (e.g. calling
|
|
|
|
|
|
* `.withDispatcher()` on it)
|
2014-02-21 12:43:30 +01:00
|
|
|
|
*/
|
|
|
|
|
|
static Props props(Integer magicNumber) {
|
|
|
|
|
|
// You need to specify the actual type of the returned actor
|
|
|
|
|
|
// since Java 8 lambdas have some runtime type information erased
|
|
|
|
|
|
return Props.create(DemoActor.class, () -> new DemoActor(magicNumber));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private final Integer magicNumber;
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public DemoActor(Integer magicNumber) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
this.magicNumber = magicNumber;
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.match(
|
|
|
|
|
|
Integer.class,
|
|
|
|
|
|
i -> {
|
|
|
|
|
|
getSender().tell(i + magicNumber, getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #props-factory
|
|
|
|
|
|
public
|
|
|
|
|
|
// #props-factory
|
|
|
|
|
|
static class SomeOtherActor extends AbstractActor {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// Props(new DemoActor(42)) would not be safe
|
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
|
|
|
|
ActorRef demoActor = getContext().actorOf(DemoActor.props(42), "demo");
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// ...
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #props-factory
|
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 AbstractActor.emptyBehavior();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #props-factory
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #props-factory
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #messages-in-companion
|
|
|
|
|
|
static class DemoMessagesActor extends AbstractLoggingActor {
|
2015-01-16 23:43:27 +05:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public static class Greeting {
|
2015-01-16 23:43:27 +05:00
|
|
|
|
private final String from;
|
|
|
|
|
|
|
|
|
|
|
|
public Greeting(String from) {
|
|
|
|
|
|
this.from = from;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String getGreeter() {
|
|
|
|
|
|
return from;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.match(
|
|
|
|
|
|
Greeting.class,
|
|
|
|
|
|
g -> {
|
|
|
|
|
|
log().info("I was greeted by {}", g.getGreeter());
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
2015-01-16 23:43:27 +05:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #messages-in-companion
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public static class LifecycleMethods extends AbstractActor {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return AbstractActor.emptyBehavior();
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
/*
|
|
|
|
|
|
* This section must be kept in sync with the actual Actor trait.
|
2018-12-06 22:40:43 +08: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
|
|
|
|
* BOYSCOUT RULE: whenever you read this, verify that!
|
|
|
|
|
|
*/
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #lifecycle-callbacks
|
|
|
|
|
|
public void preStart() {}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public void preRestart(Throwable reason, Optional<Object> message) {
|
|
|
|
|
|
for (ActorRef each : getContext().getChildren()) {
|
|
|
|
|
|
getContext().unwatch(each);
|
|
|
|
|
|
getContext().stop(each);
|
|
|
|
|
|
}
|
|
|
|
|
|
postStop();
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
public void postRestart(Throwable reason) {
|
|
|
|
|
|
preStart();
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public void postStop() {}
|
|
|
|
|
|
// #lifecycle-callbacks
|
2018-12-06 22:40:43 +08: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
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
|
public static class Hook extends AbstractActor {
|
|
|
|
|
|
ActorRef target = null;
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return AbstractActor.emptyBehavior();
|
2014-03-20 12:05:32 +01:00
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #preStart
|
2014-02-21 12:43:30 +01:00
|
|
|
|
@Override
|
|
|
|
|
|
public void preStart() {
|
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
|
|
|
|
target = getContext().actorOf(Props.create(MyActor.class, "target"));
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #preStart
|
|
|
|
|
|
// #postStop
|
2014-02-21 12:43:30 +01:00
|
|
|
|
@Override
|
|
|
|
|
|
public void postStop() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #clean-up-some-resources
|
2014-02-21 12:43:30 +01:00
|
|
|
|
final String message = "stopped";
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tell
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// don’t forget to think about who is the sender (2nd argument)
|
2017-03-16 09:30:00 +01:00
|
|
|
|
target.tell(message, getSelf());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #tell
|
2014-02-21 12:43:30 +01:00
|
|
|
|
final Object result = "";
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #forward
|
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
|
|
|
|
target.forward(result, getContext());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #forward
|
2014-02-21 12:43:30 +01:00
|
|
|
|
target = null;
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #clean-up-some-resources
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #postStop
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
// compilation test only
|
|
|
|
|
|
public void compileSelections() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-local
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// will look up this absolute path
|
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
|
|
|
|
getContext().actorSelection("/user/serviceA/actor");
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// will look up sibling beneath same supervisor
|
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
|
|
|
|
getContext().actorSelection("../joe");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-local
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-wildcard
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// will look all children to serviceB with names starting with worker
|
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
|
|
|
|
getContext().actorSelection("/user/serviceB/worker*");
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// will look up all siblings beneath same supervisor
|
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
|
|
|
|
getContext().actorSelection("../*");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-wildcard
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-remote
|
2019-05-15 18:01:34 +02:00
|
|
|
|
getContext().actorSelection("akka://app@otherhost:1234/user/serviceB");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #selection-remote
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class ReplyException extends AbstractActor {
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchAny(
|
|
|
|
|
|
x -> {
|
|
|
|
|
|
// #reply-exception
|
|
|
|
|
|
try {
|
|
|
|
|
|
String result = operation();
|
|
|
|
|
|
getSender().tell(result, getSelf());
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
|
getSender().tell(new akka.actor.Status.Failure(e), getSelf());
|
|
|
|
|
|
throw e;
|
|
|
|
|
|
}
|
|
|
|
|
|
// #reply-exception
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String operation() {
|
|
|
|
|
|
return "Hi";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #gracefulStop-actor
|
|
|
|
|
|
static class Manager extends AbstractActor {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
private static enum Shutdown {
|
|
|
|
|
|
Shutdown
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
|
2014-02-21 12:43:30 +01:00
|
|
|
|
public static final Shutdown SHUTDOWN = Shutdown.Shutdown;
|
|
|
|
|
|
|
|
|
|
|
|
private ActorRef worker =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
getContext().watch(getContext().actorOf(Props.create(Cruncher.class), "worker"));
|
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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"job",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
worker.tell("crunch", getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
SHUTDOWN,
|
|
|
|
|
|
x -> {
|
|
|
|
|
|
worker.tell(PoisonPill.getInstance(), getSelf());
|
|
|
|
|
|
getContext().become(shuttingDown());
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private AbstractActor.Receive shuttingDown() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"job", s -> getSender().tell("service unavailable, shutting down", getSelf()))
|
|
|
|
|
|
.match(Terminated.class, t -> t.actor().equals(worker), t -> getContext().stop(getSelf()))
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #gracefulStop-actor
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void usePatternsGracefulStop() throws Exception {
|
|
|
|
|
|
ActorRef actorRef = system.actorOf(Props.create(Manager.class));
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #gracefulStop
|
2014-02-21 12:43:30 +01:00
|
|
|
|
try {
|
2017-03-14 15:51:44 +05:00
|
|
|
|
CompletionStage<Boolean> stopped =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
gracefulStop(actorRef, Duration.ofSeconds(5), Manager.SHUTDOWN);
|
2017-03-14 15:51:44 +05:00
|
|
|
|
stopped.toCompletableFuture().get(6, TimeUnit.SECONDS);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// the actor has been stopped
|
|
|
|
|
|
} catch (AskTimeoutException e) {
|
|
|
|
|
|
// the actor wasn't stopped within 5 seconds
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #gracefulStop
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static class Cruncher 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() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
return receiveBuilder().matchEquals("crunch", s -> {}).build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #swapper
|
|
|
|
|
|
static class Swapper extends AbstractLoggingActor {
|
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()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
Swap,
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
log().info("Hi");
|
|
|
|
|
|
getContext()
|
|
|
|
|
|
.become(
|
|
|
|
|
|
receiveBuilder()
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
Swap,
|
|
|
|
|
|
x -> {
|
|
|
|
|
|
log().info("Ho");
|
|
|
|
|
|
getContext()
|
|
|
|
|
|
.unbecome(); // resets the latest 'become' (just for fun)
|
|
|
|
|
|
})
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
false); // push on top instead of replace
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #swapper
|
|
|
|
|
|
public
|
|
|
|
|
|
// #swapper
|
|
|
|
|
|
static class SwapperApp {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
ActorSystem system = ActorSystem.create("SwapperSystem");
|
|
|
|
|
|
ActorRef swapper = system.actorOf(Props.create(Swapper.class), "swapper");
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Hi
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Ho
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Hi
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Ho
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Hi
|
|
|
|
|
|
swapper.tell(Swap, ActorRef.noSender()); // logs Ho
|
2014-08-25 15:49:28 +02:00
|
|
|
|
system.terminate();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #swapper
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void creatingActorWithSystemActorOf() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #system-actorOf
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// ActorSystem is a heavy object: create only one per application
|
2014-03-20 12:05:32 +01:00
|
|
|
|
final ActorSystem system = ActorSystem.create("MySystem", config);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
final ActorRef myActor = system.actorOf(Props.create(MyActor.class), "myactor");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #system-actorOf
|
2014-02-21 12:43:30 +01:00
|
|
|
|
try {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
myActor.tell("hello", getRef());
|
|
|
|
|
|
expectMsgEquals("hello");
|
2016-10-23 22:35:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
} finally {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
TestKit.shutdownActorSystem(system);
|
2016-10-23 22:35:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void creatingGraduallyBuiltActorWithSystemActorOf() {
|
|
|
|
|
|
final ActorSystem system = ActorSystem.create("MySystem", config);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
final ActorRef actor =
|
|
|
|
|
|
system.actorOf(Props.create(GraduallyBuiltActor.class), "graduallyBuiltActor");
|
2016-10-23 22:35:18 +02:00
|
|
|
|
try {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2016-10-23 22:35:18 +02:00
|
|
|
|
{
|
|
|
|
|
|
actor.tell("hello", getRef());
|
|
|
|
|
|
expectMsgEquals("hello");
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
} finally {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
TestKit.shutdownActorSystem(system);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void creatingPropsConfig() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #creating-props
|
2014-02-21 12:43:30 +01:00
|
|
|
|
Props props1 = Props.create(MyActor.class);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
Props props2 =
|
|
|
|
|
|
Props.create(ActorWithArgs.class, () -> new ActorWithArgs("arg")); // careful, see below
|
2014-02-21 12:43:30 +01:00
|
|
|
|
Props props3 = Props.create(ActorWithArgs.class, "arg");
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #creating-props
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #creating-props-deprecated
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// NOT RECOMMENDED within another actor:
|
|
|
|
|
|
// encourages to close over enclosing class
|
2019-01-12 04:00:53 +08:00
|
|
|
|
Props props7 = Props.create(ActorWithArgs.class, () -> new ActorWithArgs("arg"));
|
|
|
|
|
|
// #creating-props-deprecated
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
@Test(expected = IllegalArgumentException.class)
|
2014-02-21 12:43:30 +01:00
|
|
|
|
public void creatingPropsIllegal() {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #creating-props-illegal
|
2014-02-21 12:43:30 +01:00
|
|
|
|
// This will throw an IllegalArgumentException since some runtime
|
|
|
|
|
|
// type information of the lambda is erased.
|
|
|
|
|
|
// Use Props.create(actorClass, Creator) instead.
|
|
|
|
|
|
Props props = Props.create(() -> new ActorWithArgs("arg"));
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #creating-props-illegal
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #receive-timeout
|
|
|
|
|
|
static class ReceiveTimeoutActor extends AbstractActor {
|
|
|
|
|
|
// #receive-timeout
|
2017-03-16 09:30:00 +01:00
|
|
|
|
ActorRef target = getContext().getSystem().deadLetters();
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #receive-timeout
|
2014-03-20 12:05:32 +01:00
|
|
|
|
public ReceiveTimeoutActor() {
|
|
|
|
|
|
// To set an initial delay
|
2018-05-14 10:22:02 +02:00
|
|
|
|
getContext().setReceiveTimeout(Duration.ofSeconds(10));
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"Hello",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
// To set in a response to a message
|
|
|
|
|
|
getContext().setReceiveTimeout(Duration.ofSeconds(1));
|
|
|
|
|
|
// #receive-timeout
|
|
|
|
|
|
target = getSender();
|
|
|
|
|
|
target.tell("Hello world", getSelf());
|
|
|
|
|
|
// #receive-timeout
|
|
|
|
|
|
})
|
|
|
|
|
|
.match(
|
|
|
|
|
|
ReceiveTimeout.class,
|
|
|
|
|
|
r -> {
|
|
|
|
|
|
// To turn it off
|
|
|
|
|
|
getContext().cancelReceiveTimeout();
|
|
|
|
|
|
// #receive-timeout
|
|
|
|
|
|
target.tell("timeout", getSelf());
|
|
|
|
|
|
// #receive-timeout
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// #receive-timeout
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void using_receiveTimeout() {
|
|
|
|
|
|
final ActorRef myActor = system.actorOf(Props.create(ReceiveTimeoutActor.class));
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
myActor.tell("Hello", getRef());
|
|
|
|
|
|
expectMsgEquals("Hello world");
|
|
|
|
|
|
expectMsgEquals("timeout");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #hot-swap-actor
|
|
|
|
|
|
static class HotSwapActor 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
|
|
|
|
private AbstractActor.Receive angry;
|
|
|
|
|
|
private AbstractActor.Receive happy;
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
|
public HotSwapActor() {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
angry =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
receiveBuilder()
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"foo",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getSender().tell("I am already angry?", getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"bar",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getContext().become(happy);
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
happy =
|
|
|
|
|
|
receiveBuilder()
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"bar",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getSender().tell("I am already happy :-)", getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"foo",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getContext().become(angry);
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals("foo", s -> getContext().become(angry))
|
|
|
|
|
|
.matchEquals("bar", s -> getContext().become(happy))
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #hot-swap-actor
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void using_hot_swap() {
|
|
|
|
|
|
final ActorRef actor = system.actorOf(Props.create(HotSwapActor.class), "hot");
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
actor.tell("foo", getRef());
|
|
|
|
|
|
actor.tell("foo", getRef());
|
|
|
|
|
|
expectMsgEquals("I am already angry?");
|
|
|
|
|
|
actor.tell("bar", getRef());
|
|
|
|
|
|
actor.tell("bar", getRef());
|
|
|
|
|
|
expectMsgEquals("I am already happy :-)");
|
|
|
|
|
|
actor.tell("foo", getRef());
|
|
|
|
|
|
actor.tell("foo", getRef());
|
|
|
|
|
|
expectMsgEquals("I am already angry?");
|
2018-05-14 10:22:02 +02:00
|
|
|
|
expectNoMessage(Duration.ofSeconds(1));
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #stash
|
|
|
|
|
|
static class ActorWithProtocol extends AbstractActorWithStash {
|
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()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"open",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getContext()
|
|
|
|
|
|
.become(
|
|
|
|
|
|
receiveBuilder()
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"write",
|
|
|
|
|
|
ws -> {
|
|
|
|
|
|
/* do writing */
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"close",
|
|
|
|
|
|
cs -> {
|
|
|
|
|
|
unstashAll();
|
|
|
|
|
|
getContext().unbecome();
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchAny(msg -> stash())
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
false);
|
|
|
|
|
|
})
|
|
|
|
|
|
.matchAny(msg -> stash())
|
|
|
|
|
|
.build();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// #stash
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void using_Stash() {
|
|
|
|
|
|
final ActorRef actor = system.actorOf(Props.create(ActorWithProtocol.class), "stash");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #watch
|
|
|
|
|
|
static class WatchActor 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
|
|
|
|
private final ActorRef child = getContext().actorOf(Props.empty(), "target");
|
2014-02-21 12:43:30 +01:00
|
|
|
|
private ActorRef lastSender = system.deadLetters();
|
|
|
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
|
public WatchActor() {
|
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
|
|
|
|
getContext().watch(child); // <-- this is the only call needed for registration
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.matchEquals(
|
|
|
|
|
|
"kill",
|
|
|
|
|
|
s -> {
|
|
|
|
|
|
getContext().stop(child);
|
|
|
|
|
|
lastSender = getSender();
|
|
|
|
|
|
})
|
|
|
|
|
|
.match(
|
|
|
|
|
|
Terminated.class,
|
|
|
|
|
|
t -> t.actor().equals(child),
|
|
|
|
|
|
t -> {
|
|
|
|
|
|
lastSender.tell("finished", getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #watch
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void using_watch() {
|
|
|
|
|
|
ActorRef actor = system.actorOf(Props.create(WatchActor.class));
|
|
|
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
actor.tell("kill", getRef());
|
|
|
|
|
|
expectMsgEquals("finished");
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public
|
|
|
|
|
|
// #identify
|
|
|
|
|
|
static class Follower extends AbstractActor {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
final Integer identifyId = 1;
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
public Follower() {
|
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
|
|
|
|
ActorSelection selection = getContext().actorSelection("/user/another");
|
2017-03-16 09:30:00 +01:00
|
|
|
|
selection.tell(new Identify(identifyId), getSelf());
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Override
|
|
|
|
|
|
public Receive createReceive() {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.match(
|
|
|
|
|
|
ActorIdentity.class,
|
|
|
|
|
|
id -> id.getActorRef().isPresent(),
|
|
|
|
|
|
id -> {
|
|
|
|
|
|
ActorRef ref = id.getActorRef().get();
|
|
|
|
|
|
getContext().watch(ref);
|
|
|
|
|
|
getContext().become(active(ref));
|
|
|
|
|
|
})
|
|
|
|
|
|
.match(
|
|
|
|
|
|
ActorIdentity.class,
|
|
|
|
|
|
id -> !id.getActorRef().isPresent(),
|
|
|
|
|
|
id -> {
|
|
|
|
|
|
getContext().stop(getSelf());
|
|
|
|
|
|
})
|
|
|
|
|
|
.build();
|
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
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
final AbstractActor.Receive active(final ActorRef another) {
|
|
|
|
|
|
return receiveBuilder()
|
2019-01-12 04:00:53 +08:00
|
|
|
|
.match(
|
|
|
|
|
|
Terminated.class, t -> t.actor().equals(another), t -> getContext().stop(getSelf()))
|
|
|
|
|
|
.build();
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #identify
|
2014-02-21 12:43:30 +01:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void using_Identify() {
|
|
|
|
|
|
ActorRef a = system.actorOf(Props.empty());
|
|
|
|
|
|
ActorRef b = system.actorOf(Props.create(Follower.class));
|
|
|
|
|
|
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
2014-02-21 12:43:30 +01:00
|
|
|
|
{
|
|
|
|
|
|
watch(b);
|
|
|
|
|
|
system.stop(a);
|
2018-05-14 10:22:02 +02:00
|
|
|
|
assertEquals(expectMsgClass(Duration.ofSeconds(2), Terminated.class).actor(), b);
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
|
@Test
|
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
|
|
|
|
public void usePatternsAskPipe() {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
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
|
|
|
|
{
|
|
|
|
|
|
ActorRef actorA = system.actorOf(TestActors.echoActorProps());
|
|
|
|
|
|
ActorRef actorB = system.actorOf(TestActors.echoActorProps());
|
|
|
|
|
|
ActorRef actorC = getRef();
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #ask-pipe
|
2018-12-06 22:40:43 +08:00
|
|
|
|
final Duration t = Duration.ofSeconds(5);
|
2017-03-14 15:51:44 +05:00
|
|
|
|
|
|
|
|
|
|
// using 1000ms timeout
|
|
|
|
|
|
CompletableFuture<Object> future1 =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
ask(actorA, "request", Duration.ofMillis(1000)).toCompletableFuture();
|
2017-03-14 15:51:44 +05:00
|
|
|
|
|
|
|
|
|
|
// using timeout from above
|
2019-01-12 04:00:53 +08:00
|
|
|
|
CompletableFuture<Object> future2 = ask(actorB, "another request", t).toCompletableFuture();
|
2017-03-14 15:51:44 +05:00
|
|
|
|
|
|
|
|
|
|
CompletableFuture<Result> transformed =
|
2019-01-12 04:00:53 +08:00
|
|
|
|
CompletableFuture.allOf(future1, future2)
|
|
|
|
|
|
.thenApply(
|
|
|
|
|
|
v -> {
|
|
|
|
|
|
String x = (String) future1.join();
|
|
|
|
|
|
String s = (String) future2.join();
|
|
|
|
|
|
return new Result(x, s);
|
|
|
|
|
|
});
|
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
|
|
|
|
|
|
|
|
|
|
pipe(transformed, system.dispatcher()).to(actorC);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #ask-pipe
|
2018-12-06 22:40:43 +08: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
|
|
|
|
expectMsgEquals(new Result("request", "another request"));
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2014-03-20 12:05:32 +01:00
|
|
|
|
}
|
2018-12-06 22:40:43 +08: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
|
|
|
|
@Test
|
|
|
|
|
|
public void useKill() {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
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
|
|
|
|
{
|
|
|
|
|
|
ActorRef victim = system.actorOf(TestActors.echoActorProps());
|
|
|
|
|
|
watch(victim);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #kill
|
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
|
|
|
|
victim.tell(akka.actor.Kill.getInstance(), ActorRef.noSender());
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2017-09-27 15:32:01 +09:00
|
|
|
|
// expecting the actor to indeed terminate:
|
2018-05-14 10:22:02 +02:00
|
|
|
|
expectTerminated(Duration.ofSeconds(3), victim);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #kill
|
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
|
|
|
|
}
|
|
|
|
|
|
};
|
2014-03-20 12:05:32 +01:00
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2014-03-20 12:05:32 +01:00
|
|
|
|
@Test
|
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
|
|
|
|
public void usePoisonPill() {
|
2017-03-17 03:02:47 +08:00
|
|
|
|
new TestKit(system) {
|
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
|
|
|
|
{
|
|
|
|
|
|
ActorRef victim = system.actorOf(TestActors.echoActorProps());
|
|
|
|
|
|
watch(victim);
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #poison-pill
|
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
|
|
|
|
victim.tell(akka.actor.PoisonPill.getInstance(), ActorRef.noSender());
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #poison-pill
|
2018-05-14 10:22:02 +02:00
|
|
|
|
expectTerminated(Duration.ofSeconds(3), victim);
|
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
|
|
|
|
}
|
|
|
|
|
|
};
|
2014-03-20 12:05:32 +01:00
|
|
|
|
}
|
2018-12-06 22:40:43 +08:00
|
|
|
|
|
2019-10-15 05:01:13 -06:00
|
|
|
|
private CompletionStage<Done> cleanup() {
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-12-01 18:49:38 +01:00
|
|
|
|
@Test
|
|
|
|
|
|
public void coordinatedShutdown() {
|
|
|
|
|
|
final ActorRef someActor = system.actorOf(Props.create(FirstActor.class));
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #coordinated-shutdown-addTask
|
|
|
|
|
|
CoordinatedShutdown.get(system)
|
|
|
|
|
|
.addTask(
|
|
|
|
|
|
CoordinatedShutdown.PhaseBeforeServiceUnbind(),
|
|
|
|
|
|
"someTaskName",
|
|
|
|
|
|
() -> {
|
|
|
|
|
|
return akka.pattern.Patterns.ask(someActor, "stop", Duration.ofSeconds(5))
|
|
|
|
|
|
.thenApply(reply -> Done.getInstance());
|
|
|
|
|
|
});
|
|
|
|
|
|
// #coordinated-shutdown-addTask
|
|
|
|
|
|
|
2019-10-15 05:01:13 -06:00
|
|
|
|
// #coordinated-shutdown-cancellable
|
|
|
|
|
|
Cancellable cancellable =
|
|
|
|
|
|
CoordinatedShutdown.get(system)
|
|
|
|
|
|
.addCancellableTask(
|
|
|
|
|
|
CoordinatedShutdown.PhaseBeforeServiceUnbind(), "someTaskCleanup", () -> cleanup());
|
|
|
|
|
|
// much later...
|
|
|
|
|
|
cancellable.cancel();
|
|
|
|
|
|
// #coordinated-shutdown-cancellable
|
|
|
|
|
|
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #coordinated-shutdown-jvm-hook
|
|
|
|
|
|
CoordinatedShutdown.get(system)
|
|
|
|
|
|
.addJvmShutdownHook(() -> System.out.println("custom JVM shutdown hook..."));
|
|
|
|
|
|
// #coordinated-shutdown-jvm-hook
|
2016-12-01 18:49:38 +01:00
|
|
|
|
|
|
|
|
|
|
// don't run this
|
|
|
|
|
|
if (false) {
|
2019-01-12 04:00:53 +08:00
|
|
|
|
// #coordinated-shutdown-run
|
|
|
|
|
|
CompletionStage<Done> done =
|
|
|
|
|
|
CoordinatedShutdown.get(system).runAll(CoordinatedShutdown.unknownReason());
|
|
|
|
|
|
// #coordinated-shutdown-run
|
2016-12-01 18:49:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-07-16 18:56:48 +10:00
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
public void coordinatedShutdownActorTermination() {
|
|
|
|
|
|
ActorRef someActor = system.actorOf(Props.create(FirstActor.class));
|
|
|
|
|
|
someActor.tell(PoisonPill.getInstance(), ActorRef.noSender());
|
|
|
|
|
|
// #coordinated-shutdown-addActorTerminationTask
|
|
|
|
|
|
CoordinatedShutdown.get(system)
|
|
|
|
|
|
.addActorTerminationTask(
|
|
|
|
|
|
CoordinatedShutdown.PhaseBeforeServiceUnbind(),
|
|
|
|
|
|
"someTaskName",
|
|
|
|
|
|
someActor,
|
|
|
|
|
|
Optional.of("stop"));
|
|
|
|
|
|
// #coordinated-shutdown-addActorTerminationTask
|
|
|
|
|
|
}
|
2014-02-21 12:43:30 +01:00
|
|
|
|
}
|