Improve Java perspective dispatcher docs (#27521)
This commit is contained in:
parent
acee1b4185
commit
29b95f166a
14 changed files with 230 additions and 207 deletions
|
|
@ -1,35 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.actor;
|
||||
|
||||
import akka.actor.AbstractActor;
|
||||
import akka.dispatch.Futures;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
// #blocking-in-future
|
||||
class BlockingFutureActor extends AbstractActor {
|
||||
ExecutionContext ec = getContext().getDispatcher();
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return receiveBuilder()
|
||||
.match(
|
||||
Integer.class,
|
||||
i -> {
|
||||
System.out.println("Calling blocking Future: " + i);
|
||||
Future<Integer> f =
|
||||
Futures.future(
|
||||
() -> {
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Blocking future finished: " + i);
|
||||
return i;
|
||||
},
|
||||
ec);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
// #blocking-in-future
|
||||
|
|
@ -20,7 +20,11 @@ public class BlockingActor extends AbstractBehavior<Integer> {
|
|||
.onMessage(
|
||||
Integer.class,
|
||||
i -> {
|
||||
Thread.sleep(5000); // block for 5 seconds, representing blocking I/O, etc
|
||||
// DO NOT DO THIS HERE: this is an example of incorrect code,
|
||||
// better alternatives are described futher on.
|
||||
|
||||
// block for 5 seconds, representing blocking I/O, etc
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Blocking operation finished: " + i);
|
||||
return Behaviors.same();
|
||||
})
|
||||
|
|
|
|||
|
|
@ -13,8 +13,7 @@ public class BlockingDispatcherTest {
|
|||
Behavior<Void> root =
|
||||
Behaviors.setup(
|
||||
context -> {
|
||||
ActorRef<Integer> actor1 =
|
||||
context.spawn(BlockingFutureActor.create(), "BlockingFutureActor");
|
||||
ActorRef<Integer> actor1 = context.spawn(BlockingActor.create(), "BlockingActor");
|
||||
ActorRef<Integer> actor2 = context.spawn(new PrintActor(), "PrintActor");
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
|
|
|
|||
|
|
@ -1,49 +0,0 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.actor.typed;
|
||||
|
||||
import akka.actor.typed.*;
|
||||
import akka.actor.typed.javadsl.*;
|
||||
import akka.dispatch.Futures;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
// #blocking-in-future
|
||||
class BlockingFutureActor extends AbstractBehavior<Integer> {
|
||||
private final ExecutionContext ec;
|
||||
|
||||
public static Behavior<Integer> create() {
|
||||
return Behaviors.setup(BlockingFutureActor::new);
|
||||
}
|
||||
|
||||
private BlockingFutureActor(ActorContext<Integer> context) {
|
||||
ec = context.getExecutionContext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return newReceiveBuilder()
|
||||
.onMessage(
|
||||
Integer.class,
|
||||
i -> {
|
||||
triggerFutureBlockingOperation(i, ec);
|
||||
return Behaviors.same();
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
private static final void triggerFutureBlockingOperation(Integer i, ExecutionContext ec) {
|
||||
System.out.println("Calling blocking Future: " + i);
|
||||
Future<Integer> f =
|
||||
Futures.future(
|
||||
() -> {
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Blocking future finished: " + i);
|
||||
return i;
|
||||
},
|
||||
ec);
|
||||
}
|
||||
}
|
||||
// #blocking-in-future
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.actor.typed;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import scala.concurrent.ExecutionContextExecutor;
|
||||
|
||||
import akka.actor.typed.*;
|
||||
import akka.actor.typed.javadsl.*;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class DispatcherDocTest {
|
||||
|
||||
private final ActorSystem<Void> system = null;
|
||||
private final ActorContext<Void> context = null;
|
||||
|
||||
public void defineDispatcherInCode() {
|
||||
// #defining-dispatcher-in-code
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(), "PrintActor", DispatcherSelector.fromConfig("my-dispatcher"));
|
||||
// #defining-dispatcher-in-code
|
||||
}
|
||||
|
||||
public void defineFixedPoolSizeDispatcher() {
|
||||
// #defining-fixed-pool-size-dispatcher
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(),
|
||||
"PrintActor",
|
||||
DispatcherSelector.fromConfig("blocking-io-dispatcher"));
|
||||
// #defining-fixed-pool-size-dispatcher
|
||||
}
|
||||
|
||||
public void definePinnedDispatcher() {
|
||||
// #defining-pinned-dispatcher
|
||||
ActorRef<Integer> myActor =
|
||||
context.spawn(
|
||||
new PrintActor(), "PrintActor", DispatcherSelector.fromConfig("my-pinned-dispatcher"));
|
||||
// #defining-pinned-dispatcher
|
||||
}
|
||||
|
||||
public void compileLookup() {
|
||||
// #lookup
|
||||
// this is scala.concurrent.ExecutionContextExecutor, which implements
|
||||
// both scala.concurrent.ExecutionContext (for use with Futures, Scheduler, etc.)
|
||||
// and java.util.concurrent.Executor (for use with CompletableFuture etc.)
|
||||
final ExecutionContextExecutor ex =
|
||||
system.dispatchers().lookup(DispatcherSelector.fromConfig("my-dispatcher"));
|
||||
// #lookup
|
||||
}
|
||||
}
|
||||
|
|
@ -4,15 +4,15 @@
|
|||
|
||||
package jdocs.actor.typed;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import akka.actor.typed.*;
|
||||
import akka.actor.typed.javadsl.*;
|
||||
import akka.dispatch.Futures;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
// #separate-dispatcher
|
||||
class SeparateDispatcherFutureActor extends AbstractBehavior<Integer> {
|
||||
final ExecutionContext ec;
|
||||
final Executor ec;
|
||||
|
||||
public static Behavior<Integer> create() {
|
||||
return Behaviors.setup(SeparateDispatcherFutureActor::new);
|
||||
|
|
@ -38,14 +38,18 @@ class SeparateDispatcherFutureActor extends AbstractBehavior<Integer> {
|
|||
.build();
|
||||
}
|
||||
|
||||
private static final void triggerFutureBlockingOperation(Integer i, ExecutionContext ec) {
|
||||
private static final void triggerFutureBlockingOperation(Integer i, Executor ec) {
|
||||
System.out.println("Calling blocking Future on separate dispatcher: " + i);
|
||||
Future<Integer> f =
|
||||
Futures.future(
|
||||
CompletableFuture<Integer> f =
|
||||
CompletableFuture.supplyAsync(
|
||||
() -> {
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Blocking future finished: " + i);
|
||||
return i;
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
System.out.println("Blocking future finished: " + i);
|
||||
return i;
|
||||
} catch (InterruptedException e) {
|
||||
return -1;
|
||||
}
|
||||
},
|
||||
ec);
|
||||
}
|
||||
|
|
@ -16,6 +16,7 @@ import jdocs.actor.MyActor;
|
|||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.ExecutionContextExecutor;
|
||||
|
||||
// #imports
|
||||
import akka.actor.*;
|
||||
|
|
@ -88,9 +89,10 @@ public class DispatcherDocTest extends AbstractJavaTest {
|
|||
@SuppressWarnings("unused")
|
||||
public void compileLookup() {
|
||||
// #lookup
|
||||
// this is scala.concurrent.ExecutionContext
|
||||
// for use with Futures, Scheduler, etc.
|
||||
final ExecutionContext ex = system.dispatchers().lookup("my-dispatcher");
|
||||
// this is scala.concurrent.ExecutionContextExecutor, which implements
|
||||
// both scala.concurrent.ExecutionContext (for use with Futures, Scheduler, etc.)
|
||||
// and java.util.concurrent.Executor (for use with CompletableFuture etc.)
|
||||
final ExecutionContextExecutor ex = system.dispatchers().lookup("my-dispatcher");
|
||||
// #lookup
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue