com.hazelcast.util.executor

## Class LoggingScheduledExecutor

- java.lang.Object
- - java.util.concurrent.AbstractExecutorService
  - - java.util.concurrent.ThreadPoolExecutor
    - - java.util.concurrent.ScheduledThreadPoolExecutor
      - - com.hazelcast.util.executor.LoggingScheduledExecutor

- All Implemented Interfaces:
    Executor, ExecutorService, ScheduledExecutorService

      public class LoggingScheduledExecutor
      extends ScheduledThreadPoolExecutor

  Logs execution exceptions by overriding `ThreadPoolExecutor.afterExecute(java.lang.Runnable, java.lang.Throwable)` and `ScheduledThreadPoolExecutor.decorateTask(java.lang.Runnable, java.util.concurrent.RunnableScheduledFuture<V>)` methods. Reasoning is given tasks to `ScheduledThreadPoolExecutor` stops silently if there is an execution exception. Note: Task decoration is only needed to call given tasks `toString` methods. `ScheduledExecutorService.scheduleWithFixedDelay(java.lang.Runnable, long, long, java.util.concurrent.TimeUnit)` Remove on cancel: To prevent running into an accumulation of cancelled task which can cause gc related problems (e.g. transactions in combination with LockResource eviction), it is best that tasks are removed from the scheduler on cancellation. In Java 7+ the there is a method `ScheduledThreadPoolExecutor#setRemoveOnCancelPolicy(boolean)` which removes the runnable on cancellation. Removal of tasks is done in logarithmic time (see ScheduledThreadPoolExecutor.DelayedWorkQueue.indexOf where there is a direct lookup instead of a linear scan over the queue). So in Java 7 we try to set this method using reflection. In Java 7+ the manualRemoveOnCancel is ignored, since it has an efficient removal of cancelled tasks and therefore it will always apply it. If we would wrap the task with the RemoveOnCancelFuture, it would even prevent constant time removal. In Java 6 there is no such method setRemoveOnCancelPolicy and therefore the task is wrapped in a RemoveOnCancelFuture which will remove itself from the work-queue on cancellation. However this removal isn't done in constant time, but in linear time because Java 6 ScheduledThreadPoolExecutor doesn't support a constant time removal. By default in Java 6, the removal of this task is done based on the 'removeOnCancel' property which gets its value from the `GroupProperty.TASK_SCHEDULER_REMOVE_ON_CANCEL`

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor

      `ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory)`
    `LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, boolean removeOnCancel)`
    `LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, boolean removeOnCancel, RejectedExecutionHandler handler)`
    `LoggingScheduledExecutor(ILogger logger, int corePoolSize, ThreadFactory threadFactory, RejectedExecutionHandler handler)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `afterExecute(Runnable runnable, Throwable throwable)`
    `protected <V> RunnableScheduledFuture<V>` | `decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task)`
    `protected <V> RunnableScheduledFuture<V>` | `decorateTask(Runnable runnable, RunnableScheduledFuture<V> task)`
    `void` | `notifyShutdownInitiated()`

    - ### Methods inherited from class java.util.concurrent.ScheduledThreadPoolExecutor

      `execute, getContinueExistingPeriodicTasksAfterShutdownPolicy, getExecuteExistingDelayedTasksAfterShutdownPolicy, getQueue, getRemoveOnCancelPolicy, schedule, schedule, scheduleAtFixedRate, scheduleWithFixedDelay, setContinueExistingPeriodicTasksAfterShutdownPolicy, setExecuteExistingDelayedTasksAfterShutdownPolicy, setRemoveOnCancelPolicy, shutdown, shutdownNow, submit, submit, submit`

    - ### Methods inherited from class java.util.concurrent.ThreadPoolExecutor

      `allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, 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`

- - ### Constructor Detail

    - #### LoggingScheduledExecutor

          public LoggingScheduledExecutor(ILogger logger,
                                          int corePoolSize,
                                          ThreadFactory threadFactory)

    - #### LoggingScheduledExecutor

          public LoggingScheduledExecutor(ILogger logger,
                                          int corePoolSize,
                                          ThreadFactory threadFactory,
                                          boolean removeOnCancel)

    - #### LoggingScheduledExecutor

          public LoggingScheduledExecutor(ILogger logger,
                                          int corePoolSize,
                                          ThreadFactory threadFactory,
                                          RejectedExecutionHandler handler)

    - #### LoggingScheduledExecutor

          public LoggingScheduledExecutor(ILogger logger,
                                          int corePoolSize,
                                          ThreadFactory threadFactory,
                                          boolean removeOnCancel,
                                          RejectedExecutionHandler handler)

  - ### Method Detail

    - #### 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`

    - #### afterExecute

          protected void afterExecute(Runnable runnable,
                                      Throwable throwable)

      Overrides:
        `afterExecute` in class `ThreadPoolExecutor`

    - #### notifyShutdownInitiated

          public void notifyShutdownInitiated()

Copyright © 2019. All rights reserved.