package com.lq.activity.process;
import com.lq.activity.application.ActivityService;
import com.lq.activity.domain.enums.ActivityType;
import com.lq.activity.domain.model.Activity;
import com.lq.activity.infrastructure.repository.ActivityRepository;
import com.lq.activity.utils.CronUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledFuture;
@Slf4j
@Component
@EnableScheduling
public class LootActivityProcess {
private ThreadPoolTaskScheduler schedulerPool;
@Autowired
public void setSchedulerPool(ThreadPoolTaskScheduler schedulerPool) {
this.schedulerPool = schedulerPool;
}
@Bean
public ThreadPoolTaskScheduler threadPoolTaskScheduler() {
return new ThreadPoolTaskScheduler();
}
@Autowired
private ActivityService activityService;
@Autowired
private ActivityRepository activityRepo;
private Map<String, ScheduledFuture<?>> futureMap = new HashMap<>();
@PostConstruct
private void initFutureMap() {
List<Activity> activityList = activityRepo.findAllByActivityType(ActivityType.loot);
for (Activity activity : activityList) {
addFutureActivity(activity);
}
}
public void addFutureActivity(Activity activity) {
// 根据不同的时候添加不同的任务
Date now = new Date();
Date startTime = activity.getStartTime();
Date endTime = activity.getEndTime();
Date drawPrizeTime = activity.getDrawPrizeTime();
if (now.before(activity.getStartTime())) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
addFuture(activity, startTime, LootProcessType.startLoot);
addFuture(activity, endTime, LootProcessType.endLoot);
} else if (now.after(startTime) && now.before(endTime)) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
addFuture(activity, endTime, LootProcessType.endLoot);
} else if (now.after(endTime) && now.before(drawPrizeTime)) {
addFuture(activity, drawPrizeTime, LootProcessType.drawLoot);
}
}
/**
* 按照夺宝活动添加夺宝定时任务
*
* @param activity 活动
* @param key 任务标识
*/
public void addFuture(Activity activity, Date date, String key) {
key = activity.getId() + "-" + key;
stopTask(futureMap.get(key));
futureMap.put(key, getFuture(activity, date, key));
log.info("添加完成,现有:{}个任务", futureMap.size());
}
/**
* 生成夺宝定时任务
*/
public ScheduledFuture<?> getFuture(Activity activity, Date date, String key) {
String cron = CronUtils.generateCron(date);
if (!StringUtils.hasLength(cron)) {
return null;
}
String[] keys = key.split("-");
CronTrigger trigger = new CronTrigger(cron);
if (LootProcessType.drawLoot.equals(keys[1])) {
return schedulerPool.schedule(executeDraw(activity), trigger);
}
return schedulerPool.schedule(executeUpdateLootStatus(activity), trigger});
}
/**
* 停止定时任务
*/
private void stopTask(ScheduledFuture<?> future) {
if (future != null) {
future.cancel(true);
}
}
/**
* 执行定时任务的线程-生成夺宝获奖号
*/
private Runnable executeDraw(Activity activity) {
return () -> activityService.generateLootProductWin(activity);
}
/**
* 执行定时任务的线程-修改夺宝活动状态
*/
private Runnable executeUpdateLootStatus(Activity activity) {
return () -> activityService.updateLootStatusForActivityAndLootProduct(activity);
}
}
线程池定时任务
最新推荐文章于 2024-04-30 15:10:34 发布