Class SessionPooledExecutor

java.lang.Object
org.tentackle.session.SessionPooledExecutor

public class SessionPooledExecutor extends Object
Executes runnables on a pool of sessions.
Useful to run background tasks in parallel, each task with its own session.
  • Constructor Details

    • SessionPooledExecutor

      public SessionPooledExecutor(SessionPool sessionPool)
      Creates the executor service for a session pool.
      Parameters:
      sessionPool - the session pool
  • Method Details

    • getSessionPool

      public SessionPool getSessionPool()
      Gets the session pool.
      Returns:
      the pool
    • getExecutorService

      public ExecutorService getExecutorService()
      Gets the executor service.
      Returns:
      the executor service
    • shutdown

      public void shutdown()
      Shuts down the executor service and the pool.
    • submit

      public <V> void submit(Supplier<V> task, Consumer<V> successHandler, Consumer<RuntimeException> failHandler)
      Submits a task for execution in background by a thread pool.
      Type Parameters:
      V - the type returned by the task
      Parameters:
      task - the task to execute
      successHandler - the handler to be invoked when the task finished successfully, null if none
      failHandler - the handler to be invoked when the task failed with an exception, null if none
    • submit

      public <V> void submit(Collection<Supplier<V>> tasks, long timeoutMillis, Consumer<Map<Supplier<V>,V>> successHandler, Consumer<Map<Supplier<V>,RuntimeException>> failHandler)
      Submits a collection of tasks for background execution in multiple threads of a thread pool and waits for their termination.
      If there are failures, the fail handler (if any) will be invoked before the success handler.
      Type Parameters:
      V - the type returned by the runners
      Parameters:
      tasks - the tasks to execute
      timeoutMillis - the timeout in milliseconds, ≤ 0 if wait infinitely
      successHandler - updater invoked when all runners finished, null if none
      failHandler - updater invoked only if execution of some runner failed, null if log only as error
    • createExecutorService

      protected ExecutorService createExecutorService()
      Creates the executor service.
      For unlimited session pools the thread pool will also be unlimited and the number of threads will be adjusted automatically, i.e. new threads are created when necessary (tasks are not queued) and unused threads are removed when no more used for a certain amount of time.
      For limited session pools a fixed size thread pool is used and tasks queued, if all threads are in use.
      Returns:
      the executor service
    • createThread

      protected Thread createThread(Runnable runnable)
      Creates a new thread.
      Parameters:
      runnable - the runnable to run
      Returns:
      the thread