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)); // JDKawait, 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
Runs block and blocks the calling thread until it completes.
Like await, with coroutineContext added to the call.
Like forEach, with coroutineContext added to the collection.