cron表达式是什么:

cron表达式是一个字符串,该字符串由6个空格分为7个域,每一个域代表一个时间含义。常用语定时任务。

cron表达式每5分钟一次:

JobDetail job1 = JobBuilder.newJob(FirstJob.class).withIdentity("job1", "group1").build();

Trigger trigger1 = TriggerBuilder.newTrigger().withIdentity("cronTrigger1", "group1")

.withSchedule(CronScheduleBuilder.cronSchedule("*/10 * * * * ?")).build();

Scheduler scheduler1 = new StdSchedulerFactory().getScheduler();

scheduler1.start();

scheduler1.scheduleJob(job1, trigger1);

JobDetail job2 = JobBuilder.newJob(SecondJob.class).withIdentity("job2", "group2").build();

Trigger trigger2 = TriggerBuilder.newTrigger().withIdentity("cronTrigger2", "group2")

.withSchedule(CronScheduleBuilder.cronSchedule(new CronExpression("0/7 * * * * ?"))).build();

Scheduler scheduler2 = new StdSchedulerFactory().getScheduler();

scheduler2.start();

scheduler2.scheduleJob(job2, trigger2);

Thread.sleep(100000);

scheduler1.shutdown();

scheduler2.shutdown();

推荐内容