Class ContextAware

java.lang.Object
com.axelor.concurrent.ContextAware

public final class ContextAware extends Object
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 Details

    • tenantId

      protected String tenantId
    • user

      protected User user
    • baseUrl

      protected String baseUrl
    • language

      protected Locale language
    • withTransaction

      protected boolean withTransaction
  • Method Details

    • of

      public static ContextAware of()
      Creates a ContextAware instance for building a context-aware task.
      Returns:
      context-aware task builder
    • of

      public static ContextAware of(String tenantId)
      Creates a ContextAware instance for building a context-aware task.
      Parameters:
      tenantId - the tenant identifier
      Returns:
      context-aware task builder
    • of

      public static ContextAware of(String tenantId, User user)
      Creates a ContextAware instance for building a context-aware task.
      Parameters:
      tenantId - the tenant identifier
      user - the user associated with the context
      Returns:
      context-aware task builder
    • of

      public static ContextAware of(String tenantId, User user, String baseUrl, Locale language)
    • of

      public static ContextAware of(String tenantId, User user, String baseUrl, Locale language, boolean withTransaction)
    • withTenantId

      public ContextAware withTenantId(String tenantId)
      Sets the tenant identifier for the context-aware task.
      Parameters:
      tenantId -
      Returns:
      context-aware task builder
    • withUser

      public ContextAware withUser(User user)
      Sets the user for the context-aware task.
      Parameters:
      user -
      Returns:
      context-aware task builder
    • withBaseUrl

      public ContextAware withBaseUrl(String baseUrl)
      Sets the base URL for the context-aware task.
      Parameters:
      baseUrl - the base URL for the context
      Returns:
      context-aware task builder
    • withLanguage

      public ContextAware withLanguage(Locale language)
      Sets the language preference for the context-aware task.
      Parameters:
      language - the locale representing the language preference
      Returns:
      context-aware task builder
    • withTransaction

      public ContextAware withTransaction(boolean withTransaction)
      Sets the transactional flag for the context-aware task.
      Parameters:
      withTransaction - whether to start a transaction
      Returns:
      context-aware task builder
    • build

      public <V> ContextAwareCallable<V> build(Callable<V> callable)
      Builds a ContextAwareCallable instance from the given Callable task and the context from the current ContextAware instance.
      Type Parameters:
      V -
      Parameters:
      callable - task
      Returns:
      context-aware callable task
    • build

      public ContextAwareRunnable build(Runnable runnable)
      Builds a ContextAwareRunnable instance from the given Runnable task and the context from the current ContextAware instance.
      Parameters:
      runnable -
      Returns:
      context-aware runnable task