Skip to content

Commit

Permalink
🎨 Optimized autowired
Browse files Browse the repository at this point in the history
  • Loading branch information
MisakaTAT committed Jun 7, 2024
1 parent 8f47ca9 commit 4fb60fb
Show file tree
Hide file tree
Showing 18 changed files with 210 additions and 332 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@file:Suppress("SpellCheckingInspection")

group = "com.mikuac"
version = "2.2.7"
version = "9.9.9"

plugins {
signing
Expand Down
29 changes: 9 additions & 20 deletions src/main/java/com/mikuac/shiro/adapter/WebSocketClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
63 changes: 21 additions & 42 deletions src/main/java/com/mikuac/shiro/adapter/WebSocketServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
74 changes: 29 additions & 45 deletions src/main/java/com/mikuac/shiro/boot/Shiro.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);
}

/**
* <p>createWebSocketContainer.</p>
*
* @return {@link ServletServerContainerFactoryBean}
*/
@Bean
@ConditionalOnMissingBean
public ServletServerContainerFactoryBean createWebSocketServerContainer() {
Expand Down
Loading

0 comments on commit 4fb60fb

Please sign in to comment.