java.lang.Object
org.tentackle.session.SessionPooledExecutor
Executes runnables on a pool of sessions.
Useful to run background tasks in parallel, each task with its own session.
Useful to run background tasks in parallel, each task with its own session.
-
Constructor Summary
ConstructorsConstructorDescriptionSessionPooledExecutor(SessionPool sessionPool) Creates the executor service for a session pool. -
Method Summary
Modifier and TypeMethodDescriptionprotected ExecutorServiceCreates 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.protected ThreadcreateThread(Runnable runnable) Creates a new thread.protected ThreadGroupCreates the thread group of the threads in the pool.Gets the executor service.Gets the session pool.Gets the thread group for the thread pool.intGets the size of the thread pool.
The default implementation returns the maximum size of the session pool.
If the session pool is unlimited, the thread pool will be unlimited too.
Override this method, if sizes differ.
Caution: using an unlimited or larger thread pool with a limited or smaller session pool will cause an exception when all sessions are in use!voidshutdown()Shuts down the executor service and the pool.<V> voidsubmit(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.<V> voidsubmit(Supplier<V> task, Consumer<V> successHandler, Consumer<RuntimeException> failHandler) Submits a task for execution in background by a thread pool.
-
Constructor Details
-
SessionPooledExecutor
Creates the executor service for a session pool.- Parameters:
sessionPool- the session pool
-
-
Method Details
-
getSessionPool
Gets the session pool.- Returns:
- the pool
-
getExecutorService
Gets the executor service.- Returns:
- the executor service
-
getThreadGroup
Gets the thread group for the thread pool.- Returns:
- the thread group
-
getThreadPoolSize
public int getThreadPoolSize()Gets the size of the thread pool.
The default implementation returns the maximum size of the session pool.
If the session pool is unlimited, the thread pool will be unlimited too.
Override this method, if sizes differ.
Caution: using an unlimited or larger thread pool with a limited or smaller session pool will cause an exception when all sessions are in use!- Returns:
- the number of threads or unlimited dynamic size if ≤ 0
- See Also:
-
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 executesuccessHandler- the handler to be invoked when the task finished successfully, null if nonefailHandler- 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 executetimeoutMillis- the timeout in milliseconds, ≤ 0 if wait infinitelysuccessHandler- updater invoked when all runners finished, null if nonefailHandler- updater invoked only if execution of some runner failed, null if log only as error
-
createThreadGroup
Creates the thread group of the threads in the pool.- Returns:
- the thread group
-
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
- See Also:
-
createThread
Creates a new thread.- Parameters:
runnable- the runnable to run- Returns:
- the thread
-