定时任务,执行时间动态配置方式

如果配置内容不在application.yml或application.properties,则需要在类上注解

@PropertySource(value = "classpath:task.properties",encoding = "UTF-8")


具体执行的方法上写这个
@Scheduled(cron="${Task_expireOTCAdsState_cron}") 


import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


@Component
@PropertySource(value = "classpath:task.properties",encoding = "UTF-8")
public class QuartzService {
    
    @Autowired
    private OTCAdsService otcAdsService;
    
    
    /**
     * 定时任务更新广告过期
     */
    @Scheduled(cron="${Task_expireOTCAdsState_cron}") 
//    @Scheduled(cron = "0 0/1 * * * ?")
    public void expireOTCAdsState(){
        LogUtils.info(this.getClass(), "广告过期执行开始:");
        
        LogUtils.info(this.getClass(), "广告过期执行行数:" + count);
        LogUtils.info(this.getClass(), "广告过期执行结束:");
    }

}

task.properties

# 任务执行时间cron配置
Task_expireOTCAdsState_cron=0 0/1 * * * ?

猜你喜欢

转载自www.cnblogs.com/shihaiming/p/9453296.html