Futures patterns
Dependency
Pekko offers tiny helpers for use with Future
sCompletionStage
. These are part of Pekko’s core module:
- sbt
val PekkoVersion = "1.1.2" libraryDependencies += "org.apache.pekko" %% "pekko-actor" % PekkoVersion
- Maven
<properties> <scala.binary.version>2.13</scala.binary.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.pekko</groupId> <artifactId>pekko-bom_${scala.binary.version}</artifactId> <version>1.1.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.apache.pekko</groupId> <artifactId>pekko-actor_${scala.binary.version}</artifactId> </dependency> </dependencies>
- Gradle
def versions = [ ScalaBinary: "2.13" ] dependencies { implementation platform("org.apache.pekko:pekko-bom_${versions.ScalaBinary}:1.1.2") implementation "org.apache.pekko:pekko-actor_${versions.ScalaBinary}" }
After
org.apache.pekko.pattern.after
org.apache.pekko.pattern.Patterns.after
makes it easy to complete a Future
CompletionStage
with a value or exception after a timeout.
- Scala
-
source
val delayed = pekko.pattern.after(200.millis)(Future.failed(new IllegalStateException("OHNOES"))) val future = Future { Thread.sleep(1000); "foo" } val result = Future.firstCompletedOf(Seq(future, delayed))
- Java
-
source
CompletionStage<String> failWithException = CompletableFuture.supplyAsync( () -> { throw new IllegalStateException("OHNOES1"); }); CompletionStage<String> delayed = Patterns.after(Duration.ofMillis(200), system, () -> failWithException);
Retry
org.apache.pekko.pattern.retry
org.apache.pekko.pattern.Patterns.retry
will retry a Future
CompletionStage
some number of times with a delay between each attempt.
- Scala
-
source
import org.apache.pekko import pekko.actor.typed.scaladsl.adapter._ implicit val scheduler: pekko.actor.Scheduler = system.scheduler.toClassic implicit val ec: ExecutionContext = system.executionContext // Given some future that will succeed eventually @volatile var failCount = 0 def futureToAttempt() = { if (failCount < 5) { failCount += 1 Future.failed(new IllegalStateException(failCount.toString)) } else Future.successful(5) } // Return a new future that will retry up to 10 times val retried: Future[Int] = pekko.pattern.retry(() => futureToAttempt(), attempts = 10, 100 milliseconds)
- Java
-
source
Callable<CompletionStage<String>> attempt = () -> CompletableFuture.completedFuture("test"); CompletionStage<String> retriedFuture = Patterns.retry(attempt, 3, java.time.Duration.ofMillis(200), system); Callable<CompletionStage<String>> attempt = () -> CompletableFuture.completedFuture("test"); CompletionStage<String> retriedFuture = Patterns.retry( attempt, (notUsed, e) -> e != null, 3, java.time.Duration.ofMillis(200), system);
1.1.2