springboot-定时任务

在做开发的时候都会遇到一些定时的需求

下面我简单的说一下springboot的自带的定时

首先创建 一个要做任务的类 添加注解@Component

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author zly
 * @Date 2018/11/1 13:40
 */
@Component
public class ScheduleContorller {
    @Scheduled(cron = "0 53 13 * * ?")//每天13.53分执行一次 文章的最后会说明一下 corn的用法
    public void test(){
        System.out.println(new Date());
    }
}

在启动类中开启定时任务的开关

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication//(exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.cmbcchina.ccd.itpm.dao")
@EnableScheduling
public class NpmcsApplication {

    public static void main(String[] args) {
        SpringApplication.run(NpmcsApplication.class, args);
    }
}

启动会自动执行创建的任务

cron表达式详解

表达式生成地址:http://cron.qqe2.com/
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。按顺序依次为:

秒(0~59)
分钟(0~59)
3 小时(0~23)
4 天(0~31)
5 月(0~11)
6 星期(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
其中每个元素可以是一个值(如6),一个连续区间(9-12),一个间隔时间(8-18/4)(/表示每隔4小时),一个列表(1,3,5),通配符。由于”月份中的日期”和”星期中的日期”这两个元素互斥的,必须要对其中一个设置。配置实例:

每隔5秒执行一次:/5 * * * ?
每隔1分钟执行一次:0 /1 * * ?
0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时

0 0 12 ? * WED 表示每个星期三中午12点

“0 0 12 * * ?” 每天中午12点触发
“0 15 10 ? * *” 每天上午10:15触发
“0 15 10 * * ?” 每天上午10:15触发
“0 15 10 * * ? *” 每天上午10:15触发
“0 15 10 * * ? 2005” 2005年的每天上午10:15触发
“0 * 14 * * ?” 在每天下午2点到下午2:59期间的每1分钟触发
“0 0/5 14 * * ?” 在每天下午2点到下午2:55期间的每5分钟触发
“0 0/5 14,18 * * ?” 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
“0 0-5 14 * * ?” 在每天下午2点到下午2:05期间的每1分钟触发
“0 10,44 14 ? 3 WED” 每年三月的星期三的下午2:10和2:44触发
“0 15 10 ? * MON-FRI” 周一至周五的上午10:15触发
“0 15 10 15 * ?” 每月15日上午10:15触发
“0 15 10 L * ?” 每月最后一日的上午10:15触发
“0 15 10 ? * 6L” 每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6L 2002-2005” 2002年至2005年的每月的最后一个星期五上午10:15触发
“0 15 10 ? * 6#3” 每月的第三个星期五上午10:15触发

整合Quartz

添加pom依赖2.0以上

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
1.5.9 用一下的依赖
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>

创建一个Quartz类继承QuartzJobBean 

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;

/**
 * @Author zly
 * @Date 2018/11/1 13:59
 */
public class Qartz extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("定时测试");
    }
}

在创建一个定时任务的配置类添加注解@Configuration

import com.cmbcchina.ccd.itpm.project.controller.Qartz;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @Author zly
 * @Date 2018/11/1 14:01
 */
@Configuration
public class QuartzConfig {
    @Bean
    public JobDetail Quartz(){
        return JobBuilder.newJob(Qartz.class).withIdentity("testQuartz").storeDurably().build();
    }
    @Bean
    public Trigger testQuartzTirgger(){
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(3)  //设置时间周期单位秒
                .repeatForever();//一般输入秒,分,时 的用法
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 * * * * ?");//这个是Cron表达式的用法
        return TriggerBuilder.newTrigger().forJob(Quartz())
                .withIdentity("testQuartz")
                .withSchedule(cronScheduleBuilder)
                .build();
    }

}

启动就可以 

一般小的项目用springboot定时任务也就能够完成你想做的事情Quartz更多的时候是使用在分布式中

努力提高自己的技术,不忘初心
原文地址:https://www.cnblogs.com/blackCatFish/p/9889134.html