quartz定时器-CronScheduleBuilder

quartz官网: http://www.quartz-scheduler.org

pom.xml

<dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.1</version>
</dependency>

MyJob.java

package cn.code.quartztest;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJob implements Job {
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("要执行的工作!!!");
    }
}

Test.java

package cn.code.quartztest;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

public class Test {
    public static void main(String[] args) throws Exception{
        //创建工作
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
                .withDescription("工作的描述")
                .withIdentity("工作的名称", "工作的组")
                .build();

        //创建触发器

        Trigger trigger = TriggerBuilder.newTrigger()
                .withDescription("触发器的描述")
                .withIdentity("触发器的名称", "触发器的组")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 11 * ? * *")) //"0/5 11 * ? * *"表达式
                .startAt(new Date())//不设置,默认为当前时间
                .build();

        //创建调度器,粘合工作和触发器
        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(jobDetail, trigger);

        //启动调度器
        scheduler.start();
    }
}


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页