Class ContextualExecutors

java.lang.Object
org.jboss.resteasy.concurrent.ContextualExecutors

public class ContextualExecutors extends Object
A utility to create and/or wrap executors in a contextual executor.
Since:
5.0.0
Author:
James R. Perkins
See Also:
  • Constructor Details

    • ContextualExecutors

      public ContextualExecutors()
  • Method Details

    • executor

      public static Executor executor()
      An executor which executes tasks in the current thread
      Returns:
      a new contextual executor
    • threadPool

      public static ContextualExecutorService threadPool()
      Creates a new ContextualExecutorService or wraps the default ManagedExecutorService in a Jakarta EE environment.

      If executed in a Jakarta EE container which includes a default ManagedExecutorService, that executor is wrapped an said to be managed. If the default executor service cannot be found or if not being executed in a Jakarta EE container a new cached thread pool will be wrapped.

      In a Jakarta EE container the JNDI lookup name can be overridden with the resteasy.async.executor.service.jndi configuration property. By default the JNDI lookup name is java:comp/DefaultManagedExecutorService.

      Returns:
      a new contextual executor
    • scheduledThreadPool

      public static ContextualScheduledExecutorService scheduledThreadPool()
      Creates a new ContextualScheduledExecutorService or wraps the default ManagedScheduledExecutorService in a Jakarta EE environment.

      If executed in a Jakarta EE container which includes a default ManagedScheduledExecutorService, that executor is wrapped an said to be managed. If the default executor service cannot be found or if not being executed in a Jakarta EE container a new scheduled thread pool will be wrapped. The size of the thread pool is retrieved via the resteasy.async.timeout.scheduler.min.pool.size context parameter. If not found 1 is the default. The thread pool size is ignored in Jakarta EE environments.

      In a Jakarta EE container the JNDI lookup name can be overridden with the resteasy.async.scheduled.executor.service.jndi configuration property. By default the JNDI lookup name is java:comp/DefaultManagedScheduledExecutorService.

      Returns:
      a new contextual executor
    • scheduledThreadPool

      public static ContextualScheduledExecutorService scheduledThreadPool(int poolSize, ThreadFactory threadFactory)
      Creates a new ContextualScheduledExecutorService or wraps the default ManagedScheduledExecutorService in a Jakarta EE environment.

      If executed in a Jakarta EE container which includes a default ManagedScheduledExecutorService, that executor is wrapped an said to be managed. If the default executor service cannot be found or if not being executed in a Jakarta EE container a new scheduled thread pool will be wrapped.

      In a Jakarta EE container the JNDI lookup name can be overridden with the resteasy.async.scheduled.executor.service.jndi configuration property. By default the JNDI lookup name is java:comp/DefaultManagedScheduledExecutorService.

      Parameters:
      poolSize - the size of the pool to create, ignored in a Jakarta EE container
      threadFactory - the thread factory to use if a new executor is created, ignored in a Jakarta EE container
      Returns:
      a new contextual executor
    • wrap

      public static ContextualExecutorService wrap(ExecutorService delegate)
      Wraps the executor service in contextual executor and is said to be managed. You are responsible for the lifecycle of the delegate.

      The context is copied in before each invocation of the delegate, then reset after the thread is done executing.

      Parameters:
      delegate - the executor to wrap
      Returns:
      a new contextual executor
    • wrap

      public static ContextualExecutorService wrap(ExecutorService delegate, boolean managed)
      Wraps the executor service in contextual executor and is said to be managed. You are responsible for the lifecycle of the delegate.

      The context is copied in before each invocation of the delegate, then reset after the thread is done executing.

      Parameters:
      delegate - the executor to wrap
      managed - true if the lifecycle is managed by another process
      Returns:
      a new contextual executor
    • wrap

      Wraps the executor service in contextual executor and is said to be managed. You are responsible for the lifecycle of the delegate.

      The context is copied in before each invocation of the delegate, then reset after the thread is done executing.

      Parameters:
      delegate - the executor to wrap
      Returns:
      a new contextual executor
    • wrap

      public static ContextualScheduledExecutorService wrap(ScheduledExecutorService delegate, boolean managed)
      Wraps the executor service in contextual executor and is said to be managed. You are responsible for the lifecycle of the delegate.

      The context is copied in before each invocation of the delegate, then reset after the thread is done executing.

      Parameters:
      delegate - the executor to wrap
      managed - true if the lifecycle is managed by another process
      Returns:
      a new contextual executor
    • wrapOrLookup

      public static ContextualScheduledExecutorService wrapOrLookup(ScheduledExecutorService delegate)
      If the delegate is not null, it is wrapped. If the delegate is null and this is a Jakarta EE environment an attempt will be made to look up the default ManagedScheduledExecutorService. If found the default ManagedScheduledExecutorService will be wrapped. Otherwise, null will be returned.
      Parameters:
      delegate - the delegate to wrap, which may be null
      Returns:
      a wrapped contextual executor or null if no executor was provided or could be found
      See Also:
    • runnable

      public static Runnable runnable(Runnable task)
      Creates a new runnable which sets up the current context before the runnable is executed. Finally, resetting the context.
      Parameters:
      task - the runnable to wrap
      Returns:
      a new contextual runnable
    • callable

      public static <V> Callable<V> callable(Callable<V> task)
      Creates a new callable which sets up the current context before the callable is executed. Finally, resetting the context.
      Type Parameters:
      V - the return type
      Parameters:
      task - the callable to wrap
      Returns:
      a new contextual callable
    • callable

      public static <T> Collection<? extends Callable<T>> callable(Collection<? extends Callable<T>> tasks)
      Creates a new collection of callables which sets up the current context before each callable is executed. Finally, resetting the context.
      Type Parameters:
      T - the return type of the callable
      Parameters:
      tasks - the callables to wrap
      Returns:
      a collection of new contextual callables
      See Also: