From 0c43e2956b8e22878db9ab91ab04c917d7f4ba17 Mon Sep 17 00:00:00 2001 From: wayn <1669738430@qq.com> Date: Sat, 4 Dec 2021 20:10:22 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=95=86=E5=9F=8E):=20=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 优化首页接口响应速度 2. 优化商品详情接口响应速度 3. 优化分类接口响应速度 4. 优化注释 --- .../wayn/common/util/AsyncExecutorUtil.java | 36 ------------------- .../util/{Threads.java => ThreadUtil.java} | 4 +-- .../api/controller/SearchController.java | 16 ++++----- .../framework/config/ThreadPoolConfig.java | 6 ++-- .../manager/thread/AsyncManager.java | 4 +-- .../manager/thread/ShutdownManager.java | 10 +++--- .../service/UserDetailsServiceImpl.java | 12 ++++--- 7 files changed, 29 insertions(+), 59 deletions(-) delete mode 100644 waynboot-common/src/main/java/com/wayn/common/util/AsyncExecutorUtil.java rename waynboot-common/src/main/java/com/wayn/common/util/{Threads.java => ThreadUtil.java} (97%) diff --git a/waynboot-common/src/main/java/com/wayn/common/util/AsyncExecutorUtil.java b/waynboot-common/src/main/java/com/wayn/common/util/AsyncExecutorUtil.java deleted file mode 100644 index 7f21210..0000000 --- a/waynboot-common/src/main/java/com/wayn/common/util/AsyncExecutorUtil.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.wayn.common.util; - -import java.util.Date; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -/** - * 异步执行帮助类 - */ -public class AsyncExecutorUtil { - - private static Timer timer; - - static { - timer = new Timer("timer-executor-thread"); - } - - - public static void executor(Runnable r) { - ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>()); - executor.execute(r); - executor.shutdown(); - } - - public static void scheduled(TimerTask task, Date date) { - timer.scheduleAtFixedRate(task, date, 10000); - } - - - public static void timerDestroyed() { - timer.cancel(); - } -} diff --git a/waynboot-common/src/main/java/com/wayn/common/util/Threads.java b/waynboot-common/src/main/java/com/wayn/common/util/ThreadUtil.java similarity index 97% rename from waynboot-common/src/main/java/com/wayn/common/util/Threads.java rename to waynboot-common/src/main/java/com/wayn/common/util/ThreadUtil.java index 70a6853..f8af3a0 100644 --- a/waynboot-common/src/main/java/com/wayn/common/util/Threads.java +++ b/waynboot-common/src/main/java/com/wayn/common/util/ThreadUtil.java @@ -9,8 +9,8 @@ import java.util.concurrent.*; * 线程相关工具类. * */ -public class Threads { - private static final Logger logger = LoggerFactory.getLogger(Threads.class); +public class ThreadUtil { + private static final Logger logger = LoggerFactory.getLogger(ThreadUtil.class); /** * sleep等待,单位为毫秒 diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java index c6275b0..82d28b0 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/api/controller/SearchController.java @@ -11,11 +11,11 @@ import com.wayn.common.core.domain.shop.Keyword; import com.wayn.common.core.domain.vo.SearchVO; import com.wayn.common.core.service.shop.IGoodsService; import com.wayn.common.core.service.shop.IKeywordService; -import com.wayn.common.util.AsyncExecutorUtil; import com.wayn.common.util.R; import com.wayn.data.elastic.manager.ElasticDocument; import com.wayn.mobile.api.domain.SearchHistory; import com.wayn.mobile.api.service.ISearchHistoryService; +import com.wayn.mobile.framework.manager.thread.AsyncManager; import com.wayn.mobile.framework.security.util.MobileSecurityUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -35,10 +35,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.validation.constraints.NotEmpty; import java.io.IOException; import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -135,9 +132,12 @@ public class SearchController extends BaseController { returnGoodsList.add(goodsMap.get(goodsId)); } if (CollectionUtils.isNotEmpty(goodsList)) { - AsyncExecutorUtil.executor(() -> { - searchHistory.setHasGoods(true); - iSearchHistoryService.save(searchHistory); + AsyncManager.me().execute(new TimerTask() { + @Override + public void run() { + searchHistory.setHasGoods(true); + iSearchHistoryService.save(searchHistory); + } }); } return R.success().add("goods", returnGoodsList); diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/ThreadPoolConfig.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/ThreadPoolConfig.java index ad11792..dda70f1 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/ThreadPoolConfig.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/config/ThreadPoolConfig.java @@ -1,6 +1,7 @@ package com.wayn.mobile.framework.config; -import com.wayn.common.util.Threads; +import com.wayn.common.util.ThreadUtil; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -13,6 +14,7 @@ import java.util.concurrent.ThreadPoolExecutor; /** * 线程池配置,各系统分开使用 **/ +@Slf4j @Configuration public class ThreadPoolConfig { // 核心线程池大小 @@ -62,7 +64,7 @@ public class ThreadPoolConfig { @Override protected void afterExecute(Runnable r, Throwable t) { super.afterExecute(r, t); - Threads.printException(r, t); + ThreadUtil.printException(r, t); } }; } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/AsyncManager.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/AsyncManager.java index e2c096f..f17a992 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/AsyncManager.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/AsyncManager.java @@ -1,7 +1,7 @@ package com.wayn.mobile.framework.manager.thread; -import com.wayn.common.util.Threads; +import com.wayn.common.util.ThreadUtil; import com.wayn.common.util.spring.SpringContextUtil; import java.util.TimerTask; @@ -57,6 +57,6 @@ public class AsyncManager { * 停止任务线程池 */ public void shutdown() { - Threads.shutdownAndAwaitTermination(executor); + ThreadUtil.shutdownAndAwaitTermination(executor); } } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/ShutdownManager.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/ShutdownManager.java index e1f514a..eb84182 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/ShutdownManager.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/manager/thread/ShutdownManager.java @@ -1,7 +1,7 @@ package com.wayn.mobile.framework.manager.thread; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.wayn.common.constant.Constants; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.PreDestroy; @@ -11,9 +11,9 @@ import javax.annotation.PreDestroy; * * @author ruoyi */ +@Slf4j @Component public class ShutdownManager { - private static final Logger logger = LoggerFactory.getLogger("sys-user"); @PreDestroy public void destroy() { @@ -25,10 +25,10 @@ public class ShutdownManager { */ private void shutdownAsyncManager() { try { - logger.info("====关闭后台任务任务线程池===="); + log.info(Constants.LOG_PREFIX + "AsyncManager shutdown" + Constants.LOG_PREFIX); AsyncManager.me().shutdown(); } catch (Exception e) { - logger.error(e.getMessage(), e); + log.error(e.getMessage(), e); } } } diff --git a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/service/UserDetailsServiceImpl.java b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/service/UserDetailsServiceImpl.java index a2b01a8..7a8a894 100644 --- a/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/service/UserDetailsServiceImpl.java +++ b/waynboot-mobile-api/src/main/java/com/wayn/mobile/framework/security/service/UserDetailsServiceImpl.java @@ -4,9 +4,9 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.wayn.common.core.domain.shop.Member; import com.wayn.common.core.service.shop.IMemberService; import com.wayn.common.enums.UserStatusEnum; -import com.wayn.common.util.AsyncExecutorUtil; import com.wayn.common.util.ip.IpUtils; import com.wayn.mobile.framework.config.ThreadPoolConfig; +import com.wayn.mobile.framework.manager.thread.AsyncManager; import com.wayn.mobile.framework.security.LoginUserDetail; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; @@ -18,6 +18,7 @@ import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.Collections; +import java.util.TimerTask; @Slf4j @Service @@ -41,13 +42,16 @@ public class UserDetailsServiceImpl implements UserDetailsService { throw new DisabledException("登录用户:" + mobile + " 不存在"); } // 记录最后一次登陆时间以及登陆IP - AsyncExecutorUtil.executor(() -> + AsyncManager.me().execute(new TimerTask() { + @Override + public void run() { iMemberService.update() .set("last_login_time", LocalDateTime.now()) .set("last_login_ip", IpUtils.getHostIp()) .eq("id", member.getId()) - .update() - ); + .update(); + } + }); return new LoginUserDetail(member, Collections.emptySet()); }