From d1ad4c4697425ef761a7a4fc04116c3380647044 Mon Sep 17 00:00:00 2001 From: GedMarc Date: Mon, 13 May 2024 00:45:47 +0200 Subject: [PATCH] Updates for some port allocations and scheduled always to single threaded execution --- .../guicedinjection/GuiceContext.java | 2 +- .../guicedee/guicedinjection/JobService.java | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java index d09290e..c7b95f9 100644 --- a/src/main/java/com/guicedee/guicedinjection/GuiceContext.java +++ b/src/main/java/com/guicedee/guicedinjection/GuiceContext.java @@ -224,7 +224,7 @@ public void destroy() */ private static int getJavaVersion() { - String version = getSystemPropertyOrEnvironment("java.version", "11"); + String version = getSystemPropertyOrEnvironment("java.version", "21"); if (version.startsWith("1.")) { version = version.substring(2); diff --git a/src/main/java/com/guicedee/guicedinjection/JobService.java b/src/main/java/com/guicedee/guicedinjection/JobService.java index 12efccf..12dd792 100644 --- a/src/main/java/com/guicedee/guicedinjection/JobService.java +++ b/src/main/java/com/guicedee/guicedinjection/JobService.java @@ -35,11 +35,12 @@ public class JobService implements IGuicePreDestroy, IJobService private static TimeUnit defaultWaitUnit = TimeUnit.SECONDS; public static final JobService INSTANCE = new JobService(); - private static ExecutorService jobCleanup = null; - + + private static final ThreadFactory factory = Thread.ofVirtual().factory(); + static { - jobCleanup = INSTANCE.jobCleanup(); + INSTANCE.jobCleanup(); } public JobService() @@ -302,9 +303,7 @@ public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable threa .isShutdown()) { registerJobPollingPool(jobPoolName, - Executors.newScheduledThreadPool(Runtime - .getRuntime() - .availableProcessors())); + Executors.newSingleThreadScheduledExecutor(factory)); } ScheduledExecutorService service = pollingMap.get(jobPoolName); service.scheduleAtFixedRate(thread, 1L, delay, unit); @@ -320,18 +319,18 @@ public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable threa @Override public ScheduledExecutorService addPollingJob(String jobPoolName, Runnable thread, long initialDelay, long delay, TimeUnit unit) { + ScheduledExecutorService scheduledExecutorService = null; if (!pollingMap.containsKey(jobPoolName) || pollingMap .get(jobPoolName) .isTerminated() || pollingMap .get(jobPoolName) .isShutdown()) { - registerJobPollingPool(jobPoolName, - Executors.newSingleThreadScheduledExecutor()); + scheduledExecutorService = registerJobPollingPool(jobPoolName, + Executors.newSingleThreadScheduledExecutor()); } - ScheduledExecutorService service = pollingMap.get(jobPoolName); - service.scheduleAtFixedRate(thread, initialDelay, delay, unit); - return service; + scheduledExecutorService.scheduleAtFixedRate(thread, initialDelay, delay, unit); + return scheduledExecutorService; } /**