Home >类库下载 >java类库 >Quartz framework implements task scheduling

Quartz framework implements task scheduling

高洛峰
高洛峰Original
2016-11-05 13:51:122246browse

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

Quartz framework implements task scheduling

Quartz framework implements task scheduling

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn