SpringBoot 创建动态定时任务
2019-10-18 本文已影响0人
赛亚人之神
package com.linlong.back.llis.trade.schedule;
import com.alibaba.fastjson.JSON;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
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 tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;
/**
* @Author: Administrator
* @Description:
* @Date: Created in 2019-10-18 08:08
* @Modified By:
*/
@Slf4j
@EnableScheduling
@Component
public class RemoteInvoiceResultSchedule implements SchedulingConfigurer {
@Autowired
private Environment environment;
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.addTriggerTask(() -> {
log.error("【==========================定时任务开始==========================】");
this.selectInvoicePrintResult();
log.error("【==========================定时任务开始==========================】");
}, triggerContext -> {
String platId = environment.getProperty("thisPlatId");
SystemConfig config = selectSystemConfigVo.selectSystemConfigVo(platId, "invoice_cron");
String cron = config.getValue();
log.error("动态获取表达式:{}", cron);
if (StringUtils.isEmpty(cron)) {
return null;
}
CronTrigger cronTrigger = new CronTrigger(cron);
Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
log.error("动态获取表达式:{},下次执行时间:{}", cron, nextExecutionTime);
return nextExecutionTime;
});
}
}