AsyncJavaHelpers

Entry points for calling the suspending and Flow-returning parts of the ADK from Java, and for bridging a Flow to Reactive Streams.

A suspend function compiles to a method with a trailing Continuation parameter, so Java passes the call as a lambda that forwards it. Kotlin default arguments are not visible to Java, so every parameter must be supplied:

Session session = AsyncJavaHelpers.await(c -> sessionService.createSession(key, null, c));
List<Event> events =
AsyncJavaHelpers.collect(runner.runAsync(user, id, null, message, null, null));

A Flow also converts to a Reactive Streams Publisher, the common currency of RxJava, Project Reactor, and java.util.concurrent.Flow; the final hop is one line in caller code:

Flowable<Event> rx   = Flowable.fromPublisher(AsyncJavaHelpers.asPublisher(flow)); // RxJava 3
Flux<Event> flux = Flux.from(AsyncJavaHelpers.asPublisher(flow)); // Reactor
Flow.Publisher<Event> jdk =
FlowAdapters.toFlowPublisher(AsyncJavaHelpers.asPublisher(flow)); // JDK

await, async, collect, and forEach run in the caller's OpenTelemetry context (for async, unless its scope carries one). await, collect, and forEach over a Flow also take an optional coroutineContext (such as MDCContext() from kotlinx-coroutines-slf4j to carry the caller's MDC), ignoring any Job in it. For async, add such elements to its scope.

Kotlin callers should use the suspending APIs directly instead.

Functions

Link copied to clipboard

Adapts publisher to a Flow, for feeding an Rx or Reactor source into the ADK.

Link copied to clipboard

Exposes flow as a Publisher. Elements must be non-null; Reactive Streams forbids null. For a single value, use async and Mono.fromFuture(...) instead.

Link copied to clipboard
fun <T> async(scope: CoroutineScope, block: suspend () -> T): CompletableFuture<T>

Runs block on scope and returns a future. Uses the caller's OpenTelemetry context unless scope carries its own. Cancelling the future cancels the coroutine, and cancelling scope cancels work already in flight.

Link copied to clipboard
fun <T> await(block: suspend () -> T): T

Runs block and blocks the calling thread until it completes.

fun <T> await(coroutineContext: CoroutineContext, block: suspend () -> T): T

Like await, with coroutineContext added to the call.

Link copied to clipboard
fun <T> collect(flow: Flow<T>, coroutineContext: CoroutineContext = EmptyCoroutineContext): List<T>

Collects every element of flow, blocking until it completes.

Link copied to clipboard
fun <T> forEach(flow: Flow<T>, action: Consumer<T>)

Passes each element of flow to action as it arrives, blocking until flow completes.

fun <T : Any> forEach(publisher: Publisher<T>, action: Consumer<T>)

Passes each element of publisher to action, blocking until publisher completes.

fun <T> forEach(flow: Flow<T>, coroutineContext: CoroutineContext, action: Consumer<T>)

Like forEach, with coroutineContext added to the collection.

Link copied to clipboard

Exposes a fixed items sequence as a Publisher -- the Java-friendly way for a synchronous model or agent (extending a BasePublisher* base) to return already-computed values. Elements must be non-null; Reactive Streams forbids null.