rename akka-docs dir to docs (#62)
This commit is contained in:
parent
13dce0ec69
commit
708da8caec
1029 changed files with 2033 additions and 2039 deletions
21
docs/src/test/java/jdocs/future/ActorWithFuture.java
Normal file
21
docs/src/test/java/jdocs/future/ActorWithFuture.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (C) 2018-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.future;
|
||||
|
||||
// #context-dispatcher
|
||||
import org.apache.pekko.actor.AbstractActor;
|
||||
import org.apache.pekko.dispatch.Futures;
|
||||
|
||||
public class ActorWithFuture extends AbstractActor {
|
||||
ActorWithFuture() {
|
||||
Futures.future(() -> "hello", getContext().dispatcher());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Receive createReceive() {
|
||||
return AbstractActor.emptyBehavior();
|
||||
}
|
||||
}
|
||||
// #context-dispatcher
|
||||
80
docs/src/test/java/jdocs/future/FutureDocTest.java
Normal file
80
docs/src/test/java/jdocs/future/FutureDocTest.java
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
|
||||
*/
|
||||
|
||||
package jdocs.future;
|
||||
|
||||
import org.apache.pekko.actor.typed.ActorSystem;
|
||||
import org.apache.pekko.dispatch.Futures;
|
||||
import org.apache.pekko.pattern.Patterns;
|
||||
import org.apache.pekko.testkit.AkkaJUnitActorSystemResource;
|
||||
import org.apache.pekko.testkit.AkkaSpec;
|
||||
import org.apache.pekko.util.Timeout;
|
||||
import jdocs.AbstractJavaTest;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import scala.compat.java8.FutureConverters;
|
||||
import scala.concurrent.Await;
|
||||
import scala.concurrent.ExecutionContext;
|
||||
import scala.concurrent.Future;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
import static org.apache.pekko.actor.typed.javadsl.Adapter.toTyped;
|
||||
import static org.apache.pekko.dispatch.Futures.future;
|
||||
// #imports
|
||||
import org.apache.pekko.pattern.Patterns;
|
||||
|
||||
// #imports
|
||||
import static java.util.concurrent.TimeUnit.SECONDS;
|
||||
|
||||
public class FutureDocTest extends AbstractJavaTest {
|
||||
|
||||
@ClassRule
|
||||
public static AkkaJUnitActorSystemResource actorSystemResource =
|
||||
new AkkaJUnitActorSystemResource("FutureDocTest", AkkaSpec.testConf());
|
||||
|
||||
private final ActorSystem<Void> system = toTyped(actorSystemResource.getSystem());
|
||||
|
||||
@Test(expected = java.util.concurrent.CompletionException.class)
|
||||
public void useAfter() throws Exception {
|
||||
final ExecutionContext ec = system.executionContext();
|
||||
// #after
|
||||
CompletionStage<String> failWithException =
|
||||
CompletableFuture.supplyAsync(
|
||||
() -> {
|
||||
throw new IllegalStateException("OHNOES1");
|
||||
});
|
||||
CompletionStage<String> delayed =
|
||||
Patterns.after(Duration.ofMillis(200), system, () -> failWithException);
|
||||
// #after
|
||||
Future<String> future =
|
||||
future(
|
||||
() -> {
|
||||
Thread.sleep(1000);
|
||||
return "foo";
|
||||
},
|
||||
ec);
|
||||
Future<String> result =
|
||||
Futures.firstCompletedOf(
|
||||
Arrays.<Future<String>>asList(future, FutureConverters.toScala(delayed)), ec);
|
||||
Timeout timeout = Timeout.create(Duration.ofSeconds(2));
|
||||
Await.result(result, timeout.duration());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useRetry() throws Exception {
|
||||
// #retry
|
||||
Callable<CompletionStage<String>> attempt = () -> CompletableFuture.completedFuture("test");
|
||||
|
||||
CompletionStage<String> retriedFuture =
|
||||
Patterns.retry(attempt, 3, java.time.Duration.ofMillis(200), system);
|
||||
// #retry
|
||||
|
||||
retriedFuture.toCompletableFuture().get(2, SECONDS);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue