1. Task
Job: It is an interface with only one method void execute(JobExecutionContext context). Developers implement this interface to define running tasks. The JobExecutionContext class provides various information about the scheduling context. Job runtime information is stored in the JobDataMap instance;
2. Trigger
Trigger: is a class that describes the time trigger rules that trigger Job execution. There are two main subclasses: SimpleTrigger and CronTrigger. When it only needs to be triggered once or executed at a fixed time interval, SimpleTrigger is the most suitable choice; while CronTrigger can define scheduling plans for various complex time rules through Cron expressions: such as execution at 9:00 every morning, Monday, Wednesday , executed at 5:00 pm on Friday, etc.;
3. Scheduler
JobDetail: Quartz recreates a Job instance every time it executes a Job, so it does not directly accept a Job instance, instead it receives a Job Implement the class so that the Job can be instantiated at runtime through the reflection mechanism of newInstance(). Therefore, a class is needed to describe the Job implementation class and other related static information, such as Job name, description, associated listener and other information. JobDetail assumes this role.
Create a Quartz job
1. Schedule entity class
/** * *計(jì)劃實(shí)體類 */ public class Plan { private String date; private String task; public Plan(String date, String task) { this.date = date; this.task = task; } public Plan() { } @Override public String toString() { return "Plan [date=" + date + ", task=" + task + "]"; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTask() { return task; } public void setTask(String task) { this.task = task; } }
2. Reminder service class
/** * * @提醒服務(wù)類 * */ public class RemindService { //數(shù)據(jù)查詢 public List<Plan> getPlansForToday(){ List<Plan> list=new ArrayList<Plan>(); Plan p1=new Plan("2016-11-3","呵呵"); Plan p2=new Plan("2016-11-4","嘿嘿"); list.add(p1); list.add(p2); return list; } //提醒服務(wù)類 public void ouputPlan(){ List<Plan> forToday = getPlansForToday(); for (Plan plan : forToday) { System.out.println("計(jì)劃時(shí)間"+plan.getDate()+"計(jì)劃內(nèi)容"+plan.getTask()); } } }
3. Reminder task class
/** * * @提醒任務(wù)類 * */ public class RemindJob implements Job { private RemindService service=new RemindService(); public void execute(JobExecutionContext arg0) throws JobExecutionException { service.getPlansForToday(); } public RemindService getService() { return service; } public void setService(RemindService service) { this.service = service; } }
4. Schedule timer task
public class TestJob { public static void doRemind() throws SchedulerException, InterruptedException{ //創(chuàng)建一個(gè)任務(wù) JobDetail job =JobBuilder.newJob(RemindJob.class).withIdentity("job1", "group1").build(); //創(chuàng)建一個(gè)觸發(fā)器 /*Trigger trigger = TriggerBuilder.newTrigger() .withIdentity(TriggerKey.triggerKey("myTrigger", "myTriggerGroup")) .withSchedule(SimpleScheduleBuilder.simpleSchedule() .withIntervalInMilliseconds(2)) .startAt(new Date(System.currentTimeMillis()+2000)) .build();*/ Trigger trigger=TriggerBuilder.newTrigger().withIdentity("myTrigger", "group1"). withSchedule(CronScheduleBuilder.cronSchedule("0 34 16 ? * 5#1 2016")).build(); SchedulerFactory s=new StdSchedulerFactory(); Scheduler scheduler = s.getScheduler(); //注冊并進(jìn)行調(diào)度 scheduler.scheduleJob(job,trigger); //啟動調(diào)度 scheduler.start(); //睡眠10s //Thread.sleep(10000); //關(guān)閉調(diào)度 //scheduler.shutdown(); } public static void main(String[] args) throws SchedulerException, InterruptedException { doRemind(); } }
Cron expression

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
