Interface IntervalTaskListener


public interface IntervalTaskListener
Scheduled task listener, using HashedWheelTimer to simulate ScheduledExecutorService scheduling.

triggerUpdate and onUpdate methods will only be executed when isActive returns true.

example

// Called every minute
TaskKit.runIntervalMinute(() -> log.info("tick 1 Minute"), 1);
// Called every 2 minutes
TaskKit.runIntervalMinute(() -> log.info("tick 2 Minute"), 2);

// Called every 2 seconds
TaskKit.runInterval(() -> log.info("tick 2 Seconds"), 2, TimeUnit.SECONDS);
// Called every 30 minutes
TaskKit.runInterval(() -> log.info("tick 30 Minute"), 30, TimeUnit.MINUTES);

Author:
渔民小镇
See Also:
date:
2023-12-01
  • Method Summary

    Modifier and Type
    Method
    Description
    default Executor
    The executor for executing onUpdate
    default boolean
    Whether the task is active
    default void
    Exception callback
    void
    Timer listener callback
    default boolean
    Whether to trigger the onUpdate listener callback method
  • Method Details

    • isActive

      default boolean isActive()
      Whether the task is active
      Returns:
      false means inactive, and the current TimerListener will be removed from the listener list
    • triggerUpdate

      default boolean triggerUpdate()
      Whether to trigger the onUpdate listener callback method
      Returns:
      true to execute the onUpdate method
    • onUpdate

      void onUpdate()
      Timer listener callback
    • onException

      default void onException(Throwable e)
      Exception callback
      When the triggerUpdate or onUpdate method throws an exception, it will be passed here.
      
      Parameters:
      e - e
    • getExecutor

      default Executor getExecutor()
      The executor for executing onUpdate
      If null is returned, it will be executed within HashedWheelTimer.
      
      If there are time-consuming tasks, such as those involving IO operations, it is recommended to specify an executor to run the current callback (onUpdate method) to avoid blocking other tasks.
      
      Example
      default Executor getExecutor() {
      // Time-consuming task, specify an executor to consume the current onUpdate
      return TaskKit.getCacheExecutor();
      }
      
      
      Returns:
      If the return value is null, execution will use the current thread (default HashedWheelTimer), otherwise, the specified executor will be used.