Class 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.
  • Field Details

    • 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.
    • periodicScheduler

      protected final Collection<Thread> periodicScheduler
      All threads that are currently adding a periodic runnable via scheduleAtFixedRate(Runnable, long, long, TimeUnit) or scheduleWithFixedDelay(Runnable, long, long, TimeUnit). In this case we do not decorate using a LoggingScheduledFuture since the RunnableScheduledFuture (task) will get a reference to the surrounding decorator which causes an object cycle with the cleanup and prevents it from being garbage-collected.
      This is not a big problem since periodic tasks do not have a result at all so cleanup-logging is irrelevant.
  • Constructor Details

  • Method Details

    • schedule

      public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit, boolean logException, boolean rethrowException)
      Calls ScheduledThreadPoolExecutor.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 if logException is false.
    • schedule

      public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit, boolean logException)
      Calls ScheduledThreadPoolExecutor.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 interface ScheduledExecutorService
      Overrides:
      scheduleAtFixedRate in class ScheduledThreadPoolExecutor
    • scheduleWithFixedDelay

      public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
      Specified by:
      scheduleWithFixedDelay in interface ScheduledExecutorService
      Overrides:
      scheduleWithFixedDelay in class ScheduledThreadPoolExecutor
    • execute

      public void execute(Runnable command, boolean logException)
      Calls ScheduledThreadPoolExecutor.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)
      Calls ScheduledThreadPoolExecutor.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 if logException is false.
    • submit

      public <T> Future<T> submit(Runnable task, T result, boolean logException)
      Calls ScheduledThreadPoolExecutor.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)
      Calls ScheduledThreadPoolExecutor.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 class ScheduledThreadPoolExecutor
    • decorateTask

      protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)
      Overrides:
      decorateTask in class ScheduledThreadPoolExecutor
    • beforeExecute

      protected void beforeExecute(Thread t, Runnable r)
      Overrides:
      beforeExecute in class ThreadPoolExecutor
    • afterExecute

      protected void afterExecute(Runnable r, Throwable t)
      Overrides:
      afterExecute in class ThreadPoolExecutor
    • 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.