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.
    Author:
    Ulrich Kreher
    • 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.
    • Method Detail

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