Quartz表达式触发
生活随笔
收集整理的這篇文章主要介紹了
Quartz表达式触发
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
引入jar包
<dependency><groupId>org.quartz-scheduler</groupId><artifactId>quartz</artifactId><version>2.2.2</version> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId><version>4.1.3.RELEASE</version> </dependency>創建Job
public class HelloJob implements Job {private static Logger _log = LoggerFactory.getLogger(HelloJob.class);/*** */public HelloJob() {}/*** */public void execute(JobExecutionContext context)throws JobExecutionException {// Say Hello to the World and display the date/time_log.info("Hello World! - " + new Date());}}表達式觸發器
public class SimpleCronExample {public void run() throws Exception {Logger log = LoggerFactory.getLogger(SimpleCronExample.class);log.info("------- Initializing ----------------------");// 定義調度器SchedulerFactory sf = new StdSchedulerFactory();Scheduler sched = sf.getScheduler();log.info("------- Initialization Complete -----------");// 獲取當前時間的下一分鐘Date runTime = evenMinuteDate(new Date());log.info("------- Scheduling Job -------------------");// 定義jobJobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();// 定義觸發器,每2秒執行一次Trigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0 0/1 * * * ?")).build();// 將job注冊到調度器sched.scheduleJob(job, trigger);log.info(job.getKey() + " will run at: " + runTime);// 啟動調度器sched.start();log.info("------- Started Scheduler -----------------");// 等待1分鐘log.info("------- Waiting 60 seconds... -------------");try {Thread.sleep(60L * 1000L);} catch (Exception e) {//}// 關閉調度器log.info("------- Shutting Down ---------------------");sched.shutdown(true);log.info("------- Shutdown Complete -----------------");}public static void main(String[] args) throws Exception {SimpleCronExample example = new SimpleCronExample();example.run();}}總結
以上是生活随笔為你收集整理的Quartz表达式触发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Quartz简单触发
- 下一篇: Quartz整合Spring