Class LoggingScheduledThreadPoolExecutor
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- java.util.concurrent.ScheduledThreadPoolExecutor
-
- de.aristaflow.adept2.util.threading.LoggingScheduledThreadPoolExecutor
-
- All Implemented Interfaces:
Executor
,ExecutorService
,ScheduledExecutorService
public class LoggingScheduledThreadPoolExecutor extends ScheduledThreadPoolExecutor
This class creates logging futures when scheduling. These log their outcome (results and exceptions) if no one retrieved it. This is especially useful when scheduling work for polling where no one checks the result and therefore also the exceptions are ignored.- Author:
- Ulrich Kreher
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
continueRegularAfterException
Whether regular executions should continue even in case of exceptions.protected Logger
logger
The logger for logging unhandled results, Exception, RuntimeException or Error.
-
Constructor Summary
Constructors Constructor Description LoggingScheduledThreadPoolExecutor(int corePoolSize, Adept2ThreadFactory threadFactory, boolean continueRegularAfterException)
Creates a newLoggingScheduledThreadPoolExecutor
with the designated core pool size and thread factory also providing the logger.LoggingScheduledThreadPoolExecutor(int corePoolSize, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, boolean continueRegularAfterException)
Creates a newLoggingScheduledThreadPoolExecutor
with the designated core pool size, rejected execution handler and thread factory also providing the logger.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
afterExecute(Runnable r, Throwable t)
protected void
beforeExecute(Thread t, Runnable r)
protected <V> RunnableScheduledFuture<V>
decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
protected <V> RunnableScheduledFuture<V>
decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
void
execute(Runnable command, boolean logException)
CallsScheduledThreadPoolExecutor.execute(Runnable)
and optionally wraps the runnable to catch and log an occurring runtime exception and virtual machine error.ScheduledFuture<?>
schedule(Runnable command, long delay, TimeUnit unit, boolean logException, boolean rethrowException)
CallsScheduledThreadPoolExecutor.schedule(Runnable, long, TimeUnit)
and optionally wraps the runnable to log an occurring runtime exception and virtual machine error.<V> ScheduledFuture<V>
schedule(Callable<V> callable, long delay, TimeUnit unit, boolean logException)
CallsScheduledThreadPoolExecutor.schedule(Callable, long, TimeUnit)
and optionally wraps the callable to catch and log an occurred exception.ScheduledFuture<?>
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
ScheduledFuture<?>
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
Future<?>
submit(Runnable task, boolean logException, boolean rethrowException)
CallsScheduledThreadPoolExecutor.submit(Runnable)
and optionally wraps the runnable to log an occurring exception.<T> Future<T>
submit(Runnable task, T result, boolean logException)
CallsScheduledThreadPoolExecutor.submit(Runnable, Object)
and optionally wraps the runnable to log an occurring exception.<T> Future<T>
submit(Callable<T> task, boolean logException)
CallsScheduledThreadPoolExecutor.submit(Callable)
and optionally wraps the callable to catch and log an occurred exception.protected <V> Callable<V>
wrapCallable(Callable<V> command, boolean loggingRequested, boolean rethrow)
Wraps the designated command by a callable catching and logging an occurred exception and optionally rethrowing it to provide it via the returned callable.protected Runnable
wrapRunnable(Runnable command, boolean loggingRequested, boolean rethrow)
Wraps the designated command by a runnable logging runtime exceptions and virtual machine errors instead of forwarding them if requested.-
Methods inherited from class java.util.concurrent.ScheduledThreadPoolExecutor
execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, schedule, schedule, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown, shutdownNow, submit, submit, submit
-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated, toString
-
Methods inherited from class java.util.concurrent.AbstractExecutorService
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.concurrent.ExecutorService
awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated
-
-
-
-
Field Detail
-
logger
protected final Logger logger
The logger for logging unhandled results, Exception, RuntimeException or Error.
-
continueRegularAfterException
protected final boolean continueRegularAfterException
Whether regular executions should continue even in case of exceptions. The exceptions will be logged.
-
-
Constructor Detail
-
LoggingScheduledThreadPoolExecutor
public LoggingScheduledThreadPoolExecutor(int corePoolSize, Adept2ThreadFactory threadFactory, boolean continueRegularAfterException)
Creates a newLoggingScheduledThreadPoolExecutor
with the designated core pool size and thread factory also providing the logger. Optionally regular scheduled tasks (ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
andScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
may be set to log runtime exceptions and virtual machine errors and continue regular execution instead of failing and stopping.- Parameters:
corePoolSize
- The number of threads to keep in the pool, even if they are idle.threadFactory
- The factory to use when the executor creates a new thread and which provides the logger for unhandled results and exceptions.continueRegularAfterException
- Whether regular executions should continue even in case of runtime exceptions and virtual machine errors. These exceptions will be logged.- See Also:
ScheduledThreadPoolExecutor(int, java.util.concurrent.ThreadFactory)
-
LoggingScheduledThreadPoolExecutor
public LoggingScheduledThreadPoolExecutor(int corePoolSize, Adept2ThreadFactory threadFactory, RejectedExecutionHandler handler, boolean continueRegularAfterException)
Creates a newLoggingScheduledThreadPoolExecutor
with the designated core pool size, rejected execution handler and thread factory also providing the logger. Optionally regular scheduled tasks (ScheduledExecutorService.scheduleAtFixedRate(Runnable, long, long, TimeUnit)
andScheduledExecutorService.scheduleWithFixedDelay(Runnable, long, long, TimeUnit)
may be set to log runtime exceptions and virtual machine errors and continue regular execution instead of failing and stopping.- Parameters:
corePoolSize
- The number of threads to keep in the pool, even if they are idle.threadFactory
- The factory to use when the executor creates a new thread and which provides the logger for unhandled results and exceptions.handler
- The handler to use when execution is blocked.continueRegularAfterException
- Whether regular executions should continue even in case of runtime exceptions and virtual machine errors. These exceptions will be logged.- See Also:
ScheduledThreadPoolExecutor(int, java.util.concurrent.ThreadFactory, RejectedExecutionHandler)
-
-
Method Detail
-
schedule
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit, boolean logException, boolean rethrowException)
CallsScheduledThreadPoolExecutor.schedule(Runnable, long, TimeUnit)
and optionally wraps the runnable to log an occurring runtime exception and virtual machine error. This will be provided via the returned future if rethrown. If you are about to check the returned future, you should let the runtime exception/virtual machine error be rethrown.- Parameters:
logException
- Whether to log an occurred runtime exception/virtual machine error.rethrowException
- Whether to rethrow the logged runtime exception/virtual machine error. This parameter will be ignored iflogException
isfalse
.
-
schedule
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit, boolean logException)
CallsScheduledThreadPoolExecutor.schedule(Callable, long, TimeUnit)
and optionally wraps the callable to catch and log an occurred exception. The exception will always be provided via the designated callable and the returned future.- Parameters:
logException
- Whether to additionally log an occurred exception.
-
scheduleAtFixedRate
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
- Specified by:
scheduleAtFixedRate
in interfaceScheduledExecutorService
- Overrides:
scheduleAtFixedRate
in classScheduledThreadPoolExecutor
-
scheduleWithFixedDelay
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
- Specified by:
scheduleWithFixedDelay
in interfaceScheduledExecutorService
- Overrides:
scheduleWithFixedDelay
in classScheduledThreadPoolExecutor
-
execute
public void execute(Runnable command, boolean logException)
CallsScheduledThreadPoolExecutor.execute(Runnable)
and optionally wraps the runnable to catch and log an occurring runtime exception and virtual machine error. Since there is no one expecting the exception, it will not be rethrown after logging.- Parameters:
logException
- Whether to additionally log an occurred runtime exception/virtual machine error.
-
submit
public Future<?> submit(Runnable task, boolean logException, boolean rethrowException)
CallsScheduledThreadPoolExecutor.submit(Runnable)
and optionally wraps the runnable to log an occurring exception. The exception will be provided via the returned future if rethrown. If you are about to check the returned future, you should let the exception be rethrown.- Parameters:
logException
- Whether to log an occurred exception.rethrowException
- Whether to rethrow the logged exception. This will be ignored iflogException
isfalse
.
-
submit
public <T> Future<T> submit(Runnable task, T result, boolean logException)
CallsScheduledThreadPoolExecutor.submit(Runnable, Object)
and optionally wraps the runnable to log an occurring exception. The exception will always be provided via the returned future.- Parameters:
logException
- Whether to additionally log an occurred exception.
-
submit
public <T> Future<T> submit(Callable<T> task, boolean logException)
CallsScheduledThreadPoolExecutor.submit(Callable)
and optionally wraps the callable to catch and log an occurred exception. The exception will always be provided via the returned callable.- Parameters:
logException
- Whether to additionally log an occurred exception.
-
decorateTask
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)
- Overrides:
decorateTask
in classScheduledThreadPoolExecutor
-
decorateTask
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
- Overrides:
decorateTask
in classScheduledThreadPoolExecutor
-
beforeExecute
protected void beforeExecute(Thread t, Runnable r)
- Overrides:
beforeExecute
in classThreadPoolExecutor
-
afterExecute
protected void afterExecute(Runnable r, Throwable t)
- Overrides:
afterExecute
in classThreadPoolExecutor
-
wrapRunnable
protected Runnable wrapRunnable(Runnable command, boolean loggingRequested, boolean rethrow)
Wraps the designated command by a runnable logging runtime exceptions and virtual machine errors instead of forwarding them if requested. Optionally, the runtime exception/virtual machine error may be rethrown to store it in the returned future.- Parameters:
command
- The command which to wrap.loggingRequested
- Whether to actually wrap the runnable.rethrow
- Whether to rethrow the exception to store it in the returned future.- Returns:
- The command optionally wrapped by a runnable logging runtime exceptions and virtual machine errors and optionally rethrowing them.
-
wrapCallable
protected <V> Callable<V> wrapCallable(Callable<V> command, boolean loggingRequested, boolean rethrow)
Wraps the designated command by a callable catching and logging an occurred exception and optionally rethrowing it to provide it via the returned callable.- Parameters:
command
- The command which to wrap.loggingRequested
- Whether to actually wrap the callable.rethrow
- Whether to rethrow the exception to provide it via the returned callable.- Returns:
- The command optionally wrapped by a callable catching and logging an occurred exception and optionally rethrowing it.
-
-