Interface OnceTaskListener

All Superinterfaces:
TaskListener, io.netty.util.TimerTask

public interface OnceTaskListener extends io.netty.util.TimerTask, TaskListener
Timer listener callback, executed only 1 time.
The onUpdate method will only be executed when triggerUpdate  returns true.

By default, triggerUpdate returns true. Developers can control the execution of the onUpdate method by controlling the return value of the triggerUpdate method.
example
// Executed once, after 500 and 800 milliseconds
TaskKit.runOnce(() -> log.info("500 delayMilliseconds"), 500);
TaskKit.runOnce(() -> log.info("800 delayMilliseconds"), 800);

// Executed once, after 10 seconds
TaskKit.runOnce(new YourOnceTaskListener(), 10, TimeUnit.SECONDS);

// Executed once, after 1500 Milliseconds, onUpdate is executed only when theTriggerUpdate is true
boolean theTriggerUpdate = RandomKit.randomBoolean();
TaskKit.runOnce(new OnceTaskListener() {
@Override
public void onUpdate() {
log.info("1500 delayMilliseconds");
}

@Override
public boolean triggerUpdate() {
return theTriggerUpdate;
}

}, 1500, TimeUnit.MILLISECONDS);

Since:
21
Author:
渔民小镇
See Also:
date:
2023-12-06
  • Method Summary

    Modifier and Type
    Method
    Description
    default Executor
    The executor for executing onUpdate
    default void
    Exception callback
    void
    Timer listener callback
    default void
    run(io.netty.util.Timeout timeout)
     
    default boolean
    Whether to trigger the onUpdate listener callback method

    Methods inherited from interface io.netty.util.TimerTask

    cancelled
  • Method Details

    • run

      default void run(io.netty.util.Timeout timeout) throws Exception
      Specified by:
      run in interface io.netty.util.TimerTask
      Throws:
      Exception
    • 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.