0

I have a Quarkus app with REST API and multiple services that are @ApplicationScoped, and one bean that is @RequestScoped (it has data related to JWT, cookies etc).

When the REST API is invoked, I want to do some work synchronously and then return an answer to the client but to keep doing async work with the same RequestScoped bean (context data).

I tried to propagate the RequestScoped bean to the thread that will handle the async task, but once the HTTP request is finished, the RequestScoped bean is getting deleted.

What is the correct way of doing this?
Basically I want to share some context across a flow that involves sync and async tasks

Bentz
  • 103
  • 7
  • 1
    I'd suggest to copy the necessary data from the request-scoped bean into your own object and pass that to the async task. Request-scoped beans are generally not required to be thread-safe, so accessing them from multiple threads may be risky. – Ladicek Feb 08 '23 at 16:13
  • @Ladicek I can pass a copy of the data but then i have to pass down the data across multiple methods, currently inside of the methods we are pulling the data from that RequestScoped bean. I want to avoid passing down the data across the methods. I'm looking for a way to inject the data into a new "context" (a new instance of that RequestScoped bean). – Bentz Feb 08 '23 at 16:44
  • You can disable context propagation, manually activate the request context in the async task using `@ActivateRequestContext` or `RequestContextController`, lookup the bean and set its content. – Ladicek Feb 08 '23 at 20:03

0 Answers0