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)); // 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.
函数
“Exposes” 的意思是“公开”或“使可见”。例如,某个功能或数据被设计为可以被其他系统或用户访问和使用。 flow 作为……;由于…… 发布者(Publisher)元素必须是非空值(non-null);Reactive Streams 不允许使用空值(null)。对于单个值,可以使用相应的处理方式。 async 和 Mono.fromFuture(...) 相反。
遍历每个元素 发布者(publisher) 给……;发送到…… action(表示某操作会一直等待,直到某个条件满足为止):“阻塞状态将持续到……” 发布者(publisher) 完成(相关任务或操作)。
喜欢 forEach,与……一起 coroutineContext 已添加到收藏列表中。
暴露一个固定的(资源/值等)。 items 将序列视为…… 发布者(Publisher) -- 这是一种适用于同步模型或代理的、符合 Java 语言特性的实现方式(通过继承相关类来实现同步功能)。 BasePublisher* (base)用于返回已经计算好的值。这些元素不能为 null;Reactive Streams 规定不允许使用 null 值。