Package com.axelor.concurrent
Class ContextAware
java.lang.Object
com.axelor.concurrent.ContextAware
A utility class for creating context-aware wrappers around
Runnable
and Callable
tasks. This class provides convenience methods to encapsulate tasks in a context-aware execution
environment, which propagates contextual information such as tenant and user, base URL, and
language.
// Capture current thread context and run in background
executor.submit(
ContextAware.of()
.withTransaction(true)
.tenantId("tenant42")
.language(Locale.FRENCH)
.build(() -> service.processRequest())
);
// Callable example
Future<User> future = executor.submit(
ContextAware.of()
.withTransaction(false)
.tenantId("tenant42")
.build(() -> userService.findById(123))
);
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionBuilds aContextAwareRunnable
instance from the givenRunnable
task and the context from the currentContextAware
instance.<V> ContextAwareCallable
<V> Builds aContextAwareCallable
instance from the givenCallable
task and the context from the currentContextAware
instance.static ContextAware
of()
Creates aContextAware
instance for building a context-aware task.static ContextAware
Creates aContextAware
instance for building a context-aware task.static ContextAware
Creates aContextAware
instance for building a context-aware task.static ContextAware
static ContextAware
withBaseUrl
(String baseUrl) Sets the base URL for the context-aware task.withLanguage
(Locale language) Sets the language preference for the context-aware task.withTenantId
(String tenantId) Sets the tenant identifier for the context-aware task.withTransaction
(boolean withTransaction) Sets the transactional flag for the context-aware task.Sets the user for the context-aware task.
-
Field Details
-
tenantId
-
user
-
baseUrl
-
language
-
withTransaction
protected boolean withTransaction
-
-
Method Details
-
of
Creates aContextAware
instance for building a context-aware task.- Returns:
- context-aware task builder
-
of
Creates aContextAware
instance for building a context-aware task.- Parameters:
tenantId
- the tenant identifier- Returns:
- context-aware task builder
-
of
Creates aContextAware
instance for building a context-aware task.- Parameters:
tenantId
- the tenant identifieruser
- the user associated with the context- Returns:
- context-aware task builder
-
of
-
of
public static ContextAware of(String tenantId, User user, String baseUrl, Locale language, boolean withTransaction) -
withTenantId
Sets the tenant identifier for the context-aware task.- Parameters:
tenantId
-- Returns:
- context-aware task builder
-
withUser
Sets the user for the context-aware task.- Parameters:
user
-- Returns:
- context-aware task builder
-
withBaseUrl
Sets the base URL for the context-aware task.- Parameters:
baseUrl
- the base URL for the context- Returns:
- context-aware task builder
-
withLanguage
Sets the language preference for the context-aware task.- Parameters:
language
- the locale representing the language preference- Returns:
- context-aware task builder
-
withTransaction
Sets the transactional flag for the context-aware task.- Parameters:
withTransaction
- whether to start a transaction- Returns:
- context-aware task builder
-
build
Builds aContextAwareCallable
instance from the givenCallable
task and the context from the currentContextAware
instance.- Type Parameters:
V
-- Parameters:
callable
- task- Returns:
- context-aware callable task
-
build
Builds aContextAwareRunnable
instance from the givenRunnable
task and the context from the currentContextAware
instance.- Parameters:
runnable
-- Returns:
- context-aware runnable task
-