-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Duplicated Context in StartupEvent Observers #29466
Comments
/cc @cescoffier |
@mkouba I think we discussed it at some point, but I can't remember the outcome. |
#27623 and #27854 are related. I think that we should introduce some way (annotation?) to wrap the notification like this. |
@mkouba fancy to propose the feature? |
I'm still not sure how this should/could work. One of the questions is: do we want to execute the logic asynchronously, i.e. not necessarily during the startup sequence? Otherwise, we'd have to block and wait (and this should be fine in the context of |
That's what I would have done: call all the observers and join them before going further. Maybe instead of join, it might be easier to invoke them sequentially. |
I've got this feeling that you would need the observer method to return a |
Not really. This is mostly about executing the observer method on the event loop; not about asynchronous invocation or participating in a reactive stream. In other words, a user just needs to execute the observer method on the event loop but block the current thread (and wait for the result). void onStart(@Observes StartupEvent event, Vertx vertx, Mutiny.SessionFactory factory) {
Context context = VertxContext.getOrCreateDuplicatedContext(vertx);
VertxContextSafetyToggle.setContextSafe(context, true);
CompletableFuture<Void> fu = new CompletableFuture<>();
context.runOnContext(new Handler<Void>() {
@Override
public void handle(Void event) {
// do something with the session factory
fu.complete(null);
}
});
fu.get(); // wait for the result
} Could be replaced with something like: @NonBlocking
void onStart(@Observes StartupEvent event, Mutiny.SessionFactory factory) {
// do something with the session factory
} |
It actually can't be replaced the way you mention without significantly limiting what the observer method can do. In particular, it cannot start any asynchronous action, because there's no way to signal its completion. |
Why? You can just subscribe to the |
OTOH I agree that returning |
You can subscribe to the |
You're right. |
So it seems that the only reasonable thing we could do is:
public class VertxContexts {
public static void runOnVertxContext(Supplier<Uni<Void>> uniSupplier) {
Vertx vertx = lookupVertx();
Context context = VertxContext.getOrCreateDuplicatedContext(vertx);
VertxContextSafetyToggle.setContextSafe(context, true);
CompletableFuture<Void> fu = new CompletableFuture<>();
context.runOnContext(new Handler<Void>() {
@Override
public void handle(Void event) {
uniSupplier.get().subscribe().with(i -> fu.complete(null), e -> fu.completeExceptionally(e))
}
});
fu.get();
}
} and then: void onStart(@Observes StartupEvent event, Mutiny.SessionFactory factory) {
VertxContexts.runOnVertxContext(() -> {
// do something with the session factory and return Uni<Void>
});
} Am I missing something? |
I think the 2nd option is sensible in the short term. Longer term, I think we could add support for async observers returning |
Ok, I'll try to prepare something... |
- resolves quarkusio#29466
- resolves quarkusio#29466
- resolves quarkusio#29466
- resolves quarkusio#29466
- resolves quarkusio#29466
- resolves quarkusio#29466
- resolves quarkusio#29466 Co-authored-by: Ladislav Thon <ladicek@gmail.com>
- resolves quarkusio#29466 Co-authored-by: Ladislav Thon <ladicek@gmail.com>
Description
In #28017 the scheduler was changed so that scheduled methods run in the context of a duplicated context. Now I have encountered similar problems in the context of a
StartupEvent
observer. I am thus wondering whether it would again make sense to change Quarkus, so that a duplicated context (and thus context locals) are available.Implementation ideas
No response
The text was updated successfully, but these errors were encountered: