jdk17 support

This commit is contained in:
Anthony Stirling 2025-06-03 12:01:28 +01:00
parent 95fbd4647d
commit a99c104053
3 changed files with 36 additions and 14 deletions

View File

@ -5,7 +5,6 @@ import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
@ -24,6 +23,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.common.controller.WebSocketProgressController;
import stirling.software.common.model.job.JobProgress;
import stirling.software.common.model.job.JobResponse;
import stirling.software.common.util.ExecutorFactory;
/** Service for executing jobs asynchronously or synchronously */
@Service
@ -36,7 +36,7 @@ public class JobExecutorService {
private final HttpServletRequest request;
private final ResourceMonitor resourceMonitor;
private final JobQueue jobQueue;
private final ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
private final ExecutorService executor = ExecutorFactory.newVirtualOrCachedThreadExecutor();
private final long effectiveTimeoutMs;
public JobExecutorService(

View File

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import stirling.software.common.controller.WebSocketProgressController;
import stirling.software.common.model.job.JobProgress;
import stirling.software.common.util.ExecutorFactory;
/**
* Manages a queue of jobs with dynamic sizing based on system resources. Used when system resources
@ -46,18 +47,8 @@ public class JobQueue {
private BlockingQueue<QueuedJob> jobQueue;
private final Map<String, QueuedJob> jobMap = new ConcurrentHashMap<>();
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final ExecutorService jobExecutor;
private final ExecutorService jobExecutor = ExecutorFactory.newVirtualOrCachedThreadExecutor();
// Initialize executor based on Java version
{
ExecutorService executor;
try {
executor = Executors.newVirtualThreadPerTaskExecutor();
} catch (NoSuchMethodError e) {
executor = Executors.newCachedThreadPool();
}
jobExecutor = executor;
}
private boolean shuttingDown = false;
@Getter private int rejectedJobs = 0;

View File

@ -0,0 +1,31 @@
package stirling.software.common.util;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class ExecutorFactory {
/**
* Creates an ExecutorService using virtual threads if available (Java 21+), or falls back to a
* cached thread pool on older Java versions.
*/
public static ExecutorService newVirtualOrCachedThreadExecutor() {
try {
ExecutorService executor =
(ExecutorService)
Executors.class
.getMethod("newVirtualThreadPerTaskExecutor")
.invoke(null);
return executor;
} catch (NoSuchMethodException e) {
log.debug("Virtual threads not available; falling back to cached thread pool.");
} catch (Exception e) {
log.debug("Error initializing virtual thread executor: {}", e.getMessage(), e);
}
return Executors.newCachedThreadPool();
}
}