quartz + springboot 注解_SpringBoot入门建站全系列(十五)内置定时任务及Quartz定时任务使用

SpringBoot入门建站全系列(十五)内置定时任务及Quartz定时任务使用

一、概述

用Spring,就是为了简单。

但是我还是要总结下java定时任务实现的几种方式。

1.TimerTask,等于一个线程隔一段时间运行一下。

2.ScheduledExecutorService,线程池版的TimerTask。

3.Spring支持的定时任务,@Schedule注解,支持crontab表达式。

4.quartz,比较流行的任务调度工具,就是配置起来麻烦。

这里只讲3、4,前两个跟Spring没关系,这里不讲。

首发地址: 品茗IT-同步发布

品茗IT 提供在线支持:

一键快速构建Spring项目工具

一键快速构建SpringBoot项目工具

一键快速构建SpringCloud项目工具

一站式Springboot项目生成

Mysql一键生成Mybatis注解Mapper

如果大家正在寻找一个java的学习环境,或者在开发中遇到困难,可以加入我们的java学习圈,点击即可加入,共同学习,节约学习时间,减少很多在学习中遇到的难题。

二、内置定时任务

本文假设你已经引入spring-boot-starter-web。已经是个SpringBoot项目了,如果不会搭建,可以打开这篇文章看一看《SpringBoot入门建站全系列(一)项目建立》。

2.1 Maven依赖

内置定时任务不需要额外添加依赖。

2.2 配置文件

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

schedule.task.test=0/2 * * * * ?

2.3 配置定时任务

需要使用@EnableScheduling注解启动定时任务,然后在需要定时执行的方法上加上@Scheduled即可:

ScheduleConfig:

package com.cff.springbootwork.schedule.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

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

@Configuration
@EnableScheduling
public class ScheduleConfig {
    @Autowired
    ScheduleService scheduleService;

    @Scheduled(cron = "${schedule.task.test}")
    public void dayJob() {
        scheduleService.doJob();
    }
}

2.4 测试的Service

package com.cff.springbootwork.schedule.service;

import org.springframework.stereotype.Service;

@Service
public class ScheduleService {

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

}

三、Quartz定时任务

本文假设你已经引入spring-boot-starter-web。已经是个SpringBoot项目了,如果不会搭建,可以打开这篇文章看一看《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属性。

Springboot Quartz官方文档

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做了封装,适合多机部署定时任务。

快速构建项目

Spring组件化构建

SpringBoot组件化构建

SpringCloud服务化构建

喜欢这篇文章么,喜欢就加入我们一起讨论SpringBoot使用吧!

v2-49b2e5bf337f7ecd52a09a46bd7f4fab_b.jpg
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值