* ActorContext as constructor parameter in AbstractBehavior, #27689 * additional test * additional doc clarification * another rebase
This commit is contained in:
parent
0719de035b
commit
91db18b564
66 changed files with 442 additions and 323 deletions
|
|
@ -13,7 +13,9 @@ public class BlockingActor extends AbstractBehavior<Integer> {
|
|||
return Behaviors.setup(BlockingActor::new);
|
||||
}
|
||||
|
||||
private BlockingActor(ActorContext<Integer> context) {}
|
||||
private BlockingActor(ActorContext<Integer> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive<Integer> createReceive() {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ public class BlockingDispatcherTest {
|
|||
Behaviors.setup(
|
||||
context -> {
|
||||
ActorRef<Integer> actor1 = context.spawn(BlockingActor.create(), "BlockingActor");
|
||||
ActorRef<Integer> actor2 = context.spawn(new PrintActor(), "PrintActor");
|
||||
ActorRef<Integer> actor2 = context.spawn(PrintActor.create(), "PrintActor");
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
actor1.tell(i);
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ public class DispatcherDocTest {
|
|||
// #defining-dispatcher-in-code
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(), "PrintActor", DispatcherSelector.fromConfig("my-dispatcher"));
|
||||
PrintActor.create(), "PrintActor", DispatcherSelector.fromConfig("my-dispatcher"));
|
||||
// #defining-dispatcher-in-code
|
||||
}
|
||||
|
||||
|
|
@ -29,7 +29,7 @@ public class DispatcherDocTest {
|
|||
// #defining-fixed-pool-size-dispatcher
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(),
|
||||
PrintActor.create(),
|
||||
"PrintActor",
|
||||
DispatcherSelector.fromConfig("blocking-io-dispatcher"));
|
||||
// #defining-fixed-pool-size-dispatcher
|
||||
|
|
@ -39,7 +39,9 @@ public class DispatcherDocTest {
|
|||
// #defining-pinned-dispatcher
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(), "PrintActor", DispatcherSelector.fromConfig("my-pinned-dispatcher"));
|
||||
PrintActor.create(),
|
||||
"PrintActor",
|
||||
DispatcherSelector.fromConfig("my-pinned-dispatcher"));
|
||||
// #defining-pinned-dispatcher
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,7 +11,11 @@ import akka.actor.typed.javadsl.*;
|
|||
class PrintActor extends AbstractBehavior<Integer> {
|
||||
|
||||
public static Behavior<Integer> create() {
|
||||
return Behaviors.setup(context -> new PrintActor());
|
||||
return Behaviors.setup(PrintActor::new);
|
||||
}
|
||||
|
||||
private PrintActor(ActorContext<Integer> context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ class SeparateDispatcherFutureActor extends AbstractBehavior<Integer> {
|
|||
}
|
||||
|
||||
private SeparateDispatcherFutureActor(ActorContext<Integer> context) {
|
||||
super(context);
|
||||
ec =
|
||||
context
|
||||
.getSystem()
|
||||
|
|
|
|||
|
|
@ -52,13 +52,11 @@ interface SharedMutableStateDocTest {
|
|||
}
|
||||
}
|
||||
|
||||
private final ActorContext<Command> context;
|
||||
|
||||
private String state = "";
|
||||
private Set<String> mySet = new HashSet<>();
|
||||
|
||||
public MyActor(ActorContext<Command> context) {
|
||||
this.context = context;
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -96,12 +94,13 @@ interface SharedMutableStateDocTest {
|
|||
// Turn the future result into a message that is sent to
|
||||
// self when future completes
|
||||
CompletableFuture<String> futureResult = expensiveCalculation();
|
||||
context.pipeToSelf(
|
||||
futureResult,
|
||||
(result, failure) -> {
|
||||
if (result != null) return new UpdateState(result);
|
||||
else throw new RuntimeException(failure);
|
||||
});
|
||||
getContext()
|
||||
.pipeToSelf(
|
||||
futureResult,
|
||||
(result, failure) -> {
|
||||
if (result != null) return new UpdateState(result);
|
||||
else throw new RuntimeException(failure);
|
||||
});
|
||||
|
||||
// Another example of incorrect approach
|
||||
// mutating actor state from ask future callback
|
||||
|
|
@ -110,7 +109,7 @@ interface SharedMutableStateDocTest {
|
|||
message.otherActor,
|
||||
Query::new,
|
||||
Duration.ofSeconds(3),
|
||||
context.getSystem().scheduler());
|
||||
getContext().getSystem().scheduler());
|
||||
response.whenComplete(
|
||||
(result, failure) -> {
|
||||
if (result != null) state = "new state: " + result;
|
||||
|
|
@ -118,15 +117,16 @@ interface SharedMutableStateDocTest {
|
|||
|
||||
// use context.ask instead, turns the completion
|
||||
// into a message sent to self
|
||||
context.ask(
|
||||
String.class,
|
||||
message.otherActor,
|
||||
Duration.ofSeconds(3),
|
||||
Query::new,
|
||||
(result, failure) -> {
|
||||
if (result != null) return new UpdateState(result);
|
||||
else throw new RuntimeException(failure);
|
||||
});
|
||||
getContext()
|
||||
.ask(
|
||||
String.class,
|
||||
message.otherActor,
|
||||
Duration.ofSeconds(3),
|
||||
Query::new,
|
||||
(result, failure) -> {
|
||||
if (result != null) return new UpdateState(result);
|
||||
else throw new RuntimeException(failure);
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue