AsyncJava辅助工具(Helpers)

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.

函数

Link copied to clipboard

“Adapts” 的意思是“适应”或“调整”,表示某物能够根据环境的变化或需求进行相应的调整或改变。 发布者(publisher) 到…… Flow用于将 Rx 或 Reactor 源数据输入到 ADK 中。

Link copied to clipboard

“Exposes” 的意思是“公开”或“使可见”。例如,某个功能或数据被设计为可以被其他系统或用户访问和使用。 flow 作为……;由于…… 发布者(Publisher)元素必须是非空值(non-null);Reactive Streams 不允许使用空值(null)。对于单个值,可以使用相应的处理方式。 async 和 Mono.fromFuture(...) 相反。

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

运行 block 在……的时候;当……发生时 `scope` 并返回一个表示未来某个时间点的值(即“future”)。除非另有说明,否则会使用调用者的 OpenTelemetry 上下文信息。 `scope` 每个协程(coroutine)都独立运行,拥有自己的执行流程。取消某个协程会同时终止该协程的运行;而取消整个程序(或系统)则会导致所有正在运行的协程都被终止。 `scope` 取消已经在进行中的任务(或工作)。

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

运行 block 该操作会阻塞调用该函数的线程,直到该函数执行完毕为止。

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

收集所有元素…… flow在此期间,相关操作会被阻塞,直到该操作完成为止。

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

遍历每个元素 flow 给……;发送到…… action 货物一到达,就立即进行拦截(或阻止相关操作),直到……为止。 flow 完成(相关任务或操作)。

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

遍历每个元素 发布者(publisher) 给……;发送到…… action(表示某操作会一直等待,直到某个条件满足为止):“阻塞状态将持续到……” 发布者(publisher) 完成(相关任务或操作)。

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

喜欢 forEach,与……一起 coroutineContext 已添加到收藏列表中。

Link copied to clipboard

暴露一个固定的(资源/值等)。 items 将序列视为…… 发布者(Publisher) -- 这是一种适用于同步模型或代理的、符合 Java 语言特性的实现方式(通过继承相关类来实现同步功能)。 BasePublisher* (base)用于返回已经计算好的值。这些元素不能为 null;Reactive Streams 规定不允许使用 null 值。