热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

定时框架quartz使用

使用simpleScheduleimportorg.quartz.JobBuilder;importorg.quartz.JobDetail;importorg.quartz.

使用simpleSchedule

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;import org.quartz.impl.StdSchedulerFactory;public class SimpleScheduler {public static void main(String[] args){System.out.println("startt");new SimpleScheduler().startScheduler();}public void startScheduler(){try {Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler(); JobDetail job = JobBuilder.newJob(DemoJob.class).withIdentity("job1", "group1").build();Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startNow().withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(1).repeatForever()) .build();scheduler.scheduleJob(job, trigger);scheduler.start();} catch (Exception e) {// TODO: handle exception
}}
}

 

 

使用cronSchedule

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.CronScheduleBuilder;
import org.quartz.impl.StdSchedulerFactory;public class SimpleScheduler {public static void main(String[] args){System.out.println("startt");new SimpleScheduler().startScheduler();}public void startScheduler(){try {Scheduler scheduler= StdSchedulerFactory.getDefaultScheduler(); JobDetail job = JobBuilder.newJob(DemoJob.class).withIdentity("job1", "group1").build();Trigger trigger = TriggerBuilder.newTrigger()/*.withIdentity("trigger1", "group1")*/.startNow().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")) .build();scheduler.scheduleJob(job, trigger);scheduler.start();} catch (Exception e) {// TODO: handle exception
}}
}

 

 

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;public class DemoJob implements Job{public void execute(JobExecutionContext arg0) throws JobExecutionException { System.out.println("Hello"); }
}

 

转:https://www.cnblogs.com/iyjhabc/p/4238851.html



推荐阅读
author-avatar
雨含的晴天
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有