From 4fb60fbb49578694c4c68d14687344b866400883 Mon Sep 17 00:00:00 2001
From: MisakaTAT
Date: Fri, 7 Jun 2024 13:46:46 +0800
Subject: [PATCH] :art: Optimized autowired
---
build.gradle.kts | 2 +-
.../shiro/adapter/WebSocketClientHandler.java | 29 +++-----
.../shiro/adapter/WebSocketServerHandler.java | 63 ++++++----------
.../java/com/mikuac/shiro/boot/Shiro.java | 74 ++++++++-----------
.../shiro/boot/ShiroAutoConfiguration.java | 72 ++++++------------
.../shiro/common/limit/RateLimiter.java | 38 +++-------
.../mikuac/shiro/common/utils/EventUtils.java | 11 +--
.../com/mikuac/shiro/core/BotFactory.java | 71 ++++++++----------
.../mikuac/shiro/handler/ActionHandler.java | 34 ++++-----
.../mikuac/shiro/handler/EventHandler.java | 34 ++-------
.../shiro/handler/event/MessageEvent.java | 17 ++---
.../mikuac/shiro/handler/event/MetaEvent.java | 6 +-
.../shiro/handler/event/NoticeEvent.java | 24 ++----
.../shiro/handler/event/NotifyEvent.java | 11 +--
.../shiro/handler/event/RequestEvent.java | 15 ++--
.../com/mikuac/shiro/task/ScheduledTask.java | 33 +++++++++
.../com/mikuac/shiro/task/ShiroAsyncTask.java | 4 +-
.../shiro/task/ShiroTaskPoolConfig.java | 4 +-
18 files changed, 210 insertions(+), 332 deletions(-)
create mode 100644 src/main/java/com/mikuac/shiro/task/ScheduledTask.java
diff --git a/build.gradle.kts b/build.gradle.kts
index 208868bb..37c9b98f 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -1,7 +1,7 @@
@file:Suppress("SpellCheckingInspection")
group = "com.mikuac"
-version = "2.2.7"
+version = "9.9.9"
plugins {
signing
diff --git a/src/main/java/com/mikuac/shiro/adapter/WebSocketClientHandler.java b/src/main/java/com/mikuac/shiro/adapter/WebSocketClientHandler.java
index a6d7a947..aa207818 100644
--- a/src/main/java/com/mikuac/shiro/adapter/WebSocketClientHandler.java
+++ b/src/main/java/com/mikuac/shiro/adapter/WebSocketClientHandler.java
@@ -16,7 +16,6 @@
import com.mikuac.shiro.task.ShiroAsyncTask;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
@@ -34,36 +33,26 @@
@Slf4j
public class WebSocketClientHandler extends TextWebSocketHandler {
+ private final CoreEvent coreEvent;
private final EventHandler eventHandler;
-
private final BotFactory botFactory;
-
private final ActionHandler actionHandler;
-
private final ShiroAsyncTask shiroAsyncTask;
-
private final BotContainer botContainer;
+ private final WebSocketProperties wsProp;
- private CoreEvent coreEvent;
-
- @Autowired
- public void setCoreEvent(CoreEvent coreEvent) {
- this.coreEvent = coreEvent;
- }
-
- private WebSocketProperties wsProp;
-
- @Autowired
- public void setWebSocketProperties(WebSocketProperties wsProp) {
- this.wsProp = wsProp;
- }
-
- public WebSocketClientHandler(EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler, ShiroAsyncTask shiroAsyncTask, BotContainer botContainer) {
+ public WebSocketClientHandler(
+ EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler,
+ ShiroAsyncTask shiroAsyncTask, BotContainer botContainer, CoreEvent coreEvent,
+ WebSocketProperties wsProp
+ ) {
this.eventHandler = eventHandler;
this.botFactory = botFactory;
this.actionHandler = actionHandler;
this.shiroAsyncTask = shiroAsyncTask;
this.botContainer = botContainer;
+ this.coreEvent = coreEvent;
+ this.wsProp = wsProp;
}
@Override
diff --git a/src/main/java/com/mikuac/shiro/adapter/WebSocketServerHandler.java b/src/main/java/com/mikuac/shiro/adapter/WebSocketServerHandler.java
index 16f5102e..d9e002dc 100644
--- a/src/main/java/com/mikuac/shiro/adapter/WebSocketServerHandler.java
+++ b/src/main/java/com/mikuac/shiro/adapter/WebSocketServerHandler.java
@@ -13,13 +13,12 @@
import com.mikuac.shiro.enums.SessionStatusEnum;
import com.mikuac.shiro.handler.ActionHandler;
import com.mikuac.shiro.handler.EventHandler;
+import com.mikuac.shiro.properties.ShiroProperties;
import com.mikuac.shiro.properties.WebSocketProperties;
+import com.mikuac.shiro.task.ScheduledTask;
import com.mikuac.shiro.task.ShiroAsyncTask;
import lombok.NonNull;
-import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
@@ -28,9 +27,7 @@
import java.io.IOException;
import java.util.ConcurrentModificationException;
import java.util.Objects;
-import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
@@ -42,49 +39,31 @@
@Slf4j
public class WebSocketServerHandler extends TextWebSocketHandler {
- @Setter
- private static int waitWebsocketConnect = 0;
-
private final EventHandler eventHandler;
-
private final BotFactory botFactory;
-
private final ActionHandler actionHandler;
-
private final ShiroAsyncTask shiroAsyncTask;
-
private final BotContainer botContainer;
-
- private ScheduledExecutorService scheduledExecutorService;
-
- @Autowired
- public void setScheduledExecutorService(ThreadPoolTaskExecutor shiroTaskExecutor) {
- var executor = new ScheduledThreadPoolExecutor(shiroTaskExecutor.getCorePoolSize(),
- shiroTaskExecutor.getThreadPoolExecutor().getThreadFactory());
- executor.setRemoveOnCancelPolicy(true);
- scheduledExecutorService = executor;
- }
-
- private CoreEvent coreEvent;
-
- @Autowired
- public void setCoreEvent(CoreEvent coreEvent) {
- this.coreEvent = coreEvent;
- }
-
- private WebSocketProperties wsProp;
-
- @Autowired
- public void setWebSocketProperties(WebSocketProperties wsProp) {
- this.wsProp = wsProp;
- }
-
- public WebSocketServerHandler(EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler, ShiroAsyncTask shiroAsyncTask, BotContainer botContainer) {
+ private final CoreEvent coreEvent;
+ private final WebSocketProperties wsProp;
+ private final ScheduledTask scheduledTask;
+ private final ShiroProperties shiroProps;
+
+ @SuppressWarnings("java:S107")
+ public WebSocketServerHandler(
+ EventHandler eventHandler, BotFactory botFactory, ActionHandler actionHandler,
+ ShiroAsyncTask shiroAsyncTask, BotContainer botContainer, CoreEvent coreEvent,
+ WebSocketProperties wsProp, ScheduledTask scheduledTask, ShiroProperties shiroProps
+ ) {
this.eventHandler = eventHandler;
this.botFactory = botFactory;
this.actionHandler = actionHandler;
this.shiroAsyncTask = shiroAsyncTask;
this.botContainer = botContainer;
+ this.coreEvent = coreEvent;
+ this.wsProp = wsProp;
+ this.shiroProps = shiroProps;
+ this.scheduledTask = scheduledTask;
}
@Override
@@ -109,7 +88,7 @@ public void afterConnectionEstablished(@NonNull WebSocketSession session) {
var sessionContext = session.getAttributes();
sessionContext.put(Connection.SESSION_STATUS_KEY, SessionStatusEnum.ONLINE);
- if (waitWebsocketConnect <= 0) {
+ if (shiroProps.getWaitBotConnect() <= 0) {
if (botContainer.robots.containsKey(xSelfId)) {
log.info("Bot {} already connected with another instance", xSelfId);
sessionContext.clear();
@@ -141,7 +120,7 @@ public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull Cl
}
var sessionContext = session.getAttributes();
- if (waitWebsocketConnect <= 0) {
+ if (shiroProps.getWaitBotConnect() <= 0) {
sessionContext.clear();
botContainer.robots.remove(xSelfId);
log.warn("Account {} disconnected", xSelfId);
@@ -150,13 +129,13 @@ public void afterConnectionClosed(@NonNull WebSocketSession session, @NonNull Cl
}
// after the session is disconnected, postpone deletion instead of immediate removal
// if not reconnected within a certain timeframe, execute the deletion scheduled task
- ScheduledFuture> removeSelfFuture = scheduledExecutorService.schedule(() -> {
+ ScheduledFuture> removeSelfFuture = scheduledTask.executor().schedule(() -> {
if (botContainer.robots.containsKey(xSelfId)) {
botContainer.robots.remove(xSelfId);
log.warn("Account {} disconnected", xSelfId);
coreEvent.offline(xSelfId);
}
- }, waitWebsocketConnect, TimeUnit.SECONDS);
+ }, shiroProps.getWaitBotConnect(), TimeUnit.SECONDS);
sessionContext.put(Connection.SESSION_STATUS_KEY, SessionStatusEnum.OFFLINE);
sessionContext.put(Connection.FUTURE_KEY, removeSelfFuture);
}
diff --git a/src/main/java/com/mikuac/shiro/boot/Shiro.java b/src/main/java/com/mikuac/shiro/boot/Shiro.java
index 1c6d70dc..ab0d2922 100644
--- a/src/main/java/com/mikuac/shiro/boot/Shiro.java
+++ b/src/main/java/com/mikuac/shiro/boot/Shiro.java
@@ -4,10 +4,13 @@
import com.mikuac.shiro.adapter.WebSocketServerHandler;
import com.mikuac.shiro.core.BotContainer;
import com.mikuac.shiro.core.BotFactory;
+import com.mikuac.shiro.core.CoreEvent;
import com.mikuac.shiro.handler.ActionHandler;
import com.mikuac.shiro.handler.EventHandler;
+import com.mikuac.shiro.properties.ShiroProperties;
import com.mikuac.shiro.properties.WebSocketProperties;
import com.mikuac.shiro.properties.WebSocketServerProperties;
+import com.mikuac.shiro.task.ScheduledTask;
import com.mikuac.shiro.task.ShiroAsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
@@ -25,74 +28,55 @@
@Configuration
public class Shiro {
- private WebSocketServerProperties wsServerProp;
+ private final WebSocketServerProperties wsServerProp;
+ private final WebSocketProperties wsProp;
+ private final BotFactory botFactory;
+ private final EventHandler eventHandler;
+ private final ActionHandler actionHandler;
+ private final ShiroAsyncTask shiroAsyncTask;
+ private final BotContainer botContainer;
+ private final CoreEvent coreEvent;
+ private final ScheduledTask scheduledTask;
+ private final ShiroProperties shiroProps;
@Autowired
- public void setWebSocketServerProperties(WebSocketServerProperties wsServerProp) {
+ public Shiro(
+ WebSocketServerProperties wsServerProp, WebSocketProperties wsProp, BotFactory botFactory,
+ EventHandler eventHandler, ActionHandler actionHandler, ShiroAsyncTask shiroAsyncTask,
+ BotContainer botContainer, CoreEvent coreEvent, ScheduledTask scheduledTask, ShiroProperties shiroProps
+ ) {
this.wsServerProp = wsServerProp;
- }
-
- private WebSocketProperties wsProp;
-
- @Autowired
- public void setWebSocketProperties(WebSocketProperties wsProp) {
this.wsProp = wsProp;
- }
-
- private BotFactory botFactory;
-
- @Autowired
- public void setBotFactory(BotFactory botFactory) {
this.botFactory = botFactory;
- }
-
- private EventHandler eventHandler;
-
- @Autowired
- public void setEventHandler(EventHandler eventHandler) {
this.eventHandler = eventHandler;
- }
-
- private ActionHandler actionHandler;
-
- @Autowired
- public void setActionHandler(ActionHandler actionHandler) {
this.actionHandler = actionHandler;
- }
-
- private ShiroAsyncTask shiroAsyncTask;
-
- @Autowired
- public void setShiroAsyncTask(ShiroAsyncTask shiroAsyncTask) {
this.shiroAsyncTask = shiroAsyncTask;
- }
-
- private BotContainer botContainer;
-
- @Autowired
- public void setBotContainer(BotContainer botContainer) {
this.botContainer = botContainer;
+ this.coreEvent = coreEvent;
+ this.scheduledTask = scheduledTask;
+ this.shiroProps = shiroProps;
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "shiro.ws.server.enable", havingValue = "true")
public WebSocketServerHandler webSocketServerHandler() {
- return new WebSocketServerHandler(eventHandler, botFactory, actionHandler, shiroAsyncTask, botContainer);
+ return new WebSocketServerHandler(
+ eventHandler, botFactory, actionHandler, shiroAsyncTask,
+ botContainer, coreEvent, wsProp, scheduledTask, shiroProps
+ );
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = "shiro.ws.client.enable", havingValue = "true")
public WebSocketClientHandler webSocketClientHandler() {
- return new WebSocketClientHandler(eventHandler, botFactory, actionHandler, shiroAsyncTask, botContainer);
+ return new WebSocketClientHandler(
+ eventHandler, botFactory, actionHandler, shiroAsyncTask,
+ botContainer, coreEvent, wsProp
+ );
}
- /**
- *
createWebSocketContainer.
- *
- * @return {@link ServletServerContainerFactoryBean}
- */
@Bean
@ConditionalOnMissingBean
public ServletServerContainerFactoryBean createWebSocketServerContainer() {
diff --git a/src/main/java/com/mikuac/shiro/boot/ShiroAutoConfiguration.java b/src/main/java/com/mikuac/shiro/boot/ShiroAutoConfiguration.java
index c0046dc4..2f8f2edf 100644
--- a/src/main/java/com/mikuac/shiro/boot/ShiroAutoConfiguration.java
+++ b/src/main/java/com/mikuac/shiro/boot/ShiroAutoConfiguration.java
@@ -8,6 +8,7 @@
import com.mikuac.shiro.properties.WebSocketClientProperties;
import com.mikuac.shiro.properties.WebSocketProperties;
import com.mikuac.shiro.properties.WebSocketServerProperties;
+import com.mikuac.shiro.task.ScheduledTask;
import jakarta.annotation.Nonnull;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
@@ -15,7 +16,6 @@
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableAsync;
-import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHttpHeaders;
import org.springframework.web.socket.client.WebSocketConnectionManager;
@@ -25,8 +25,7 @@
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import java.util.Objects;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.Optional;
import java.util.concurrent.TimeUnit;
/**
@@ -43,57 +42,32 @@
@ComponentScan("com.mikuac.shiro")
public class ShiroAutoConfiguration implements WebSocketConfigurer {
- private WebSocketServerProperties wsServerProp;
+ private final WebSocketServerProperties wsServerProp;
+ private final WebSocketClientProperties wsClientProp;
+ private final WebSocketProperties wsProp;
+ private final ScheduledTask scheduledTask;
+ private final ShiroProperties shiroProperties;
+ private final WebSocketServerHandler webSocketServerHandler;
+ private final WebSocketClientHandler webSocketClientHandler;
@Autowired
- public void setWebSocketServerProperties(WebSocketServerProperties wsServerProp) {
- this.wsServerProp = wsServerProp;
- }
-
- private WebSocketClientProperties wsClientProp;
+ public ShiroAutoConfiguration(
+ WebSocketServerProperties wsServerProp,
+ WebSocketClientProperties wsClientProp,
+ WebSocketProperties wsProp,
+ ShiroProperties shiroProperties,
+ ScheduledTask scheduledTask,
+ Optional webSocketServerHandler,
+ Optional webSocketClientHandler
+ ) {
- @Autowired
- public void setWebSocketClientProperties(WebSocketClientProperties wsClientProp) {
+ this.wsServerProp = wsServerProp;
this.wsClientProp = wsClientProp;
- }
-
- private ScheduledExecutorService scheduledExecutorService;
-
- @Autowired
- public void setScheduledExecutorService(ThreadPoolTaskExecutor shiroTaskExecutor) {
- var executor = new ScheduledThreadPoolExecutor(shiroTaskExecutor.getCorePoolSize(),
- shiroTaskExecutor.getThreadPoolExecutor().getThreadFactory());
- executor.setRemoveOnCancelPolicy(true);
- scheduledExecutorService = executor;
- }
-
- private WebSocketProperties wsProp;
-
- @Autowired
- public void setWebSocketProperties(WebSocketProperties wsProp) {
+ this.scheduledTask = scheduledTask;
this.wsProp = wsProp;
- }
-
- private ShiroProperties shiroProperties;
-
- @Autowired
- public void setShiroProperties(ShiroProperties shiroProperties) {
this.shiroProperties = shiroProperties;
- WebSocketServerHandler.setWaitWebsocketConnect(shiroProperties.getWaitBotConnect());
- }
-
- private WebSocketServerHandler webSocketServerHandler;
-
- @Autowired(required = false)
- public void setWebSocketServerHandler(WebSocketServerHandler webSocketServerHandler) {
- this.webSocketServerHandler = webSocketServerHandler;
- }
-
- private WebSocketClientHandler webSocketClientHandler;
-
- @Autowired(required = false)
- public void setWebSocketClientHandler(WebSocketClientHandler webSocketClientHandler) {
- this.webSocketClientHandler = webSocketClientHandler;
+ this.webSocketServerHandler = webSocketServerHandler.orElse(null);
+ this.webSocketClientHandler = webSocketClientHandler.orElse(null);
}
@Override
@@ -130,7 +104,7 @@ private void createWebsocketClient() {
manager.setHeaders(headers);
manager.setAutoStartup(true);
- scheduledExecutorService.scheduleAtFixedRate(() -> {
+ scheduledTask.executor().scheduleAtFixedRate(() -> {
if (!manager.isConnected()) {
manager.startInternal();
}
diff --git a/src/main/java/com/mikuac/shiro/common/limit/RateLimiter.java b/src/main/java/com/mikuac/shiro/common/limit/RateLimiter.java
index 017fabe3..939d86b2 100644
--- a/src/main/java/com/mikuac/shiro/common/limit/RateLimiter.java
+++ b/src/main/java/com/mikuac/shiro/common/limit/RateLimiter.java
@@ -21,6 +21,13 @@
@Component
public class RateLimiter implements ApplicationRunner {
+ private final RateLimiterProperties props;
+
+ @Autowired
+ public RateLimiter(RateLimiterProperties props) {
+ this.props = props;
+ }
+
/**
* 重入锁
*/
@@ -31,42 +38,17 @@ public class RateLimiter implements ApplicationRunner {
*/
private final Condition condition = lock.newCondition();
- /**
- * 补充速率(每秒补充的令牌数量)
- */
- private long rate;
-
- /**
- * 令牌桶容量
- */
- private long capacity;
-
/**
* 桶内剩余令牌数量
*/
private long currentTokenQuantities;
- /**
- * 等待超时
- */
- private int timeout;
-
- /**
- * @param properties {@link RateLimiterProperties}
- */
- @Autowired
- public void setProperties(RateLimiterProperties properties) {
- this.capacity = properties.getCapacity();
- this.rate = properties.getRate();
- this.timeout = properties.getTimeout();
- currentTokenQuantities = capacity;
- }
-
/**
* 令牌定时补充器
*/
@Override
public void run(ApplicationArguments args) {
+ currentTokenQuantities = props.getCapacity();
new Timer().schedule(new TimerTask() {
@Override
public void run() {
@@ -118,7 +100,7 @@ public boolean acquire(int permits) {
lock.lock();
try {
while (!getToken(permits)) {
- boolean await = condition.await(timeout, TimeUnit.SECONDS);
+ boolean await = condition.await(props.getTimeout(), TimeUnit.SECONDS);
if (await) {
return false;
}
@@ -152,7 +134,7 @@ private boolean getToken(int permits) {
private void supplementToken() {
lock.lock();
try {
- currentTokenQuantities = Math.min(currentTokenQuantities + rate, capacity);
+ currentTokenQuantities = Math.min(currentTokenQuantities + props.getRate(), props.getCapacity());
condition.signalAll();
} finally {
lock.unlock();
diff --git a/src/main/java/com/mikuac/shiro/common/utils/EventUtils.java b/src/main/java/com/mikuac/shiro/common/utils/EventUtils.java
index cea141b8..70e2bb6d 100644
--- a/src/main/java/com/mikuac/shiro/common/utils/EventUtils.java
+++ b/src/main/java/com/mikuac/shiro/common/utils/EventUtils.java
@@ -24,17 +24,12 @@
@Component
public class EventUtils {
- private ApplicationContext ctx;
+ private final ApplicationContext ctx;
+ private final InjectionHandler injection;
@Autowired
- public void setCtx(ApplicationContext ctx) {
+ public EventUtils(ApplicationContext ctx, InjectionHandler injection) {
this.ctx = ctx;
- }
-
- private InjectionHandler injection;
-
- @Autowired
- public void setInjection(InjectionHandler injection) {
this.injection = injection;
}
diff --git a/src/main/java/com/mikuac/shiro/core/BotFactory.java b/src/main/java/com/mikuac/shiro/core/BotFactory.java
index c4171e0f..f813d8a8 100644
--- a/src/main/java/com/mikuac/shiro/core/BotFactory.java
+++ b/src/main/java/com/mikuac/shiro/core/BotFactory.java
@@ -2,9 +2,7 @@
import com.mikuac.shiro.annotation.common.Order;
import com.mikuac.shiro.annotation.common.Shiro;
-import com.mikuac.shiro.common.utils.AopTargetUtils;
import com.mikuac.shiro.common.utils.ScanUtils;
-import com.mikuac.shiro.exception.ShiroException;
import com.mikuac.shiro.handler.ActionHandler;
import com.mikuac.shiro.model.HandlerMethod;
import com.mikuac.shiro.properties.ShiroProperties;
@@ -31,29 +29,24 @@ public class BotFactory {
private static Set> annotations = new LinkedHashSet<>();
- private ActionHandler actionHandler;
+ private final ActionHandler actionHandler;
+ private final ShiroProperties shiroProps;
+ private final ApplicationContext applicationContext;
@Autowired
- public void setActionHandler(ActionHandler actionHandler) {
+ public BotFactory(
+ ActionHandler actionHandler, ShiroProperties shiroProps, ApplicationContext applicationContext
+ ) {
this.actionHandler = actionHandler;
- }
-
- private ShiroProperties shiroProperties;
-
- @Autowired
- public void setShiroProperties(ShiroProperties shiroProperties) {
- this.shiroProperties = shiroProperties;
- }
-
- private ApplicationContext applicationContext;
-
- @Autowired
- public void setApplicationContext(ApplicationContext applicationContext) {
+ this.shiroProps = shiroProps;
this.applicationContext = applicationContext;
}
+ // 以注解为键,存放包含此注解的处理方法
+ MultiValueMap, HandlerMethod> annotationHandler = new LinkedMultiValueMap<>();
+
/**
- * 获取所有注解类
+ * 获取所有注解
*
* @return 注解集合
*/
@@ -65,32 +58,18 @@ private static Set> getAnnotations() {
return annotations;
}
- /**
- * 创建Bot对象
- *
- * @param selfId 机器人账号
- * @param session {@link WebSocketSession}
- * @return {@link Bot}
- */
- public Bot createBot(long selfId, WebSocketSession session) {
-
- log.debug("Start creating bot instance: {}", selfId);
+ private MultiValueMap, HandlerMethod> getAnnotationHandler() {
+ if (!annotationHandler.isEmpty()) {
+ return annotationHandler;
+ }
// 获取 Spring 容器中所有指定类型的对象
Map beans = new HashMap<>(applicationContext.getBeansWithAnnotation(Shiro.class));
- // 一键多值 注解为 Key 存放所有包含某个注解的方法
- MultiValueMap, HandlerMethod> annotationHandler = new LinkedMultiValueMap<>();
beans.values().forEach(obj -> {
- Object target;
- try {
- target = AopTargetUtils.getTarget(obj);
- } catch (Exception e) {
- throw new ShiroException(e);
- }
- Class> beanClass = target.getClass();
- Arrays.stream(beanClass.getMethods()).forEach(method -> {
+ Class> clazz = obj.getClass();
+ Arrays.stream(clazz.getMethods()).forEach(method -> {
HandlerMethod handlerMethod = new HandlerMethod();
handlerMethod.setMethod(method);
- handlerMethod.setType(beanClass);
+ handlerMethod.setType(clazz);
handlerMethod.setObject(obj);
Arrays.stream(method.getDeclaredAnnotations()).forEach(annotation -> {
Set> as = getAnnotations();
@@ -102,7 +81,19 @@ public Bot createBot(long selfId, WebSocketSession session) {
});
});
this.sort(annotationHandler);
- return new Bot(selfId, session, actionHandler, shiroProperties.getPluginList(), annotationHandler, shiroProperties.getInterceptor());
+ return annotationHandler;
+ }
+
+ /**
+ * 创建Bot对象
+ *
+ * @param selfId 机器人账号
+ * @param session {@link WebSocketSession}
+ * @return {@link Bot}
+ */
+ public Bot createBot(long selfId, WebSocketSession session) {
+ log.debug("Bot instance creation started: {}", selfId);
+ return new Bot(selfId, session, actionHandler, shiroProps.getPluginList(), getAnnotationHandler(), shiroProps.getInterceptor());
}
/**
diff --git a/src/main/java/com/mikuac/shiro/handler/ActionHandler.java b/src/main/java/com/mikuac/shiro/handler/ActionHandler.java
index 76c7c881..c53cf9b3 100644
--- a/src/main/java/com/mikuac/shiro/handler/ActionHandler.java
+++ b/src/main/java/com/mikuac/shiro/handler/ActionHandler.java
@@ -41,38 +41,30 @@ public class ActionHandler {
/**
* WebSocket 配置
*/
- private WebSocketProperties wsProp;
-
- @Autowired
- public void setWebSocketProperties(WebSocketProperties wsProp) {
- this.wsProp = wsProp;
- }
+ private final WebSocketProperties wsProp;
/**
* 限速器配置
*/
- private RateLimiterProperties rateLimiterProperties;
-
- @Autowired
- public void setRateLimiterProperties(RateLimiterProperties rateLimiterProperties) {
- this.rateLimiterProperties = rateLimiterProperties;
- }
+ private final RateLimiterProperties rateLimiterProps;
/**
* 限速器
*/
- private RateLimiter rateLimiter;
-
- @Autowired
- public void setRateLimiter(RateLimiter rateLimiter) {
- this.rateLimiter = rateLimiter;
- }
+ private final RateLimiter rateLimiter;
/**
* 用于标识请求,可以是任何类型的数据,OneBot 将会在调用结果中原样返回
*/
private int echo = 0;
+ @Autowired
+ public ActionHandler(WebSocketProperties wsProp, RateLimiterProperties rateLimiterProps, RateLimiter rateLimiter) {
+ this.wsProp = wsProp;
+ this.rateLimiterProps = rateLimiterProps;
+ this.rateLimiter = rateLimiter;
+ }
+
/**
* 处理响应结果
*
@@ -114,12 +106,12 @@ public JSONObject rawAction(WebSocketSession session, ActionPath action, Map params) {
JSONObject result = new JSONObject();
- if (Boolean.TRUE.equals(rateLimiterProperties.getEnable())) {
- if (Boolean.TRUE.equals(rateLimiterProperties.getAwaitTask()) && !rateLimiter.acquire()) {
+ if (Boolean.TRUE.equals(rateLimiterProps.getEnable())) {
+ if (Boolean.TRUE.equals(rateLimiterProps.getAwaitTask()) && !rateLimiter.acquire()) {
// 阻塞当前线程直到获取令牌成功
return result;
}
- if (Boolean.TRUE.equals(!rateLimiterProperties.getAwaitTask()) && !rateLimiter.tryAcquire()) {
+ if (Boolean.TRUE.equals(!rateLimiterProps.getAwaitTask()) && !rateLimiter.tryAcquire()) {
return result;
}
}
diff --git a/src/main/java/com/mikuac/shiro/handler/EventHandler.java b/src/main/java/com/mikuac/shiro/handler/EventHandler.java
index db9412a8..e62fe108 100644
--- a/src/main/java/com/mikuac/shiro/handler/EventHandler.java
+++ b/src/main/java/com/mikuac/shiro/handler/EventHandler.java
@@ -23,38 +23,20 @@
@Component
public class EventHandler implements ApplicationRunner {
- private MetaEvent meta;
+ private final MetaEvent meta;
+ private final NoticeEvent notice;
+ private final NotifyEvent notify;
+ private final MessageEvent message;
+ private final RequestEvent request;
@Autowired
- public void setMeta(MetaEvent meta) {
+ public EventHandler(
+ MetaEvent meta, NoticeEvent notice, NotifyEvent notify, MessageEvent message, RequestEvent request
+ ) {
this.meta = meta;
- }
-
- private NoticeEvent notice;
-
- @Autowired
- public void setNotice(NoticeEvent notice) {
this.notice = notice;
- }
-
- private NotifyEvent notify;
-
- @Autowired
- public void setNotify(NotifyEvent notify) {
this.notify = notify;
- }
-
- private MessageEvent message;
-
- @Autowired
- public void setMessage(MessageEvent message) {
this.message = message;
- }
-
- private RequestEvent request;
-
- @Autowired
- public void setRequest(RequestEvent request) {
this.request = request;
}
diff --git a/src/main/java/com/mikuac/shiro/handler/event/MessageEvent.java b/src/main/java/com/mikuac/shiro/handler/event/MessageEvent.java
index b113a744..1267f630 100644
--- a/src/main/java/com/mikuac/shiro/handler/event/MessageEvent.java
+++ b/src/main/java/com/mikuac/shiro/handler/event/MessageEvent.java
@@ -28,21 +28,18 @@
@Component
public class MessageEvent {
- private EventUtils utils;
- private ShiroProperties shiroProperties;
- private BotContainer botContainer;
+ private final EventUtils utils;
+ private final ShiroProperties shiroProperties;
+ private final BotContainer botContainer;
+ private final InjectionHandler injection;
@Autowired
- public void setUtils(EventUtils utils, ShiroProperties shiroProperties, BotContainer botContainer) {
+ public MessageEvent(
+ EventUtils utils, ShiroProperties shiroProperties, BotContainer botContainer, InjectionHandler injection
+ ) {
this.utils = utils;
this.shiroProperties = shiroProperties;
this.botContainer = botContainer;
- }
-
- private InjectionHandler injection;
-
- @Autowired
- public void setInjection(InjectionHandler injection) {
this.injection = injection;
}
diff --git a/src/main/java/com/mikuac/shiro/handler/event/MetaEvent.java b/src/main/java/com/mikuac/shiro/handler/event/MetaEvent.java
index 6c06607f..0e9a7983 100644
--- a/src/main/java/com/mikuac/shiro/handler/event/MetaEvent.java
+++ b/src/main/java/com/mikuac/shiro/handler/event/MetaEvent.java
@@ -21,11 +21,11 @@
@SuppressWarnings("unused")
public class MetaEvent {
- private InjectionHandler injection;
+ private final InjectionHandler injection;
@Autowired
- public void setInjection(InjectionHandler injection) {
- this.injection = injection;
+ public MetaEvent(InjectionHandler injectionHandler) {
+ this.injection = injectionHandler;
}
/**
diff --git a/src/main/java/com/mikuac/shiro/handler/event/NoticeEvent.java b/src/main/java/com/mikuac/shiro/handler/event/NoticeEvent.java
index 9daa05a4..97bfc1c2 100644
--- a/src/main/java/com/mikuac/shiro/handler/event/NoticeEvent.java
+++ b/src/main/java/com/mikuac/shiro/handler/event/NoticeEvent.java
@@ -20,25 +20,15 @@
@Component
public class NoticeEvent {
- private EventUtils utils;
+ private final EventUtils utils;
+ private final NotifyEvent notify;
+ private final InjectionHandler injection;
@Autowired
- public void setUtils(EventUtils utils) {
- this.utils = utils;
- }
-
- private InjectionHandler injection;
-
- @Autowired
- public void setInjection(InjectionHandler injection) {
- this.injection = injection;
- }
-
- private NotifyEvent notify;
-
- @Autowired
- public void setNotify(NotifyEvent notify) {
- this.notify = notify;
+ public NoticeEvent(EventUtils eventUtils, NotifyEvent notifyEvent, InjectionHandler injectionHandler) {
+ this.utils = eventUtils;
+ this.notify = notifyEvent;
+ this.injection = injectionHandler;
}
/**
diff --git a/src/main/java/com/mikuac/shiro/handler/event/NotifyEvent.java b/src/main/java/com/mikuac/shiro/handler/event/NotifyEvent.java
index dabc1c33..3e3bf615 100644
--- a/src/main/java/com/mikuac/shiro/handler/event/NotifyEvent.java
+++ b/src/main/java/com/mikuac/shiro/handler/event/NotifyEvent.java
@@ -22,17 +22,12 @@
@Component
public class NotifyEvent {
- private EventUtils utils;
+ private final EventUtils utils;
+ private final InjectionHandler injection;
@Autowired
- public void setUtils(EventUtils utils) {
+ public NotifyEvent(EventUtils utils, InjectionHandler injection) {
this.utils = utils;
- }
-
- private InjectionHandler injection;
-
- @Autowired
- public void setInjection(InjectionHandler injection) {
this.injection = injection;
}
diff --git a/src/main/java/com/mikuac/shiro/handler/event/RequestEvent.java b/src/main/java/com/mikuac/shiro/handler/event/RequestEvent.java
index 24ad8199..cfce122b 100644
--- a/src/main/java/com/mikuac/shiro/handler/event/RequestEvent.java
+++ b/src/main/java/com/mikuac/shiro/handler/event/RequestEvent.java
@@ -21,11 +21,13 @@
@Component
public class RequestEvent {
- private EventUtils utils;
+ private final EventUtils utils;
+ private final InjectionHandler injection;
@Autowired
- public void setUtils(EventUtils utils) {
- this.utils = utils;
+ public RequestEvent(EventUtils eventUtils, InjectionHandler injectionHandler) {
+ this.utils = eventUtils;
+ this.injection = injectionHandler;
}
/**
@@ -33,13 +35,6 @@ public void setUtils(EventUtils utils) {
*/
public final Map> handlers = new HashMap<>();
- private InjectionHandler injection;
-
- @Autowired
- public void setInjection(InjectionHandler injection) {
- this.injection = injection;
- }
-
/**
* 请求事件分发
*
diff --git a/src/main/java/com/mikuac/shiro/task/ScheduledTask.java b/src/main/java/com/mikuac/shiro/task/ScheduledTask.java
new file mode 100644
index 00000000..6438a892
--- /dev/null
+++ b/src/main/java/com/mikuac/shiro/task/ScheduledTask.java
@@ -0,0 +1,33 @@
+package com.mikuac.shiro.task;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+import org.springframework.stereotype.Component;
+
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+
+@Component
+public class ScheduledTask {
+
+ private final ThreadPoolTaskExecutor shiroTaskExecutor;
+
+ @Autowired
+ public ScheduledTask(ThreadPoolTaskExecutor shiroTaskExecutor) {
+ this.shiroTaskExecutor = shiroTaskExecutor;
+ }
+
+ private ScheduledThreadPoolExecutor executor;
+
+ public ScheduledThreadPoolExecutor executor() {
+ if (executor != null) {
+ return executor;
+ }
+ executor = new ScheduledThreadPoolExecutor(
+ shiroTaskExecutor.getCorePoolSize(),
+ shiroTaskExecutor.getThreadPoolExecutor().getThreadFactory()
+ );
+ executor.setRemoveOnCancelPolicy(true);
+ return executor;
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/com/mikuac/shiro/task/ShiroAsyncTask.java b/src/main/java/com/mikuac/shiro/task/ShiroAsyncTask.java
index e591a4a2..c74b08df 100644
--- a/src/main/java/com/mikuac/shiro/task/ShiroAsyncTask.java
+++ b/src/main/java/com/mikuac/shiro/task/ShiroAsyncTask.java
@@ -17,10 +17,10 @@
@Component
public class ShiroAsyncTask {
- private BotContainer botContainer;
+ private final BotContainer botContainer;
@Autowired
- public void setBotContainer(BotContainer botContainer) {
+ public ShiroAsyncTask(BotContainer botContainer) {
this.botContainer = botContainer;
}
diff --git a/src/main/java/com/mikuac/shiro/task/ShiroTaskPoolConfig.java b/src/main/java/com/mikuac/shiro/task/ShiroTaskPoolConfig.java
index 64955dd6..73f6e983 100644
--- a/src/main/java/com/mikuac/shiro/task/ShiroTaskPoolConfig.java
+++ b/src/main/java/com/mikuac/shiro/task/ShiroTaskPoolConfig.java
@@ -18,10 +18,10 @@
@Configuration
public class ShiroTaskPoolConfig {
- private TaskPoolProperties taskPoolProperties;
+ private final TaskPoolProperties taskPoolProperties;
@Autowired
- public void setTaskPoolProperties(TaskPoolProperties taskPoolProperties) {
+ public ShiroTaskPoolConfig(TaskPoolProperties taskPoolProperties) {
this.taskPoolProperties = taskPoolProperties;
}