quarkus @ObservesAsync 调用 Uni

发布于 2025-01-15 15:31:40 字数 156 浏览 3 评论 0原文

在异步观察者中调用 Uni 的最佳方式是什么?如果我能回到大学那就太好了,但不幸的是这是行不通的。

void observe(@ObservesAsync MyEvent event) {
    Uni<Object> task;
}

What's the best way to invoke Uni in an async observer? It would be great if I could just return Uni but unfortunately that doesn't work.

void observe(@ObservesAsync MyEvent event) {
    Uni<Object> task;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人 2025-01-22 15:31:40

正如 @ladicek 提到的,您可以:

  1. 使用同步观察者并阻止直到终止
  2. 使用同步观察者并使用“即发即弃”方法“触发”异步操作
  3. 使用异步观察者(虽然它不是严格异步的,但它只是运行在另一个线程上)并产生一个CompletionStage

1)同步观察者并阻塞直到终止

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.await().indefinitely();
}

2)同步观察者并使用即发即弃方法触发异步操作

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.subscribeAsCompletionStage(); // Fire and forget, no error reporting
}

或者:

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.subscribe().with(success -> ..., failure -> log(failure));
}

3)异步观察者并产生一个CompletionStage

CompletionStage<Void> observe(@ObservesAsync MyEvent event) {
    Uni<Void> uni = ...;
    return uni.subscribeAsCompletionStage();
}

As mentioned by @ladicek, you can:

  1. use a synchronous observer and block until termination
  2. use a synchronous observer and "trigger" the async operation using a fire-and-forget approach
  3. use an async observer (while it's not strictly async, it just runs on another thread) and produce a CompletionStage

1) Synchronous observer and block until termination

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.await().indefinitely();
}

2) Synchronous observer and trigger the async operation using a fire-and-forget approach

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.subscribeAsCompletionStage(); // Fire and forget, no error reporting
}

Or:

void observe(@Observes MyEvent event) {
    Uni<Void> uni = ...;
    uni.subscribe().with(success -> ..., failure -> log(failure));
}

3) Async observer and produce a CompletionStage

CompletionStage<Void> observe(@ObservesAsync MyEvent event) {
    Uni<Void> uni = ...;
    return uni.subscribeAsCompletionStage();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文