Operator docs for Source.tick (#28225)

This commit is contained in:
Johan Andrén 2019-11-25 13:25:07 +01:00 committed by GitHub
parent 5e2435a424
commit ac15cb3289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 177 additions and 3 deletions

View file

@ -0,0 +1,76 @@
/*
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package jdocs.stream.operators.source;
import akka.NotUsed;
import akka.actor.Cancellable;
import akka.actor.typed.ActorRef;
import akka.actor.typed.ActorSystem;
import akka.actor.typed.javadsl.AskPattern;
import java.util.concurrent.CompletionStage;
import java.time.Duration;
import akka.japi.Pair;
import akka.stream.javadsl.Flow;
import akka.stream.javadsl.Source;
public class Tick {
// not really a runnable example, these are just pretend
private ActorSystem<Void> system = null;
private ActorRef<MyActor.Command> myActor = null;
static class MyActor {
interface Command {}
static class Query implements Command {
public final ActorRef<Response> replyTo;
public Query(ActorRef<Response> replyTo) {
this.replyTo = replyTo;
}
}
static class Response {
public final String text;
public Response(String text) {
this.text = text;
}
}
}
void simple() {
// #simple
Source.tick(
Duration.ofSeconds(1), // delay of first tick
Duration.ofSeconds(1), // delay of subsequent ticks
"tick" // element emitted each tick
)
.runForeach(System.out::println, system);
// #simple
}
void pollSomething() {
// #poll-actor
Source<String, Cancellable> periodicActorResponse =
Source.tick(Duration.ofSeconds(1), Duration.ofSeconds(1), "tick")
.mapAsync(
1,
notUsed -> {
CompletionStage<MyActor.Response> response =
AskPattern.ask(
myActor, MyActor.Query::new, Duration.ofSeconds(3), system.scheduler());
return response;
})
.map(response -> response.text);
// #poll-actor
// #zip-latest
Flow<Integer, Pair<Integer, String>, NotUsed> zipWithLatestResponse =
Flow.of(Integer.class).zipLatest(periodicActorResponse);
// #zip-latest
}
}