-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-162 Add DatabaseManager closure on shutdown, add scheduler adapter. (
#162) * Add DatabaseManager closure on shutdown * Refactor task scheduling using VirtualThreadScheduler. * Fix inconsistent indentation style * Remove useless import. * Remove unused EXECUTOR_SERVICE constant.
- Loading branch information
Showing
6 changed files
with
97 additions
and
46 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
4 changes: 1 addition & 3 deletions
4
src/main/java/com/eternalcode/discordapp/guildstats/GuildStatisticsTask.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
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
12 changes: 12 additions & 0 deletions
12
src/main/java/com/eternalcode/discordapp/scheduler/Scheduler.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,12 @@ | ||
package com.eternalcode.discordapp.scheduler; | ||
|
||
import java.time.Duration; | ||
|
||
public interface Scheduler { | ||
|
||
void schedule(Runnable task, Duration delay); | ||
|
||
void schedule(Runnable task); | ||
|
||
void shutdown() throws InterruptedException; | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/eternalcode/discordapp/scheduler/VirtualThreadSchedulerImpl.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,56 @@ | ||
package com.eternalcode.discordapp.scheduler; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class VirtualThreadSchedulerImpl implements Scheduler { | ||
|
||
// works on javca 21+ | ||
private static final ExecutorService EXECUTOR_SERVICE = Executors.newVirtualThreadPerTaskExecutor(); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(VirtualThreadSchedulerImpl.class.getName()); | ||
|
||
@Override | ||
public void schedule(Runnable task, Duration delay) { | ||
EXECUTOR_SERVICE.submit(() -> { | ||
while (!Thread.currentThread().isInterrupted()) { | ||
task.run(); | ||
|
||
try { | ||
Thread.sleep(delay.toMillis()); | ||
} | ||
catch (InterruptedException exception) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void schedule(Runnable task) { | ||
EXECUTOR_SERVICE.submit(task); | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
try { | ||
LOGGER.info("Shutting down executor service..."); | ||
EXECUTOR_SERVICE.shutdown(); | ||
|
||
if (!EXECUTOR_SERVICE.awaitTermination(60, TimeUnit.SECONDS)) { | ||
LOGGER.warn("Executor did not terminate in the specified time."); | ||
EXECUTOR_SERVICE.shutdownNow(); | ||
} | ||
|
||
LOGGER.info("Executor service shut down successfully."); | ||
} | ||
catch (InterruptedException exception) { | ||
LOGGER.error("Shutdown interrupted", exception); | ||
EXECUTOR_SERVICE.shutdownNow(); | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} |