统一shutdown方法

This commit is contained in:
Xujiayao 2025-12-21 17:43:01 +08:00
parent bd7ec574fb
commit 59f0cd2529
5 changed files with 49 additions and 52 deletions

View file

@ -4,6 +4,7 @@ import com.xujiayao.discord_mc_chat.client.ClientDMCC;
import com.xujiayao.discord_mc_chat.commands.CommandEventHandler;
import com.xujiayao.discord_mc_chat.server.ServerDMCC;
import com.xujiayao.discord_mc_chat.standalone.TerminalManager;
import com.xujiayao.discord_mc_chat.utils.ExecutorServiceUtils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import com.xujiayao.discord_mc_chat.utils.config.ModeManager;
import com.xujiayao.discord_mc_chat.utils.events.EventManager;
@ -160,16 +161,8 @@ public class DMCC {
// Shutdown OkHttpClient
try (ExecutorService executor = OK_HTTP_CLIENT.dispatcher().executorService();
Cache ignored1 = OK_HTTP_CLIENT.cache()) {
executor.shutdown();
if (ConfigManager.getBoolean("shutdown.graceful_shutdown")) {
// Allow up to 10 minutes for ongoing requests to complete
boolean ignored2 = executor.awaitTermination(10, TimeUnit.MINUTES);
} else {
// Allow up to 5 seconds for ongoing requests to complete
boolean ignored2 = executor.awaitTermination(5, TimeUnit.SECONDS);
}
executor.shutdownNow();
Cache ignored = OK_HTTP_CLIENT.cache()) {
ExecutorServiceUtils.shutdownAnExecutor(executor);
OK_HTTP_CLIENT.connectionPool().evictAll();
} catch (Exception e) {

View file

@ -1,5 +1,6 @@
package com.xujiayao.discord_mc_chat.client;
import com.xujiayao.discord_mc_chat.utils.ExecutorServiceUtils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import java.util.concurrent.ExecutorService;
@ -43,18 +44,7 @@ public class ClientDMCC {
nettyClient.stop();
}
executor.shutdown();
try {
if (ConfigManager.getBoolean("shutdown.graceful_shutdown")) {
// Allow up to 10 minutes for ongoing requests to complete
boolean ignored = executor.awaitTermination(10, TimeUnit.MINUTES);
} else {
// Allow up to 5 seconds for ongoing requests to complete
boolean ignored = executor.awaitTermination(5, TimeUnit.SECONDS);
}
} catch (Exception ignored) {
}
executor.shutdownNow();
ExecutorServiceUtils.shutdownAnExecutor(executor);
LOGGER.info("DMCC Client component shut down.");
}

View file

@ -1,7 +1,9 @@
package com.xujiayao.discord_mc_chat.commands;
import com.xujiayao.discord_mc_chat.DMCC;
import com.xujiayao.discord_mc_chat.utils.ExecutorServiceUtils;
import com.xujiayao.discord_mc_chat.utils.StringUtils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import com.xujiayao.discord_mc_chat.utils.events.EventManager;
import com.xujiayao.discord_mc_chat.utils.i18n.I18nManager;
@ -18,21 +20,19 @@ import static com.xujiayao.discord_mc_chat.Constants.LOGGER;
*/
public class CommandEventHandler {
private static ExecutorService commandExecutor;
private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(r -> new Thread(r, "DMCC-Command"));;
/**
* Initializes the Command event handlers.
*/
public static void init() {
commandExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "DMCC-Command"));
EventManager.register(CommandEvents.DisableEvent.class, event -> EXECUTOR.submit(DMCC::shutdown));
EventManager.register(CommandEvents.DisableEvent.class, event -> commandExecutor.submit(DMCC::shutdown));
EventManager.register(CommandEvents.EnableEvent.class, event -> EXECUTOR.submit(DMCC::init));
EventManager.register(CommandEvents.EnableEvent.class, event -> commandExecutor.submit(DMCC::init));
EventManager.register(CommandEvents.ReloadEvent.class, event -> EXECUTOR.submit(DMCC::reload));
EventManager.register(CommandEvents.ReloadEvent.class, event -> commandExecutor.submit(DMCC::reload));
EventManager.register(CommandEvents.ShutdownEvent.class, event -> commandExecutor.submit(() -> {
EventManager.register(CommandEvents.ShutdownEvent.class, event -> EXECUTOR.submit(() -> {
System.exit(0);
}));
@ -65,16 +65,6 @@ public class CommandEventHandler {
* Shuts down the command executor service.
*/
public static void shutdown() {
if (commandExecutor != null && !commandExecutor.isShutdown()) {
commandExecutor.shutdown();
try {
if (!commandExecutor.awaitTermination(5, TimeUnit.SECONDS)) {
commandExecutor.shutdownNow();
}
} catch (InterruptedException e) {
commandExecutor.shutdownNow();
Thread.currentThread().interrupt();
}
}
ExecutorServiceUtils.shutdownAnExecutor(EXECUTOR);
}
}

View file

@ -1,6 +1,7 @@
package com.xujiayao.discord_mc_chat.server;
import com.xujiayao.discord_mc_chat.server.discord.DiscordManager;
import com.xujiayao.discord_mc_chat.utils.ExecutorServiceUtils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import java.util.concurrent.ExecutorService;
@ -72,18 +73,7 @@ public class ServerDMCC {
DiscordManager.shutdown();
executor.shutdown();
try {
if (ConfigManager.getBoolean("shutdown.graceful_shutdown")) {
// Allow up to 10 minutes for ongoing requests to complete
boolean ignored = executor.awaitTermination(10, TimeUnit.MINUTES);
} else {
// Allow up to 5 seconds for ongoing requests to complete
boolean ignored = executor.awaitTermination(5, TimeUnit.SECONDS);
}
} catch (Exception ignored) {
}
executor.shutdownNow();
ExecutorServiceUtils.shutdownAnExecutor(executor);
LOGGER.info("DMCC Server component shut down.");
}

View file

@ -0,0 +1,34 @@
package com.xujiayao.discord_mc_chat.utils;
import com.xujiayao.discord_mc_chat.utils.config.ConfigManager;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Utility class for ExecutorService related operations.
*
* @author Xujiayao
*/
public class ExecutorServiceUtils {
/**
* Shuts down the given ExecutorService gracefully based on configuration.
*
* @param executor the ExecutorService to shut down
*/
public static void shutdownAnExecutor(ExecutorService executor) {
executor.shutdown();
try {
if (ConfigManager.getBoolean("shutdown.graceful_shutdown")) {
// Allow up to 10 minutes for ongoing requests to complete
boolean ignored = executor.awaitTermination(10, TimeUnit.MINUTES);
} else {
// Allow up to 5 seconds for ongoing requests to complete
boolean ignored = executor.awaitTermination(5, TimeUnit.SECONDS);
}
} catch (Exception ignored) {
}
executor.shutdownNow();
}
}