-
Hello, I'm looking for a way to implicitly propagate a So far, I only succeed when I explicitly add the Context as a parameter to all the mappers, suppliers, etc. I've read that the context is set upon subscription, otherwise the context will be empty. I can testify it is :-) Shall I do so from the mappers called by chain method? Here is some pseudo-code to try to illustrate what I would like to achieve. public Response post(MyRequest request) {
return Uni.createFrom().context(ctx -> {
ctx.put("name", securityIdentity.getPrincipal().getName());
ctx.put("request", request);
...
return Uni.createFrom().item(request)
.invoke(() -> log.info(ctx.get("name"))) // Obviously, it works
...
.chain(a -> doStep1(a)) // Hoping for the context to be passed implicitly
...
.chain(b -> Uni.createFrom().item(b)
...
.chain(c -> doStepX(c)) // Also here
...
.eventually(() -> doNotify())) // Here too would be super
...
.map(z -> {
return Response.entity(z).build();
})
.onFailure(WebApplicationException.class).recoverWithItem(w -> ((WebApplicationException) w).getResponse());
}).await().indefinitely();
}
private Uni<SomeEntity> doStep1(A a) {
return Uni.createFrom().context(ctx-> { // <-- Here the context is empty!
return doCheck(a)
.chain(m -> Uni.createFrom().item(() -> QuarkusTransaction.requiringNew().call(() -> {
SomeEntity entity = mapper.toEntity(m);
entity.setName(ctx.get("name"));
entity.persist();
return entity;
})));
});
}
... // And so on for other steps Many thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
In Quarkus, depending on what you do in the "chain", it should work OOTB using either a RequestScope bean (used as container) or |
Beta Was this translation helpful? Give feedback.
-
Thanks for you answers! In the chain, I'm doing various operations (Panache, SSH, File,...), and I have to make sure I do not hold some database resources since the file operations takes some time to complete. So I have to take care of detached and merged JPA entities. With |
Beta Was this translation helpful? Give feedback.
Contexts are not created at the start of the chain but by the entity that performs the initial subscription.
1st snippet: you need to pass the context at subscription time using
.awaitUsing(context)
.2nd snippet: you won't have a context, I don't recall Quarkus providing one for reactive methods.