问题描述
上周我对 Mutiny 库进行了第一次 ?✨? 回合,因为我的 Reactive 项目中需要一个 Quarkus REST 端点。这并不像听起来那么明显,所以我想我会分享我对 Mutiny 中的 Quarkus 库的新见解;
Quarkus documentation 将 Mutiny 库指定为 Reactive 用例的首选库;
例如;
突出的是大多数 Mutiny 示例都使用新字符串作为示例。 所以,我的问题仍然存在;
如何将 MyRequestService 与 Quarkus 中的 Mutiny 连接起来
这将是这样的:
Uni<MyRequestService> lMyRequestServiceUni = Uni.createFrom().item( ... ) ...
MyRequestService 已经使用了回调结构,所以我尝试了一个针对 Mutiny 的回调方法。
解决方法
Mutiny 在与 Emitter 集成时使用 callback-based APIs;
https://smallrye.io/smallrye-mutiny/getting-started/creating-unis
因此 MyRequestService 或底层回调对象需要实现 UniEmitter Consumer。
但首先,要成为 Reactive,我的旧阻塞 REST 端点需要返回 Uni
ServiceResource 只是将调用转发给 Service。
MyRequestService 创建一个 MyJsonResultConsumer 并将其传递给 Mutiny Emitter。生成的 Uni
最后,MyJsonResultConsumer 是实际的回调对象;方法 ready() 在 UniEmitter 上调用 complete() 结束对 Mutiny 的回调。
请记住,Mutiny 需要为 UniEmitter 提供对 accept() 的调用(返回),因此您应该检查空指针(或使用 Semaphore oid)。 ??♂️