-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created TaskScheduler to be able to cancel existing events for a mess…
…age when overwriting paginator; Changed EventHandler methods to accept the ID directly, instead of a message.
- Loading branch information
Showing
3 changed files
with
57 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/github/ygimenez/model/TaskScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.github.ygimenez.model; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Task manager for pagination events' expiration | ||
*/ | ||
public class TaskScheduler { | ||
private final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); | ||
private final Map<String, ScheduledFuture<?>> tasks = new HashMap<>(); | ||
|
||
/** | ||
* Schedule a new task, cancelling and replacing the previous if exists. | ||
* | ||
* @param id The task identifier. | ||
* @param task The task itself. | ||
* @param time The time for the timeout. | ||
* @param unit The unit for the timeout. | ||
* @return | ||
*/ | ||
public ScheduledFuture<?> schedule(String id, Runnable task, long time, TimeUnit unit) { | ||
ScheduledFuture<?> t = worker.schedule(task, time, unit); | ||
ScheduledFuture<?> prev = tasks.put(id, t); | ||
if (prev != null) { | ||
prev.cancel(true); | ||
} | ||
|
||
return t; | ||
} | ||
} |