栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程池定时任务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程池定时任务

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> futureMap = new HashMap<>();

    @PostConstruct
    private void initFutureMap() {
        List 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);
        }
    }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697586.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号