From 17f2e105faa08f67e11615308999d7d0648f2048 Mon Sep 17 00:00:00 2001 From: baoge <751575283@qq.com> Date: Wed, 12 Jul 2023 11:18:47 +0800 Subject: [PATCH] =?UTF-8?q?opt:=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E8=B0=83=E8=AF=95=E4=BF=A1=E6=81=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/nl/agv/service/HomeService.java | 6 +++ .../agv/service/impl/DevelopServiceImpl.java | 8 ++-- .../nl/agv/service/impl/ErrorServiceImpl.java | 4 +- .../nl/agv/service/impl/HomeServiceImpl.java | 13 +++--- .../service/impl/TaskServiceServiceImpl.java | 4 +- .../service/impl/VehicleInfoServiceImpl.java | 8 ++-- .../main/java/org/nl/agv/unit/RosUtil.java | 8 ++-- .../org/nl/common/utils/OptionRecord.java | 1 + .../config/thread/AsyncTaskExecutePool.java | 45 ++++++++++++++++--- .../nl/config/thread/AsyncTaskProperties.java | 5 ++- .../config/thread/ThreadPoolExecutorUtil.java | 16 ++++--- .../service/quartz/utils/ExecutionJob.java | 1 + .../secutiry/impl/OnlineUserService.java | 20 +++++++++ 13 files changed, 103 insertions(+), 36 deletions(-) diff --git a/nladmin-system/src/main/java/org/nl/agv/service/HomeService.java b/nladmin-system/src/main/java/org/nl/agv/service/HomeService.java index 6213814..bc96065 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/HomeService.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/HomeService.java @@ -9,6 +9,8 @@ import java.util.Map; * @date 2021-09-02 **/ public interface HomeService { + + /** * 查询头部 * @@ -31,4 +33,8 @@ public interface HomeService { */ Map clickSave(Map jsonObject); + + void qeryDebuggerInfo(); + + } diff --git a/nladmin-system/src/main/java/org/nl/agv/service/impl/DevelopServiceImpl.java b/nladmin-system/src/main/java/org/nl/agv/service/impl/DevelopServiceImpl.java index 335b5d3..fb49652 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/impl/DevelopServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/impl/DevelopServiceImpl.java @@ -100,10 +100,10 @@ public class DevelopServiceImpl implements DevelopService { @Override public Map debugInfo(Map jsonObject) { //先执行这个方法确保debugger有数据 - HomeServiceImpl impl = new HomeServiceImpl(); - do { - impl.QueryDebuggerInfo(); - } while (ObjectUtil.isEmpty(HomeServiceImpl.debugInfoJson)); + // HomeServiceImpl impl = new HomeServiceImpl(); + // do { + // impl.queryDebuggerInfo(); + // } while (ObjectUtil.isEmpty(HomeServiceImpl.debugInfoJson)); JSONObject returnjo = new JSONObject(); JSONObject map = HomeServiceImpl.debugInfoJson; Iterator iter = map.entrySet().iterator(); diff --git a/nladmin-system/src/main/java/org/nl/agv/service/impl/ErrorServiceImpl.java b/nladmin-system/src/main/java/org/nl/agv/service/impl/ErrorServiceImpl.java index 873522a..15c7f3f 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/impl/ErrorServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/impl/ErrorServiceImpl.java @@ -169,8 +169,8 @@ public class ErrorServiceImpl implements ErrorService{ return returnjo; } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException( e.getMessage()==null?"Ros通信失败,请检查网络连接":e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException( e.getMessage()==null?"通信失败,请检查系统参数中的Ros连接信息":e.getMessage()); } } } \ No newline at end of file diff --git a/nladmin-system/src/main/java/org/nl/agv/service/impl/HomeServiceImpl.java b/nladmin-system/src/main/java/org/nl/agv/service/impl/HomeServiceImpl.java index f5b8bbb..d3e8e26 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/impl/HomeServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/impl/HomeServiceImpl.java @@ -51,7 +51,8 @@ public class HomeServiceImpl implements HomeService{ //定时器查询调试信息,放入json其他地方直接调用; public static JSONObject debugInfoJson = new JSONObject(); - public void QueryDebuggerInfo() { + @Override + public void qeryDebuggerInfo() { try { Map urlInfo = RosUtil.getRosHostInfo(); String result1 = HttpUtil.get(urlInfo.get("javaUrl")); @@ -123,8 +124,8 @@ public class HomeServiceImpl implements HomeService{ debugInfoJson.put("PathFollow_Enable", PathFollow_Enable); } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException(e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException("通信失败,请检查系统参数中的java端口信息"); } } @@ -244,7 +245,7 @@ public class HomeServiceImpl implements HomeService{ } @Override - public Map queryHomePage(Map jsonObject) { + public MapqueryHomePage(Map jsonObject) { String button_code = ""; String button_name = ""; Integer electric = 0; @@ -359,8 +360,8 @@ public class HomeServiceImpl implements HomeService{ return returnjo; } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException( e.getMessage()==null?"Ros通信失败,请检查网络连接":e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException( e.getMessage()==null?"通信失败,请检查系统参数中的Ros连接信息":e.getMessage()); } } diff --git a/nladmin-system/src/main/java/org/nl/agv/service/impl/TaskServiceServiceImpl.java b/nladmin-system/src/main/java/org/nl/agv/service/impl/TaskServiceServiceImpl.java index 1e42bf8..4fe3cfe 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/impl/TaskServiceServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/impl/TaskServiceServiceImpl.java @@ -88,8 +88,8 @@ public class TaskServiceServiceImpl implements TaskService{ return returnjo; } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException( e.getMessage()==null?"Ros通信失败,请检查网络连接":e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException( e.getMessage()==null?"通信失败,请检查系统参数中的Ros连接信息":e.getMessage()); } } diff --git a/nladmin-system/src/main/java/org/nl/agv/service/impl/VehicleInfoServiceImpl.java b/nladmin-system/src/main/java/org/nl/agv/service/impl/VehicleInfoServiceImpl.java index 7027e7f..6cf704c 100644 --- a/nladmin-system/src/main/java/org/nl/agv/service/impl/VehicleInfoServiceImpl.java +++ b/nladmin-system/src/main/java/org/nl/agv/service/impl/VehicleInfoServiceImpl.java @@ -205,8 +205,8 @@ public class VehicleInfoServiceImpl implements VehicleInfoService{ returnjo.put("result", row); return returnjo; } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException( e.getMessage()==null?"Ros通信失败,请检查网络连接":e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException( e.getMessage()==null?"通信失败,请检查系统参数中的Ros连接信息":e.getMessage()); } } @@ -287,8 +287,8 @@ public class VehicleInfoServiceImpl implements VehicleInfoService{ returnjo.put("result", row); return returnjo; } catch(Exception e) { - Log.error("获取调试信息失败:" + e.getMessage()); - throw new BadRequestException( e.getMessage()==null?"Ros通信失败,请检查网络连接":e.getMessage()); + Log.error("通信失败:" + e.getMessage()); + throw new BadRequestException( e.getMessage()==null?"通信失败,请检查系统参数中的Ros连接信息":e.getMessage()); } } diff --git a/nladmin-system/src/main/java/org/nl/agv/unit/RosUtil.java b/nladmin-system/src/main/java/org/nl/agv/unit/RosUtil.java index 2f555c4..340c963 100644 --- a/nladmin-system/src/main/java/org/nl/agv/unit/RosUtil.java +++ b/nladmin-system/src/main/java/org/nl/agv/unit/RosUtil.java @@ -35,11 +35,11 @@ public class RosUtil{ public static Map getRosHostInfo() { Map map = new HashMap<>(); try { - String ros_host = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode("ros_host").getValue(); - String ros_port = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode("ros_port").getValue(); + String rosHost = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode("ros_host").getValue(); + String rosPort = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode("ros_port").getValue(); String javaPort = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode("java_port").getValue(); - map.put("hostName", ros_host); - map.put("port", ros_port); + map.put("hostName", rosHost); + map.put("port", rosPort); map.put("javaPort", javaPort); map.put("url", map.get("hostName") + ":" + map.get("port")); map.put("javaUrl", "http://"+map.get("hostName") + ":" + map.get("javaPort")); diff --git a/nladmin-system/src/main/java/org/nl/common/utils/OptionRecord.java b/nladmin-system/src/main/java/org/nl/common/utils/OptionRecord.java index dd2c6e1..b6935d9 100644 --- a/nladmin-system/src/main/java/org/nl/common/utils/OptionRecord.java +++ b/nladmin-system/src/main/java/org/nl/common/utils/OptionRecord.java @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.AllArgsConstructor; import lombok.Getter; +import org.nl.config.thread.AsyncTaskExecutePool; import org.nl.config.thread.ThreadPoolExecutorUtil; import org.nl.modules.wql.util.SpringContextHolder; import org.nl.system.service.ql.IPdmQlOptionService; diff --git a/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskExecutePool.java b/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskExecutePool.java index c64e314..a173056 100644 --- a/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskExecutePool.java +++ b/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskExecutePool.java @@ -17,8 +17,10 @@ package org.nl.config.thread; import lombok.extern.slf4j.Slf4j; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; +import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; @@ -26,14 +28,17 @@ import java.util.concurrent.ThreadPoolExecutor; /** * 异步任务线程池装配类 + * * @author https://juejin.im/entry/5abb8f6951882555677e9da2 * @date 2019年10月31日15:06:18 */ @Slf4j @Configuration -public class AsyncTaskExecutePool implements AsyncConfigurer { - - /** 注入配置类 */ +@EnableAsync +public class AsyncTaskExecutePool implements AsyncConfigurer{ + /** + * 注入配置类 + */ private final AsyncTaskProperties config; public AsyncTaskExecutePool(AsyncTaskProperties config) { @@ -60,11 +65,41 @@ public class AsyncTaskExecutePool implements AsyncConfigurer { return executor; } + /** + * 线程池配置 + * @return java.util.concurrent.Executor + * @author gbx + * @since 2023-06-16 + */ + @Bean(name = "taskExecutor") + public ThreadPoolTaskExecutor threadPoolTaskExecutor() { + ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); + // 核心线程池大小 + threadPoolTaskExecutor.setCorePoolSize(config.getCorePoolSize()); + // 最大线程数 + threadPoolTaskExecutor.setMaxPoolSize(config.getMaxPoolSize()); + // 队列容量 + threadPoolTaskExecutor.setQueueCapacity(config.getQueueCapacity()); + // 活跃时间 + threadPoolTaskExecutor.setKeepAliveSeconds(config.getKeepAliveSeconds()); + // 主线程等待子线程执行时间 + threadPoolTaskExecutor.setAwaitTerminationSeconds(config.getAwaitTerminationSeconds()); + // threadPoolTaskExecutor.setAwaitTerminationSeconds(30); + // 线程名字前缀 + threadPoolTaskExecutor.setThreadNamePrefix("test-thread-"); + // RejectedExecutionHandler:当pool已经达到max-size的时候,如何处理新任务 + // CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行 + threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + // 初始化 + threadPoolTaskExecutor.initialize(); + return threadPoolTaskExecutor; + } + @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return (throwable, method, objects) -> { - log.error("===="+throwable.getMessage()+"====", throwable); - log.error("exception method:"+method.getName()); + log.error("====" + throwable.getMessage() + "====", throwable); + log.error("exception method:" + method.getName()); }; } } diff --git a/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskProperties.java b/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskProperties.java index bba9114..7ac5a1e 100644 --- a/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskProperties.java +++ b/nladmin-system/src/main/java/org/nl/config/thread/AsyncTaskProperties.java @@ -16,8 +16,6 @@ package org.nl.config.thread; import lombok.Data; -import java.util.Date; - import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -38,4 +36,7 @@ public class AsyncTaskProperties { private int keepAliveSeconds; private int queueCapacity; + + private int awaitTerminationSeconds; + } diff --git a/nladmin-system/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java b/nladmin-system/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java index 5624f54..cd36ba4 100644 --- a/nladmin-system/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java +++ b/nladmin-system/src/main/java/org/nl/config/thread/ThreadPoolExecutorUtil.java @@ -15,7 +15,8 @@ */ package org.nl.config.thread; -import org.nl.modules.wql.util.SpringContextHolder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -26,16 +27,17 @@ import java.util.concurrent.TimeUnit; * @author Zheng Jie * @date 2019年10月31日18:16:47 */ +@Configuration public class ThreadPoolExecutorUtil { - public static ThreadPoolExecutor getPoll(){ - AsyncTaskProperties properties = SpringContextHolder.getBean(AsyncTaskProperties.class); + @Bean(name = "threadPool") + public static ThreadPoolExecutor getPoll(){ return new ThreadPoolExecutor( - properties.getCorePoolSize(), - properties.getMaxPoolSize(), - properties.getKeepAliveSeconds(), + 8, + 32, + 30, TimeUnit.SECONDS, - new ArrayBlockingQueue<>(properties.getQueueCapacity()), + new ArrayBlockingQueue<>(128), new TheadFactoryName() ); } diff --git a/nladmin-system/src/main/java/org/nl/system/service/quartz/utils/ExecutionJob.java b/nladmin-system/src/main/java/org/nl/system/service/quartz/utils/ExecutionJob.java index 9a28980..40ecfb0 100644 --- a/nladmin-system/src/main/java/org/nl/system/service/quartz/utils/ExecutionJob.java +++ b/nladmin-system/src/main/java/org/nl/system/service/quartz/utils/ExecutionJob.java @@ -3,6 +3,7 @@ package org.nl.system.service.quartz.utils; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import lombok.extern.slf4j.Slf4j; +import org.nl.config.thread.AsyncTaskExecutePool; import org.nl.config.thread.ThreadPoolExecutorUtil; import org.nl.modules.common.utils.RedisUtils; import org.nl.modules.common.utils.ThrowableUtil; diff --git a/nladmin-system/src/main/java/org/nl/system/service/secutiry/impl/OnlineUserService.java b/nladmin-system/src/main/java/org/nl/system/service/secutiry/impl/OnlineUserService.java index f28366a..5a1259d 100644 --- a/nladmin-system/src/main/java/org/nl/system/service/secutiry/impl/OnlineUserService.java +++ b/nladmin-system/src/main/java/org/nl/system/service/secutiry/impl/OnlineUserService.java @@ -18,12 +18,15 @@ package org.nl.system.service.secutiry.impl; import cn.dev33.satoken.secure.SaSecureUtil; import cn.dev33.satoken.stp.SaLoginModel; import cn.dev33.satoken.stp.StpUtil; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.nl.agv.service.HomeService; +import org.nl.agv.service.impl.HomeServiceImpl; import org.nl.modules.common.config.RsaProperties; import org.nl.modules.common.exception.BadRequestException; import org.nl.modules.common.utils.*; @@ -35,15 +38,19 @@ import org.nl.system.service.user.dto.CurrentUser; import org.nl.system.service.user.dto.OnlineUserDto; import org.nl.system.service.user.dto.UserDto; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.data.domain.Pageable; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ThreadPoolExecutor; /** * @author Zheng Jie @@ -52,10 +59,17 @@ import java.util.*; @Service @Slf4j public class OnlineUserService{ + + @Resource + @Qualifier("threadPool") + private ThreadPoolExecutor pool; @Autowired private ISysUserService sysUserService; @Autowired private ISysRoleService roleService; + @Autowired + private HomeService homeService; + private final RedisUtils redisUtils; @Value("${sa-token.cookie.domain}") private String domain; @@ -225,6 +239,8 @@ public class OnlineUserService{ @SneakyThrows public Map login(Map paramMap) { + //查询调试信息 + CompletableFuture.runAsync(this::queryDebugInfo, pool); // 密码解密 - 前端的加密规则: encrypt AuthUserDto authUser = JSON.toJavaObject((JSON) JSON.toJSON(paramMap), AuthUserDto.class); String password = RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, authUser.getPassword()); @@ -271,4 +287,8 @@ public class OnlineUserService{ //onlineUserService.save(userDto, StpUtil.getTokenValue(), request); return authInfo; } + + public void queryDebugInfo() { + homeService.qeryDebuggerInfo(); + } }