From 58365c242424f0f45ab125f59d73f296928b1e46 Mon Sep 17 00:00:00 2001 From: MiniDay <372403923@qq.com> Date: Sun, 29 Oct 2023 16:40:51 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/core/bukkit/HamsterCorePlugin.java | 2 +- .../plugin/core/bukkit/api/CoreBukkitAPI.java | 20 ---------- .../plugin/core/bungee/HamsterCorePlugin.java | 2 +- .../plugin/core/bungee/api/CoreBungeeAPI.java | 20 ---------- .../mc/plugin/core/common/api/CoreAPI.java | 39 +++++++++++++------ .../core/common/thread/CountdownThread.java | 2 +- .../mc/plugin/core/common/util/CoreUtils.java | 4 +- 7 files changed, 33 insertions(+), 56 deletions(-) diff --git a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/HamsterCorePlugin.java b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/HamsterCorePlugin.java index 65a40e6..31b3776 100644 --- a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/HamsterCorePlugin.java +++ b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/HamsterCorePlugin.java @@ -78,7 +78,7 @@ public class HamsterCorePlugin extends JavaPlugin { logger.info("已关闭数据库连接池."); CoreAPI.getInstance().getExecutorService().shutdownNow(); logger.info("已关闭线程池."); - CoreAPI.getInstance().getScheduledExecutorService().shutdownNow(); + CoreAPI.getInstance().getScheduledService().shutdownNow(); logger.info("已关闭调度器."); for (Player player : Bukkit.getOnlinePlayers()) { InventoryView view = player.getOpenInventory(); diff --git a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/api/CoreBukkitAPI.java b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/api/CoreBukkitAPI.java index 6690a97..3b89f2c 100644 --- a/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/api/CoreBukkitAPI.java +++ b/core-bukkit/src/main/java/cn/hamster3/mc/plugin/core/bukkit/api/CoreBukkitAPI.java @@ -5,7 +5,6 @@ import cn.hamster3.mc.plugin.core.bukkit.util.serializer.ItemStackAdapter; import cn.hamster3.mc.plugin.core.bukkit.util.serializer.PotionEffectAdapter; import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; -import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory; import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter; import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter; import com.google.gson.Gson; @@ -20,17 +19,11 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.potion.PotionEffect; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - @SuppressWarnings("unused") public final class CoreBukkitAPI extends CoreAPI { private final Gson gson; private final Gson humanGson; private final HikariDataSource datasource; - private final ExecutorService executorService; - private final ScheduledExecutorService scheduledExecutorService; private CoreBukkitAPI() { HamsterCorePlugin plugin = HamsterCorePlugin.getInstance(); @@ -72,9 +65,6 @@ public final class CoreBukkitAPI extends CoreAPI { .serializeNulls() .setPrettyPrinting() .create(); - - executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor")); - scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler")); } public static CoreBukkitAPI getInstance() { @@ -107,14 +97,4 @@ public final class CoreBukkitAPI extends CoreAPI { public @NotNull Gson getHumanGson() { return humanGson; } - - @Override - public @NotNull ExecutorService getExecutorService() { - return executorService; - } - - @Override - public @NotNull ScheduledExecutorService getScheduledExecutorService() { - return scheduledExecutorService; - } } diff --git a/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterCorePlugin.java b/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterCorePlugin.java index 3331e58..1e03fd8 100644 --- a/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterCorePlugin.java +++ b/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/HamsterCorePlugin.java @@ -47,7 +47,7 @@ public class HamsterCorePlugin extends Plugin { logger.info("已关闭数据库连接池."); CoreAPI.getInstance().getExecutorService().shutdownNow(); logger.info("已关闭线程池."); - CoreAPI.getInstance().getScheduledExecutorService().shutdownNow(); + CoreAPI.getInstance().getScheduledService().shutdownNow(); logger.info("已关闭调度器."); long time = System.currentTimeMillis() - start; logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms."); diff --git a/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/CoreBungeeAPI.java b/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/CoreBungeeAPI.java index e071181..e36b5a3 100644 --- a/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/CoreBungeeAPI.java +++ b/core-bungeecord/src/main/java/cn/hamster3/mc/plugin/core/bungee/api/CoreBungeeAPI.java @@ -4,7 +4,6 @@ import cn.hamster3.mc.plugin.core.bungee.HamsterCorePlugin; import cn.hamster3.mc.plugin.core.bungee.util.CoreBungeeCordUtils; import cn.hamster3.mc.plugin.core.common.api.CoreAPI; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; -import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory; import cn.hamster3.mc.plugin.core.common.util.serializer.ComponentTypeAdapter; import cn.hamster3.mc.plugin.core.common.util.serializer.MessageTypeAdapter; import com.google.gson.Gson; @@ -16,17 +15,11 @@ import net.kyori.adventure.text.Component; import net.md_5.bungee.config.Configuration; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - @SuppressWarnings("unused") public final class CoreBungeeAPI extends CoreAPI { private final Gson gson; private final Gson humanGson; private final HikariDataSource datasource; - private final ExecutorService executorService; - private final ScheduledExecutorService scheduledExecutorService; private CoreBungeeAPI() { HamsterCorePlugin plugin = HamsterCorePlugin.getInstance(); @@ -64,9 +57,6 @@ public final class CoreBungeeAPI extends CoreAPI { .serializeNulls() .setPrettyPrinting() .create(); - - executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor")); - scheduledExecutorService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler")); } public static CoreBungeeAPI getInstance() { @@ -99,14 +89,4 @@ public final class CoreBungeeAPI extends CoreAPI { public @NotNull Gson getHumanGson() { return humanGson; } - - @Override - public @NotNull ExecutorService getExecutorService() { - return executorService; - } - - @Override - public @NotNull ScheduledExecutorService getScheduledExecutorService() { - return scheduledExecutorService; - } } diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/api/CoreAPI.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/api/CoreAPI.java index ed746d0..379f4fe 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/api/CoreAPI.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/api/CoreAPI.java @@ -1,5 +1,6 @@ package cn.hamster3.mc.plugin.core.common.api; +import cn.hamster3.mc.plugin.core.common.thread.NamedThreadFactory; import com.google.gson.Gson; import lombok.Getter; import net.kyori.adventure.platform.AudienceProvider; @@ -9,19 +10,46 @@ import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @SuppressWarnings("unused") public abstract class CoreAPI { @Getter protected static CoreAPI instance; + /** + * 异步线程池 + */ + @Getter + private final ExecutorService executorService; + /** + * 调度器线程池 + */ + @Getter + private final ScheduledExecutorService scheduledService; + + public CoreAPI() { + executorService = Executors.newCachedThreadPool(new NamedThreadFactory("HamsterCore - Executor")); + scheduledService = Executors.newScheduledThreadPool(1, new NamedThreadFactory("HamsterCore - Scheduler")); + } @NotNull public abstract AudienceProvider getAudienceProvider(); + /** + * 获取 HamsterCore 公用数据库连接池 + * + * @return 公用数据库连接池 + */ @NotNull public abstract DataSource getDataSource(); + /** + * 获取 HamsterCore 公用数据库连接 + * + * @return 公用数据库连接 + * @throws SQLException - + */ @NotNull public Connection getConnection() throws SQLException { return getDataSource().getConnection(); @@ -39,15 +67,4 @@ public abstract class CoreAPI { @NotNull public abstract Gson getHumanGson(); - /** - * @return 异步线程池 - */ - @NotNull - public abstract ExecutorService getExecutorService(); - - /** - * @return 调度器线程池 - */ - @NotNull - public abstract ScheduledExecutorService getScheduledExecutorService(); } diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/thread/CountdownThread.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/thread/CountdownThread.java index c528502..9f9cc6d 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/thread/CountdownThread.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/thread/CountdownThread.java @@ -23,7 +23,7 @@ public abstract class CountdownThread implements Runnable { } public void start(long initialDelay) { - future = CoreAPI.getInstance().getScheduledExecutorService() + future = CoreAPI.getInstance().getScheduledService() .scheduleWithFixedDelay(this, initialDelay, interval, TimeUnit.MILLISECONDS); } diff --git a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/CoreUtils.java b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/CoreUtils.java index a38a48d..c84f227 100644 --- a/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/CoreUtils.java +++ b/core-common/src/main/java/cn/hamster3/mc/plugin/core/common/util/CoreUtils.java @@ -36,10 +36,10 @@ public final class CoreUtils { @Deprecated public static ExecutorService WORKER_EXECUTOR = CoreAPI.getInstance().getExecutorService(); /** - * @deprecated 使用 {@link CoreAPI#getScheduledExecutorService()} + * @deprecated 使用 {@link CoreAPI#getScheduledService()} */ @Deprecated - public static ScheduledExecutorService SCHEDULED_EXECUTOR = CoreAPI.getInstance().getScheduledExecutorService(); + public static ScheduledExecutorService SCHEDULED_EXECUTOR = CoreAPI.getInstance().getScheduledService(); private CoreUtils() { }