From e55d98a36b89469252b6f591b430b9e7d25f04e5 Mon Sep 17 00:00:00 2001 From: "a15234804788@163.com" Date: Sun, 9 Oct 2022 16:09:42 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=B8=BA=E5=9F=B9=E8=AE=AD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8F=90=E5=89=8D=E6=8F=90=E9=86=92=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/user/UserApplication.java | 2 + .../user/controller/TrainController.java | 51 +++++++++++------- .../user/service/impl/TrainServiceImpl.java | 53 +++++++++++++++++++ .../system/user/util/ThreadPoolFactory.java | 20 +++++++ 4 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 lab-service/lab-user/src/main/java/org/springblade/system/user/util/ThreadPoolFactory.java diff --git a/lab-service/lab-user/src/main/java/org/springblade/system/user/UserApplication.java b/lab-service/lab-user/src/main/java/org/springblade/system/user/UserApplication.java index ae5ca69..834be4d 100644 --- a/lab-service/lab-user/src/main/java/org/springblade/system/user/UserApplication.java +++ b/lab-service/lab-user/src/main/java/org/springblade/system/user/UserApplication.java @@ -5,6 +5,7 @@ import org.springblade.core.cloud.feign.EnableBladeFeign; import org.springblade.core.launch.BladeApplication; import org.springblade.core.launch.constant.AppConstant; import org.springframework.cloud.client.SpringCloudApplication; +import org.springframework.scheduling.annotation.EnableAsync; /** * 用户启动器 @@ -13,6 +14,7 @@ import org.springframework.cloud.client.SpringCloudApplication; */ @EnableBladeFeign @SpringCloudApplication +@EnableAsync public class UserApplication { public static void main(String[] args) { diff --git a/lab-service/lab-user/src/main/java/org/springblade/system/user/controller/TrainController.java b/lab-service/lab-user/src/main/java/org/springblade/system/user/controller/TrainController.java index 9aa3152..d649bc6 100644 --- a/lab-service/lab-user/src/main/java/org/springblade/system/user/controller/TrainController.java +++ b/lab-service/lab-user/src/main/java/org/springblade/system/user/controller/TrainController.java @@ -1,6 +1,7 @@ package org.springblade.system.user.controller; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.AllArgsConstructor; @@ -22,17 +23,20 @@ import org.springblade.system.user.service.ITeacherService; import org.springblade.system.user.service.ITrainPersonService; import org.springblade.system.user.service.ITrainService; import org.springblade.system.user.service.ITrainSpeakService; +import org.springblade.system.user.util.ThreadPoolFactory; import org.springblade.system.user.vo.TrainVO; import org.springframework.web.bind.annotation.*; import javax.annotation.PostConstruct; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * 培训计划 控制器 @@ -49,16 +53,8 @@ public class TrainController extends BladeController { private final ITrainPersonService trainPersonService; - private ScheduledExecutorService executorService; - @PostConstruct - public void init(){ - //cpu核心数 - int i = Runtime.getRuntime().availableProcessors(); - //创建执行定时任务的线程池 - this.executorService = Executors.newScheduledThreadPool(i==0 ? 2:i*2); - } /** * 详情 */ @@ -88,7 +84,7 @@ public class TrainController extends BladeController { for (Train train1 : trains) { long time = System.currentTimeMillis(); long startTime = train1.getStartTime().getTime(); - long endTime = 1000 * 60 * train1.getDuration() + startTime; + long endTime = 1000 * 60 * train1.getDuration() + startTime; if (time >= startTime && time <= endTime) { train1.setStatus(1); } @@ -184,14 +180,33 @@ public class TrainController extends BladeController { } /** - * 测试一段时间后执行任务 + * 测试方法,测试一段时间后自动执行任务 */ -// @GetMapping("/testTimer") -// public R testTimer(Integer t){ -// executorService.schedule(new Runnable() { -// @Override -// public void run() { -// -// } -// },1000L); + @GetMapping("/test") + public void test(){ + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(Train::getId,"1578954759149441026"); + Train train = trainService.getOne(queryWrapper); + + //提前通知时间 分钟 + Integer remind = Integer.parseInt(train.getRemind()); + //会议开始时间 + Date startTime = train.getStartTime(); + //会议开始时间的毫秒数 + long time = startTime.getTime(); + //应该开始提醒的毫秒数 + long time1 = time - remind * 60 *1000; + //当前时间的毫秒数 + Long now = LocalDateTime.now().toInstant(OffsetDateTime.now().getOffset()).toEpochMilli(); + String[] s = {"小红", "小明", "小张"}; + ThreadPoolFactory.sheduleThreadPool.schedule(new Runnable() { + @Override + public void run() { + for(String str:s){ + System.out.println("1111111" + str); + } + + } + },time1 - now, TimeUnit.MILLISECONDS); + } } diff --git a/lab-service/lab-user/src/main/java/org/springblade/system/user/service/impl/TrainServiceImpl.java b/lab-service/lab-user/src/main/java/org/springblade/system/user/service/impl/TrainServiceImpl.java index 56dfef7..f6b4c29 100644 --- a/lab-service/lab-user/src/main/java/org/springblade/system/user/service/impl/TrainServiceImpl.java +++ b/lab-service/lab-user/src/main/java/org/springblade/system/user/service/impl/TrainServiceImpl.java @@ -1,6 +1,7 @@ package org.springblade.system.user.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import io.swagger.models.auth.In; import lombok.AllArgsConstructor; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.secure.utils.AuthUtil; @@ -16,12 +17,19 @@ import org.springblade.system.user.service.ITeacherService; import org.springblade.system.user.service.ITrainPersonService; import org.springblade.system.user.service.ITrainService; import org.springblade.system.user.service.ITrainSpeakService; +import org.springblade.system.user.util.ThreadPoolFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import javax.annotation.PostConstruct; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; /** * 培训计划服务实现类 @@ -45,6 +53,17 @@ public class TrainServiceImpl extends BaseServiceImpl implem private final ISysClient sysClient; +// private ScheduledExecutorService executorService = Executors.newScheduledThreadPool(100); +// +// @PostConstruct +// public void init() { +// ScheduledExecutorService executorService; +// //cpu核心数 +// int i = Runtime.getRuntime().availableProcessors(); +// //创建执行定时任务的线程池 +// executorService = Executors.newScheduledThreadPool(i == 0 ? 10 : i * 8); +// } + @Override @Transactional(rollbackFor = Exception.class) public boolean saveTrain(Train train) { @@ -54,6 +73,19 @@ public class TrainServiceImpl extends BaseServiceImpl implem train.setNum(split.length); train.setStatus(-1); this.save(train); + + //计算相关时间 + //提前通知时间 分钟 + Integer remind = Integer.parseInt(train.getRemind()); + //会议开始时间 + Date startTime = train.getStartTime(); + //会议开始时间的毫秒数 + long time = startTime.getTime(); + //应该开始提醒的毫秒数 + long time1 = time - remind * 60 *1000; + //当前时间的毫秒数 + Long now = LocalDateTime.now().toInstant(OffsetDateTime.now().getOffset()).toEpochMilli(); + // 参训人员入库 if (train.getTrainPerson() != null && !"".equals(train.getTrainPerson())) { List personList = new ArrayList<>(); @@ -110,6 +142,26 @@ public class TrainServiceImpl extends BaseServiceImpl implem teacher.setRecentTrain(list.get(0).getName()); } + //异步执行提前通知参会人员的任务 + ThreadPoolFactory.sheduleThreadPool.schedule(new Runnable() { + @Override + public void run() { + for (String s : split) { + messageClient.event(SysTypeEnum.INFORM.getValue(), "会议提醒", + "您有新的会议将在" + train.getDuration() + "分钟后开始,请准时参加!", 1, 5, s, "/train/project"); + } + } + },time1 - now, TimeUnit.MILLISECONDS); + + //异步执行提前通知讲师的任务 + ThreadPoolFactory.sheduleThreadPool.schedule(new Runnable() { + @Override + public void run() { + messageClient.event(SysTypeEnum.INFORM.getValue(), "会议提醒", + "您有新的会议将在" + train.getDuration() + "分钟后开始,请准时参加!", 1, 5, train.getTeacher().toString(), "/train/project"); + } + },time1 - now, TimeUnit.MILLISECONDS); + return teacherService.updateById(teacher); } @@ -214,4 +266,5 @@ public class TrainServiceImpl extends BaseServiceImpl implem teacherService.updateById(teacher); } } + } diff --git a/lab-service/lab-user/src/main/java/org/springblade/system/user/util/ThreadPoolFactory.java b/lab-service/lab-user/src/main/java/org/springblade/system/user/util/ThreadPoolFactory.java new file mode 100644 index 0000000..66ced42 --- /dev/null +++ b/lab-service/lab-user/src/main/java/org/springblade/system/user/util/ThreadPoolFactory.java @@ -0,0 +1,20 @@ +package org.springblade.system.user.util; + +import java.util.concurrent.*; + +/** + * 延迟任务的线程池 + * @author ytl + * @since 2022-10-09 15:14 + */ +public class ThreadPoolFactory { + /** 获取当前系统的CPU 数目*/ + static int cpuNums = Runtime.getRuntime().availableProcessors(); + + public static ScheduledExecutorService sheduleThreadPool = null; + /**`` ``* 静态方法`` ``*/ + static + { + sheduleThreadPool = new ScheduledThreadPoolExecutor(cpuNums * 20); + } +}