其他分享
首页 > 其他分享> > spring动态cron任务

spring动态cron任务

作者:互联网

package com.lv.qggz.man.service.task;

import com.lv.qggz.lib.entity.IntervalAICFaceConfig;
import com.lv.qggz.lib.service.IntervalAICFaceConfigService;
import com.lv.qggz.man.service.IntervalDataService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.ThreadPoolExecutor;

/**
* @Author: sh
* @Description: GeneSealTxtAndImgService
* @Version:
* @Date: 2022/8/26
*/
@EnableScheduling
@Component
public class GeneSealTxtAndImgService implements SchedulingConfigurer {

private static org.apache.log4j.Logger logger = Logger.getLogger(GeneSealTxtAndImgService.class);

@Resource
private IntervalAICFaceConfigService intervalAICFaceConfigService;

@Resource
private IntervalDataService intervalDataService;

@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
IntervalAICFaceConfig intervalAICFaceConfig = intervalAICFaceConfigService.queryCfg();
scheduledTaskRegistrar.addTriggerTask(() -> {
try {
if (null != intervalAICFaceConfig) {
String startDate = intervalAICFaceConfig.getStartDate();
String endDate = intervalAICFaceConfig.getEndDate();
String intervalHours = intervalAICFaceConfig.getIntervalHours();
String export = intervalDataService.export(startDate, endDate, Integer.valueOf(intervalHours));
}
} catch (Exception e) {
logger.error("gene in net seal txt data exception=" + e.getMessage());
}
}, triggerContext -> {
String cron = "";
if (null != intervalAICFaceConfig) {
if (StringUtils.isNotBlank(intervalAICFaceConfig.getQuartzCron())) {
cron = intervalAICFaceConfig.getQuartzCron();
}
}
CronTrigger trigger = new CronTrigger(cron);
Date nextExec = trigger.nextExecutionTime(triggerContext);
return nextExec;
});
}
}

标签:lv,intervalAICFaceConfig,String,spring,springframework,cron,import,org,动态
来源: https://www.cnblogs.com/sung1024/p/16628338.html