-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to platform Independent Task Handler
- Loading branch information
1 parent
65d19cb
commit 28964c5
Showing
11 changed files
with
218 additions
and
176 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
133 changes: 133 additions & 0 deletions
133
DriveBackup/src/main/java/ratismal/drivebackup/handler/task/IndependentTaskHandler.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,133 @@ | ||
package ratismal.drivebackup.handler.task; | ||
|
||
import org.jetbrains.annotations.Contract; | ||
import org.jetbrains.annotations.NotNull; | ||
import ratismal.drivebackup.handler.logging.LoggingHandler; | ||
import ratismal.drivebackup.handler.logging.PrefixedLogger; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.ScheduledFuture; | ||
import java.util.concurrent.ScheduledThreadPoolExecutor; | ||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* A platform independent task handler for scheduling tasks. | ||
*/ | ||
public final class IndependentTaskHandler { | ||
|
||
private static final long TIMEOUT = 10L; | ||
private static final TimeUnit TIMEOUT_UNIT = TimeUnit.MINUTES; | ||
private static final long KEEP_ALIVE_TIME = 60L; | ||
private static final TimeUnit KEEP_ALIVE_UNIT = TimeUnit.SECONDS; | ||
private static final int MAXIMUM_POOL_SIZE = 100; | ||
private static final int CORE_POOL_SIZE = 0; | ||
|
||
private ScheduledExecutorService scheduledExecutor; | ||
private final PrefixedLogger logger; | ||
private ThreadFactory threadFactory; | ||
|
||
/** | ||
* Creates a new IndependentTaskHandler instance. | ||
* After constructing, call {@link #setup()} to initialize the handler. | ||
* @param loggingHandler the logging handler to use | ||
*/ | ||
@Contract (pure = true) | ||
public IndependentTaskHandler(@NotNull LoggingHandler loggingHandler) { | ||
logger = loggingHandler.getPrefixedLogger("IndependentTaskHandler"); | ||
} | ||
|
||
/** | ||
* Initializes the task handler. | ||
*/ | ||
public void setup() { | ||
threadFactory = new SimpleThreadFactory(); | ||
ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(CORE_POOL_SIZE, threadFactory); | ||
scheduledThreadPoolExecutor.setMaximumPoolSize(MAXIMUM_POOL_SIZE); | ||
scheduledThreadPoolExecutor.setKeepAliveTime(KEEP_ALIVE_TIME, KEEP_ALIVE_UNIT); | ||
scheduledExecutor = scheduledThreadPoolExecutor; | ||
} | ||
|
||
/** | ||
* Shuts down the task handler. | ||
* Waits for tasks to finish for up to 10 minutes. | ||
* If tasks do not finish in time, forces shutdown. | ||
*/ | ||
public void shutdown() { | ||
shutdown(TIMEOUT, TIMEOUT_UNIT); | ||
} | ||
|
||
/** | ||
* Shuts down the task handler. | ||
* Waits for tasks to finish for up to timeout + unit time. | ||
* If tasks do not finish in time, forces shutdown. | ||
*/ | ||
public void shutdown(long timeout, TimeUnit unit){ | ||
logger.info("Shutting down IndependentTaskHandler"); | ||
logger.info("Shutting down scheduled executor"); | ||
scheduledExecutor.shutdown(); | ||
logger.info("Waiting for tasks to finish up to " + timeout + " " + unit.toString() + "..."); | ||
try { | ||
boolean sch = scheduledExecutor.awaitTermination(timeout, unit); | ||
if (sch) { | ||
logger.info("Scheduled tasks finished"); | ||
} else { | ||
logger.error("Scheduled tasks did not finish in time, forcing shutdown"); | ||
scheduledExecutor.shutdownNow(); | ||
logger.info("Forced scheduled executor shutdown"); | ||
} | ||
logger.info("IndependentTaskHandler shutdown complete"); | ||
} catch (InterruptedException e) { | ||
logger.error("Was interrupted while waiting for tasks to finish: ", e); | ||
} | ||
} | ||
|
||
/** | ||
* Schedules a repeating task. | ||
* The task will run after the delay and then every period. | ||
* <p> | ||
* see {@link ScheduledExecutorService#scheduleAtFixedRate(Runnable, long, long, TimeUnit)} | ||
* | ||
* @param delay the delay before the first run | ||
* @param period the period between runs | ||
* @param unit the time unit of the delay and period | ||
* @param runnable the task to run | ||
* @return a ScheduledFuture representing the task | ||
*/ | ||
public @NotNull ScheduledFuture<?> scheduleRepeatingTask(long delay, long period, TimeUnit unit, Runnable runnable) { | ||
return scheduledExecutor.scheduleAtFixedRate(runnable, delay, period, unit); | ||
} | ||
|
||
/** | ||
* Schedules a delayed task. | ||
* The task will run after the delay. | ||
* <p> | ||
* see {@link ScheduledExecutorService#schedule(Runnable, long, TimeUnit)} | ||
* | ||
* @param delay the delay before the task runs | ||
* @param unit the time unit of the delay | ||
* @param runnable the task to run | ||
* @return a ScheduledFuture representing the task | ||
*/ | ||
public @NotNull ScheduledFuture<?> scheduleLaterTask(long delay, TimeUnit unit, Runnable runnable) { | ||
return scheduledExecutor.schedule(runnable, delay, unit); | ||
} | ||
|
||
/** | ||
* Schedules a delayed task. | ||
* The task will run after the delay. | ||
* <p> | ||
* see {@link ScheduledExecutorService#schedule(Callable, long, TimeUnit)} | ||
* | ||
* @param <T> the type of the result | ||
* @param delay the delay | ||
* @param unit the time unit of the delay | ||
* @param callable the task to run | ||
* @return a ScheduledFuture representing the task | ||
*/ | ||
public <T> @NotNull ScheduledFuture<T> scheduleLaterTask(long delay, TimeUnit unit, Callable<T> callable) { | ||
return scheduledExecutor.schedule(callable, delay, unit); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
DriveBackup/src/main/java/ratismal/drivebackup/handler/task/SimpleThreadFactory.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,33 @@ | ||
package ratismal.drivebackup.handler.task; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.concurrent.ThreadFactory; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public final class SimpleThreadFactory implements ThreadFactory { | ||
|
||
private static final AtomicInteger poolNumber = new AtomicInteger(1); | ||
private final ThreadGroup group; | ||
private final AtomicInteger threadNumber = new AtomicInteger(1); | ||
private final String namePrefix; | ||
|
||
public SimpleThreadFactory() { | ||
group = Thread.currentThread().getThreadGroup(); | ||
namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-"; | ||
} | ||
|
||
public @NotNull Thread newThread(@NotNull Runnable r) { | ||
Thread t = new Thread(group, r, | ||
namePrefix + threadNumber.getAndIncrement(), | ||
0); | ||
if (t.isDaemon()) { | ||
t.setDaemon(false); | ||
} | ||
if (t.getPriority() != Thread.NORM_PRIORITY) { | ||
t.setPriority(Thread.NORM_PRIORITY); | ||
} | ||
return t; | ||
} | ||
|
||
} |
53 changes: 0 additions & 53 deletions
53
DriveBackup/src/main/java/ratismal/drivebackup/handler/task/TaskHandler.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
DriveBackup/src/main/java/ratismal/drivebackup/handler/task/TaskIdentifier.java
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.