Hi,
I’m a bit confused about how thread handling works for background tasks in Jmix (version 2.4.3).
I have a background task that can take a couple of seconds to complete. In its done()
method, I navigate to a different view. My expectation (based on the documentation) is that done()
should always be called on the UI thread. However, I’ve noticed that sometimes done()
appears to be invoked on a background thread instead.
From my observations:
- If the task finishes almost immediately,
done()
seems to run on the UI thread. - If the task takes a few seconds,
done()
seems to be executed on a background thread.
When done()
runs on a background thread, RequestContextHolder.getRequestAttributes()
returns null
, and attempting to navigate causes Spring’s DefaultOAuth2AuthorizedClientManager
to throw an error.
My questions are:
- What determines which thread
done()
is invoked on? - Is there a recommended way to ensure that navigation and security context-dependent operations happen safely after a background task finishes? ( Right now I’m thinking of simply using Spring’s
AuthorizedClientServiceOAuth2AuthorizedClientManager
when getRequestAttributes are null. )
Thanks in advance for any clarification!