1package com.osroyale.game.task;
3import com.google.common.base.Preconditions;
4import org.apache.logging.log4j.LogManager;
5import org.apache.logging.log4j.Logger;
7import java.util.ArrayList;
9import java.util.ListIterator;
10import java.util.Queue;
11import java.util.concurrent.ConcurrentLinkedQueue;
44* The
class that handles scheduling tasks, and processing them.
53 private static final Logger logger = LogManager.getLogger(
TaskManager.class);
60 private final Queue<Task> pending =
new ConcurrentLinkedQueue<>();
66 private final List<Task> active =
new ArrayList<>();
82 Preconditions.checkNotNull(task);
85 if (!task.canSchedule()) {
89 task.setRunning(
true);
92 if (task.isInstant()) {
95 }
catch (Exception ex) {
96 logger.warn(String.format(
"error executing task: %s", task.getClass().getSimpleName()), ex);
101 }
catch (Exception ex) {
102 logger.error(String.format(
"error scheduling task: %s", task.getClass().getSimpleName()), ex);
111 while((t = pending.poll()) !=
null) {
115 for (
final ListIterator<Task> itr = active.listIterator(); itr.hasNext();) {
116 final Task task = itr.next();
120 task.setExecutionTime();
122 }
catch (Exception ex) {
123 logger.warn(String.format(
"Error executing task: %s [pendingQueue: %d executionQueue: %d], ", task.getClass().getSimpleName(), pending.size(), active.size()), ex);
127 if (!task.isRunning()) {
140 cancel(attachment,
false);
152 public void cancel(Object attachment,
boolean logout) {
153 for (
Task task : pending) {
154 if (attachment.equals(task.getAttachment().orElse(
null))) {
160 public List<Task> getTasks() {
void cancel(Object attachment)
synchronized void schedule(Task task)
void cancel(Object attachment, boolean logout)
synchronized void processTasks()