注釈付きの時間指定タスク



Annotated Timed Tasks



@Component /*@EnableScheduling*/ //Enable timing tasks, which can be written on the class or springboot starter public class TaskDemo { /** * * Execute once every minute */ @Scheduled(cron = '0 0/1 * * * ?') public void work() throws Exception { System.out.println('Perform a scheduled task every minute:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } @Scheduled(cron = '0/2 * * * * ?') //Execute every 2 seconds public void doSomething() throws Exception { System.out.println('Perform a timed task every 2 seconds:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } @Scheduled(cron = '0 0 0/1 * * ? ') // Execute once every hour public void goWork() throws Exception { System.out.println('Timed tasks executed every hour:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } @Scheduled(cron = '0 0 10,14,16 * * ? ') // Every day at 10 am, 2 pm, 4 pm public void goWork2() throws Exception { System.out.println('Perform a timed task at 10 am, 2 pm, and 4 pm every day:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } @Scheduled(cron = '0 0/30 9-17 * * ? ') // Every half hour during working hours from 9 to 5 public void goWork3() throws Exception { System.out.println('Timed tasks that are executed every half an hour during working hours from 9 to 5:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } @Scheduled(cron = '0 15 10 * * ? ') // trigger every day at 10:15 am public void goWork4() throws Exception { System.out.println('A timed task that is triggered once every day at 10:15 am:'+new SimpleDateFormat('yyyy-MM-dd HH:mm:ss').format(new Date())) } }