定时任务的简单实现
如下代碼:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;@Component public class A{@AutowiredBxxxxxService bxxxxxService;@Scheduled(cron = "0 0/60 * * * ?")public void action(){Result result = bxxxxxService.pickTimer();log.info(result.getMessage());} }1.利用@Scheduled(cron = “0 0/60 * * * ?”)注解定時(shí)實(shí)現(xiàn)bxxxxxService.pickTimer()方法的代碼功能
2.cron 參數(shù)及通配符含義和用法
1)時(shí)間參數(shù)位置排序(秒 分 時(shí) 日 月 年)
2)通配符種類 (, - * ? / L # W)
3)舉例:
@Scheduled(cron = “*/5 * * * *”) 每隔5秒執(zhí)行一次
@Scheduled(cron = “0 0 23 * * ?”) 每天23點(diǎn)執(zhí)行一次
@Scheduled(cron = “0 0 1 1 * ?”) 每月1號(hào)凌晨1點(diǎn)執(zhí)行一次
@Scheduled(cron = “0 0 0,13,18,21 * * ?”) 每天的0點(diǎn)、13點(diǎn)、18點(diǎn)、21點(diǎn)都執(zhí)行一次
總結(jié)
- 上一篇: javascript关于setTimeo
- 下一篇: 构造一个Car类,包含无参和有参两种构造