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 aContextAwareRunnableinstance from the givenRunnabletask and the context from the currentContextAwareinstance.<V> ContextAwareCallable<V> Builds aContextAwareCallableinstance from the givenCallabletask and the context from the currentContextAwareinstance.static ContextAwareof()Creates aContextAwareinstance for building a context-aware task.static ContextAwareCreates aContextAwareinstance for building a context-aware task.static ContextAwareCreates aContextAwareinstance for building a context-aware task.static ContextAwarestatic ContextAwarewithBaseUrl(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 aContextAwareinstance for building a context-aware task.- Returns:
- context-aware task builder
-
of
Creates aContextAwareinstance for building a context-aware task.- Parameters:
tenantId- the tenant identifier- Returns:
- context-aware task builder
-
of
Creates aContextAwareinstance 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 aContextAwareCallableinstance from the givenCallabletask and the context from the currentContextAwareinstance.- Type Parameters:
V-- Parameters:
callable- task- Returns:
- context-aware callable task
-
build
Builds aContextAwareRunnableinstance from the givenRunnabletask and the context from the currentContextAwareinstance.- Parameters:
runnable-- Returns:
- context-aware runnable task
-