1.首先第一步,固然是引入依賴啦,maven倉庫中最新的版本是2.3.0框架
<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.3.0</version> </dependency>
2.進行一個簡單的例子,來學習此框架maven
import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import static org.quartz.JobBuilder.newJob; import static org.quartz.SimpleScheduleBuilder.simpleSchedule; import static org.quartz.TriggerBuilder.newTrigger; /** * QuartzTest class * * @author guanhuifang * @date 2018/1/4 下午3:31 **/ public class QuartzTest { public static void main(String[] args) { try { //建立一個定時任務調度器 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); //定時任務啓動 scheduler.start(); //新建一個觸發器,開始時間是如今,每間隔40s重複 Trigger trigger = newTrigger().withIdentity("trigger1", "group1") .startNow() .withSchedule(simpleSchedule().withIntervalInSeconds(10).repeatForever()).build(); //定義name/group //建立一個job任務數據, JobDetail job = newJob(HelloJob.class) .withIdentity("job1", "group1") .build(); scheduler.scheduleJob(job, trigger); try { Thread.sleep(100*1000); //爲毫秒級別,先執行100s再說 } catch (InterruptedException e) { e.printStackTrace(); } scheduler.shutdown(); } catch (SchedulerException e) { e.printStackTrace(); } } }
3.運行結果以下:學習