码迷,mamicode.com
首页 > 编程语言 > 详细

SpringQuartz快速入门

时间:2020-06-05 13:27:24      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:set   表达式   prope   release   服务   on()   tomcat   测试   boot   

SpringQuartz

一、什么是SpringQuartz

  • Quartz是启动定时任务的框架,可以帮助我们在特定的时间执行对应的程序,比如定时发送邮件等

二、快速开始

  1. 引入依赖
		<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.kuaishou.rao</groupId>
    <artifactId>demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
    </dependencies>
  1. 创建抽象类,创建抽象类的作用是可以让所有的定时任务都遵循一个规范
public abstract class AbstractJob extends QuartzJobBean {

    public abstract String getCron();
    public abstract String getName();
    public abstract Class<? extends Job> getJobClass();
    public String getGroup() {
        return "TEST_GROUP";
    }

    public abstract Trigger getTrigger() throws ParseException;
    public Trigger getDefaultTrigger() throws ParseException {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        //cron表达式
        triggerFactoryBean.setCronExpression(getCron());
        triggerFactoryBean.setName(getName());
        triggerFactoryBean.setGroup(getGroup());
        triggerFactoryBean.setJobDetail(getJobDetail());
        //如果错过了这个执行周期,所有的misFire都不管,执行下一个周期的任务。
        triggerFactoryBean.setMisfireInstruction(CronTrigger.MISFIRE_INSTRUCTION_DO_NOTHING);
        //在上述所有的bean都设置成功之后再执行下面的操作
        triggerFactoryBean.afterPropertiesSet();
        return triggerFactoryBean.getObject();
    }

    public abstract JobDetail getJobDetail();
    public JobDetail getDefaultJobDetail() {
        JobDetailFactoryBean detailFactoryBean = new JobDetailFactoryBean();
        detailFactoryBean.setJobClass(getJobClass());
        detailFactoryBean.setName(getName());
        detailFactoryBean.setGroup(getGroup());
        //之前的没有执行的任务将在服务重启后补执行
        detailFactoryBean.setRequestsRecovery(true);
        //把执行结果持久化到数据库
        detailFactoryBean.setDurability(true);
        detailFactoryBean.afterPropertiesSet();
        return detailFactoryBean.getObject();
    }
}
  1. 创建测试类,继承抽象类
@Configuration
public class TestJob extends AbstractJob{
    @Override
    public String getCron() {
        return "1-59 * * * * ? *";
    }

    @Override
    public String getName() {
        return "TestJob";
    }

    @Override
    public Class<? extends Job> getJobClass() {
        return TestJob.class;
    }

    @Override
    @Bean("TestJobTrigger")
    public Trigger getTrigger() throws ParseException {
        return getDefaultTrigger();
    }

    @Override
    @Bean("TestJobDetail")
    public JobDetail getJobDetail() {
        return getDefaultJobDetail();
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("hello world");
    }
}

三、运行结果

  • 启动工程之后,在控制台每秒打印一次hello world
2020-06-05 11:21:24.672  INFO 1464 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler ‘quartzScheduler‘ initialized from an externally provided properties instance.
2020-06-05 11:21:24.672  INFO 1464 --- [           main] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 2.3.2
2020-06-05 11:21:24.672  INFO 1464 --- [           main] org.quartz.core.QuartzScheduler          : JobFactory set to: org.springframework.scheduling.quartz.SpringBeanJobFactory@5a5c128
2020-06-05 11:21:24.696  INFO 1464 --- [           main] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2020-06-05 11:21:24.696  INFO 1464 --- [           main] org.quartz.core.QuartzScheduler          : Scheduler quartzScheduler_$_NON_CLUSTERED started.
hello world
2020-06-05 11:21:24.732  INFO 1464 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ‘‘
2020-06-05 11:21:24.735  INFO 1464 --- [           main] com.kuaishou.rao.demo.DemoApplication    : Started DemoApplication in 1.414 seconds (JVM running for 2.062)
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

SpringQuartz快速入门

标签:set   表达式   prope   release   服务   on()   tomcat   测试   boot   

原文地址:https://www.cnblogs.com/rao11/p/13048906.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!