com.tenio.task.TaskManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tenio Show documentation
Show all versions of tenio Show documentation
TenIO is a java NIO (Non-blocking I/O) based server specifically designed for multiplayer games.
It supports UDP and TCP transports which are handled by Netty for high-speed network transmission.
It uses MsgPack for compressing data so that can be transferred quickly through the network.
This framework can help you quickly create a game server or integrate it into your system.
/*
The MIT License
Copyright (c) 2016-2020 kong
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.tenio.task;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.annotation.concurrent.GuardedBy;
import com.tenio.exception.RunningScheduledTaskException;
import com.tenio.logger.AbstractLogger;
/**
* This class uses Java scheduler ({@link ScheduledFuture}) to manage your
* tasks. The scheduler is used to schedule a thread or task that executes at a
* certain period of time or periodically at a fixed interval. It's useful when
* you want to create a time counter before starting a match or send messages
* periodically for one player.
*
* @see ITaskManager
*
* @author kong
*
*/
public final class TaskManager extends AbstractLogger implements ITaskManager {
/**
* A list of tasks in the server
*/
@GuardedBy("this")
private final Map> __tasks = new HashMap>();
@Override
public synchronized void create(String id, ScheduledFuture> task) {
if (__tasks.containsKey(id)) {
try {
if (!__tasks.get(id).isDone() || !__tasks.get(id).isCancelled()) {
throw new RunningScheduledTaskException();
}
} catch (RunningScheduledTaskException e) {
error(e, "task id: ", id);
return;
}
}
__tasks.put(id, task);
info("RUN TASK", buildgen(id, " >Time left> ", task.getDelay(TimeUnit.SECONDS), " seconds"));
}
@Override
public synchronized void kill(String id) {
var task = __tasks.get(id);
if (task != null) {
task.cancel(true);
info("KILLED TASK", buildgen(id, " >Time left> ", task.getDelay(TimeUnit.SECONDS), " seconds"));
__tasks.remove(id);
}
}
@Override
public synchronized void clear() {
__tasks.forEach((id, task) -> {
task.cancel(true);
info("KILLED TASK", buildgen(id, " >Time left> ", task.getDelay(TimeUnit.SECONDS), " seconds"));
});
__tasks.clear();
}
@Override
public synchronized int getRemainTime(String id) {
var task = __tasks.get(id);
if (task != null) {
return (int) task.getDelay(TimeUnit.SECONDS);
}
return -1;
}
}