From 802666a7ce74caf25f16711049644085a5ac0423 Mon Sep 17 00:00:00 2001 From: John Niang Date: Fri, 21 Mar 2025 14:21:25 +0800 Subject: [PATCH] Fix the problem of unit test failure under Java 21 (#7304) #### What type of PR is this? /kind bug /area core /milestone 2.20.x #### What this PR does / why we need it: This PR adjusts the order of checking instance of ExecutorService and AutoCloseable interfaces. Because the ExecutorService extends AutoCloseable in Java 21. As a result, unit tests against the method won't be passed in Java 21. #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../halo/app/extension/controller/DefaultController.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/api/src/main/java/run/halo/app/extension/controller/DefaultController.java b/api/src/main/java/run/halo/app/extension/controller/DefaultController.java index 895bb4735..b3bdf9dfa 100644 --- a/api/src/main/java/run/halo/app/extension/controller/DefaultController.java +++ b/api/src/main/java/run/halo/app/extension/controller/DefaultController.java @@ -242,15 +242,17 @@ public class DefaultController implements Controller { } try { - if (executor instanceof AutoCloseable closeable) { + // we have to check if the executor is an instance of ExecutorService at first. + // Because ExecutorService extends AutoCloseable interface in Java 21 + if (executor instanceof ExecutorService executorService) { + closeExecutorService(executorService); + } else if (executor instanceof AutoCloseable closeable) { closeable.close(); if (Thread.currentThread().isInterrupted()) { log.warn("Wait timeout for controller {} shutdown", name); } else { log.info("Controller {} is disposed", name); } - } else if (executor instanceof ExecutorService executorService) { - closeExecutorService(executorService); } } catch (Exception e) { log.warn("Interrupted while waiting for controller {} shutdown", name);