From ee3036e477f9bf6d42456dfbcbe6444c46dbb115 Mon Sep 17 00:00:00 2001 From: zhc077 <565291854@qq.com> Date: Sat, 23 Nov 2024 17:17:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E6=A1=A3=E7=94=9F=E6=88=90=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=2011.23?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../project/controller/ProjectController.java | 1 + .../service/impl/Project4WordServiceImpl.java | 14 +++++ .../modules/system/config/AsyncExecutor.java | 54 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/config/AsyncExecutor.java diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/controller/ProjectController.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/controller/ProjectController.java index 2482889..970d5b9 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/controller/ProjectController.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/controller/ProjectController.java @@ -610,6 +610,7 @@ public class ProjectController extends JeecgController log.setOperationMark("申请人提交项目书,申请人提交预算书"); //提交项目时,生成项目书pdf文件 +// System.out.println("提交项目-the thread exec end:" + Thread.currentThread().getName()); iProject4WordService.createXiangMuShu4Word(project); } //申请单位审核通过,并向上级提交->科技主管部门待提交(区/县管理员) diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java index c516eac..983a675 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/project/service/impl/Project4WordServiceImpl.java @@ -35,6 +35,7 @@ import org.jeecg.modules.system.service.impl.SysBaseApiImpl; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; @@ -257,7 +258,16 @@ public class Project4WordServiceImpl extends ServiceImpl private String uploadPath;*/ @Override + @Async("getAsyncExecutorMethod") public Object createXiangMuShu4Word(Project obj) { + + log.info("---------createXiangMuShu4Word start,the thread name: {}", Thread.currentThread().getName()); + try { + Thread.sleep(3000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + long startTime = System.currentTimeMillis(); ProjectDto dto = getData4Xiangmushu(obj); if (ObjectUtil.isEmpty(dto)) { return null; @@ -326,6 +336,9 @@ public class Project4WordServiceImpl extends ServiceImpl } catch (Exception e2) { e2.printStackTrace(); } + + long s = (System.currentTimeMillis() - startTime) / 1000; + log.info("---------createXiangMuShu4Word,the thread name:{},生成pdf文件用时: {}", Thread.currentThread().getName(), s + "秒"); return dto; } @@ -333,6 +346,7 @@ public class Project4WordServiceImpl extends ServiceImpl public Object createPdf(String wordPath, File file, String padFileName) { if (wordPath.equals("")) { log.error("---------------word文件路径不能为空!"); + return null; } else { try { // FileUtils.convertDocxToPdf(wordPath, file + File.separator + padFileName); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/config/AsyncExecutor.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/config/AsyncExecutor.java new file mode 100644 index 0000000..85c73e1 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/config/AsyncExecutor.java @@ -0,0 +1,54 @@ +package org.jeecg.modules.system.config; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +import java.util.concurrent.ThreadPoolExecutor; + + +/** + * @Description: TODO + * @Author: Z.H.C + * @CreateTime: 2024-11-23 16:52 + * @Version: 1.0 + */ + +@Configuration +@EnableAsync +public class AsyncExecutor { + + private static final Logger logger = LoggerFactory.getLogger(AsyncExecutor.class); + + //核心线程数 + private static final int CORE_POOL_SIZE = 5; + + //最大线程数 + private static final int MAX_POOL_SIZE = 5; + + //队列大小 + private static final int QUEUE_CAPACITY = 30; + + //线程池中的线程的名称前缀 + private static final String THREAD_NAME = "MyExecutor-"; + + @Bean + public ThreadPoolTaskExecutor getAsyncExecutorMethod() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + //配置核心线程数 + executor.setCorePoolSize(CORE_POOL_SIZE); + //配置最大线程数 + executor.setMaxPoolSize(MAX_POOL_SIZE); + //配置队列大小 + executor.setQueueCapacity(QUEUE_CAPACITY); + //配置线程池中的线程的名称前缀 + executor.setThreadNamePrefix(THREAD_NAME); + //配置线程池拒绝策略,我设置为CallerRunsPolicy,当线程和队列都满了,由发起线程的主线程自己执行 + executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); + executor.initialize(); + return executor; + } +}