Interface OnceTaskListener
- All Superinterfaces:
TaskListener, io.netty.util.TimerTask
Timer listener callback, executed only 1 time.
TheexampleonUpdatemethod will only be executed whentriggerUpdatereturns true. By default, triggerUpdate returns true. Developers can control the execution of the onUpdate method by controlling the return value of the triggerUpdate method.
// 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
Methods inherited from interface io.netty.util.TimerTask
cancelled
-
Method Details
-
run
-
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
Exception callbackWhen the triggerUpdate or onUpdate method throws an exception, it will be passed here.
- Parameters:
e- e
-
getExecutor
The executor for executing onUpdateIf 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.
Exampledefault 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.
-