使用 Quarkus/Mutiny 通过回调方法将 MyRequestService 连接到 Reactive REST GET 端点

问题描述

上周我对 Mutiny 库进行了第一次 ?✨? 回合,因为我的 Reactive 项目中需要一个 Quarkus REST 端点。这并不像听起来那么明显,所以我想我会分享我对 Mutiny 中的 Quarkus 库的新见解;

Quarkus documentation 将 Mutiny 库指定为 Reactive 用例的首选库;

Going forward,Mutiny will be the preferred library within Quarkus for all things reactive.

例如;

enter image description here

突出的是大多数 Mutiny 示例都使用新字符串作为示例。 所以,我的问题仍然存在;

如何将 MyRequestService 与 Quarkus 中的 Mutiny 连接起来

这将是这样的:

Uni<MyRequestService> lMyRequestServiceUni = Uni.createFrom().item( ... ) ...

MyRequestService 已经使用了回调结构,所以我尝试了一个针对 Mutiny 的回调方法

解决方法

Mutiny 在与 Emitter 集成时使用 callback-based APIs;

You can create a Uni using an emitter. This approach is useful when integrating callback-based APIs

https://smallrye.io/smallrye-mutiny/getting-started/creating-unis

因此 MyRequestService 或底层回调对象需要实现 UniEmitter Consumer。 但首先,要成为 Reactive,我的旧阻塞 REST 端点需要返回 Uni 而不是 MyJsonResult;

Uni instead of MyJsonResult

ServiceResource 只是将调用转发给 Service。

MyRequestService creates a MyJsonResultConsumer and delivers this to the Mutiny emitter. The resulting lMyJsonResultUni is returned to the ServiceResource

MyRequestService 创建一个 MyJsonResultConsumer 并将其传递给 Mutiny Emitter。生成的 Uni 返回给 ServiceResource。

Finally MyJsonResultConsumer is the actual callback object; method ready() calls complete() on the UniEmitter concluding the callback towards Mutiny

最后,MyJsonResultConsumer 是实际的回调对象;方法 ready() 在 UniEmitter 上调用 complete() 结束对 Mutiny 的回调。

请记住,Mutiny 需要为 UniEmitter 提供对 accept() 的调用(返回),因此您应该检查空指针(或使用 Semaphore oid)。 ??‍♂️

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...