微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决使用 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)。 ??‍♂️

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?