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
/** * *计划实体类 */ 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
/** * * @提醒服务类 * */ public class RemindService { //数据查询 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; } //提醒服务类 public void ouputPlan(){ List<Plan> forToday = getPlansForToday(); for (Plan plan : forToday) { System.out.println("计划时间"+plan.getDate()+"计划内容"+plan.getTask()); } } }
3. Reminder task class
/** * * @提醒任务类 * */ 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{ //创建一个任务 JobDetail job =JobBuilder.newJob(RemindJob.class).withIdentity("job1", "group1").build(); //创建一个触发器 /*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(); //注册并进行调度 scheduler.scheduleJob(job,trigger); //启动调度 scheduler.start(); //睡眠10s //Thread.sleep(10000); //关闭调度 //scheduler.shutdown(); } public static void main(String[] args) throws SchedulerException, InterruptedException { doRemind(); } }
Cron expression

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool