标签:完成后 ini format class dex fixed sim ati hmm
springboot定时任务需要@EnableScheduling注解
@SpringBootApplication @EnableScheduling public class Application { public static void main(String args[]) { SpringApplication.run(Application.class, args); } }
定时任务有三种执行方式,首先每次任务执行要等到上次任务执行完成后才会执行,fixedDelay,fixedRate可以设定初始延迟时间initialDelay,cron表达式不能。
@Component public class DelayJob { SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); @Scheduled(fixedDelay=2000) public void testFixedDelay(){ System.out.println("testFixedDelay begin at "+format.format(new Date())); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("testFixedDelay end at"+format.format(new Date())); } @Scheduled(fixedRate=2000) public void testFixedRate(){ System.out.println("testFixedRate begin at "+format.format(new Date())); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("testFixedRate end at"+format.format(new Date())); } @Scheduled(cron="0/4 * * * * ?") public void testCronTab(){ System.out.println("testCronTab begin at "+format.format(new Date())); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("testCronTab end at"+format.format(new Date())); } }
标签:完成后 ini format class dex fixed sim ati hmm
原文地址:https://www.cnblogs.com/liuboyuan/p/10342021.html