1、为培训增加提前提醒的功能

dev
a15234804788@163.com 3 years ago
parent 5e7d539cec
commit e55d98a36b
  1. 2
      lab-service/lab-user/src/main/java/org/springblade/system/user/UserApplication.java
  2. 51
      lab-service/lab-user/src/main/java/org/springblade/system/user/controller/TrainController.java
  3. 53
      lab-service/lab-user/src/main/java/org/springblade/system/user/service/impl/TrainServiceImpl.java
  4. 20
      lab-service/lab-user/src/main/java/org/springblade/system/user/util/ThreadPoolFactory.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) {

@ -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<Train> 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);
}
}

@ -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<TrainMapper, Train> 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<TrainMapper, Train> 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<TrainPerson> personList = new ArrayList<>();
@ -110,6 +142,26 @@ public class TrainServiceImpl extends BaseServiceImpl<TrainMapper, Train> 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<TrainMapper, Train> implem
teacherService.updateById(teacher);
}
}
}

@ -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);
}
}
Loading…
Cancel
Save