美文网首页
SpringBoot 创建动态定时任务

SpringBoot 创建动态定时任务

作者: 赛亚人之神 | 来源:发表于2019-10-18 16:45 被阅读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;
        });
      }
    
    }
    
    
    

    相关文章

      网友评论

          本文标题:SpringBoot 创建动态定时任务

          本文链接:https://www.haomeiwen.com/subject/ezfwmctx.html