Quartz定时任务使用(二)

Quartz定时任务

本文假设你已经引入spring-boot-starter-web。已经是个SpringBoot项目了,如果不会搭建,这篇文章看一看

3.1 Maven依赖

需要额外引入quartz的starter:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

3.2 配置文件

在application.properties 中需要额外添加quartz的配置,也可以把定时任务的crontab表达式提出来,如:

schedule.task.test=0/2 * * * * ?
spring.quartz.job-store-type=memory
spring.quartz.scheduler-name=quartzScheduler

这里,spring.quartz.job-store-type意思是用内存存储quartz的定时任务信息,如果需要用数据库存储,可以用:spring.quartz.job-store-type=jdbc,这样的话,配置的东西就多了,还需要配置quartz的数据源,配置spring.quartz.jdbc.initialize-schema属性。

spring.quartz.scheduler-name指明scheduler的名称。

3.3 配置定时任务

注意这里,每个定时任务需要配置一个JobDetail和一个Trigger,Springboot自己管理了一个SchedulerFactory,因此不需要再配置SchedulerFactoryBean:

QuartzJobConfig :

package com.cff.springbootwork.quartz.config;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cff.springbootwork.quartz.job.SampleJob;

@Configuration 
public class QuartzJobConfig {
    @Value("${schedule.task.test}")
    private String testScheduleCron;


    @Bean
    public JobDetail teatQuartzDetail(){
        return JobBuilder.newJob(SampleJob.class).withIdentity("testQuartz").storeDurably().build();
    }

    @Bean
    public Trigger testQuartzTrigger(){
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(testScheduleCron);
        return TriggerBuilder.newTrigger().forJob(teatQuartzDetail())
                .withIdentity("testQuartz")
                .withSchedule(scheduleBuilder)
                .build();
    }
}

这里的JobDetail 中的newJob必须实现Job接口,因此我们要自己建一个job。

package com.cff.springbootwork.quartz.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import com.cff.springbootwork.quartz.service.ScheduleService;

@Component
public class SampleJob extends QuartzJobBean {
    @Autowired
    private ScheduleService scheduleService;

    private String name;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        scheduleService.doJob();
    }

    public ScheduleService getScheduleService() {
        return scheduleService;
    }

    public void setScheduleService(ScheduleService scheduleService) {
        this.scheduleService = scheduleService;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

3.4 测试的Service

package com.cff.springbootwork.quartz.service;

import org.springframework.stereotype.Service;

@Service
public class ScheduleService {

    public void doJob() {
        System.out.println("test");
    }

}

总结一句,Springboot 内置的定时任务已经可以实现大多数定时任务的需求,如果对任务有严格要求,可以使用xxl-job,它对quartz做了封装,适合多机部署定时任务。

原文地址:https://www.cnblogs.com/wanqiang/p/14281940.html