线程池定时任务

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值