Springboot學習筆記(二)-定時任務


springboot中要使用定時任務需要在配置類或啟動類上標注注解@EnableScheduling,並在定時執行的無參方法上標注注解@Scheduled,程序啟動后會根據@Scheduled所提供的信息定時執行。

Scheduled參數

參數名 含義
cron = "* * * * * ?" 每秒執行
zone 時區,默認為本地時區TimeZone.getDefault()
fixedDelay = 1000 上次任務執行完成后1秒開始
fixedDelayString = "1000" 等同fixedDelay = 1000
fixedRate = 1000 每秒執行
fixedRateString = "1000" 等同fixedRate = 1000
initialDelay = 1000 初始延時1秒執行
initialDelayString = "1000" 等同initialDelay = 1000

cron表達式

不用記,網上有在線cron生成器

**String

公司規定的代碼規范中不允許使用魔法數字,可以用這些參數規避。

代碼

@Component
public class ScheduledHelloTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledHelloTask.class);

    private int getSecond() {
        return Calendar.getInstance().get(Calendar.SECOND);
    }

    // 每秒執行
    @Scheduled(cron = "* * * * * ?", zone = "Asia/Shanghai")
    public void sayHello() {
        LOGGER.info("Hello World!");
    }

    // 任務執行完成后延時1秒開始
    @Scheduled(fixedDelay = 1000)
    public void sayHello1() throws InterruptedException {
        LOGGER.info(getSecond() + "春暖花開~");
        Thread.sleep(1000);
    }

    // 每秒執行,效果等同{cron = "* * * * * ?"}
    @Scheduled(initialDelay = 2000, fixedRate = 1000)
    public void sayHello2() throws InterruptedException {
        LOGGER.info(getSecond() + "你好~");
    }
}

關閉

有時候我們在得到自己需要的結果后想關閉定時任務,比如通過前台發送鏈接來開啟上面的打印Hello World!任務,希望它執行10次后關閉。
此時就不能在類ScheduledHelloTask上添加@Component了, 因為我們需要動態注冊bean來實現。改造如下:

@EnableScheduling
public class ScheduledHelloTask {
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledHelloTask.class);
    private AtomicInteger atomicInteger = new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger(AtomicInteger atomicInteger) {
        this.atomicInteger = atomicInteger;
    }

    // 每秒執行
    @Scheduled(cron = "* * * * * ?", zone = "Asia/Shanghai")
    public void sayHello() {
        int count = atomicInteger.incrementAndGet();
        LOGGER.info("第" + count + "次:Hello World!");
    }
}

添加ScheduleController,代碼如下:

@RestController
public class ScheduleController {
    private static final String BEAN_NAME = "scheduledHelloTask";

    @GetMapping
    public String sayHi() throws InterruptedException {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext();
        if (!applicationContext.containsBean(BEAN_NAME)) {
            applicationContext.register(ScheduledHelloTask.class);
        }
        applicationContext.refresh();
        while (applicationContext.containsBean(BEAN_NAME)) {
            if (applicationContext.getBean(ScheduledHelloTask.class).getAtomicInteger().get() == 10) {
                applicationContext.removeBeanDefinition(BEAN_NAME);
            }
        }
        return "success";
    }
}

兩次訪問localhost:8080,結果如下:

預期效果已實現!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM