Springboot中如何自定义任务定时器

Springboot中如何自定义任务定时器


导入依赖:
springBoot已经默认集成了定时任务的依赖,只需要引入基本的依赖就可以使用定时任务。

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-test</artifactId>
        </dependency>

   </dependencies>

启动配置类中加入@EnableScheduling注解,意思是开启定时任务。

@SpringBootApplication
@EnableScheduling
public class SpringbootExcelApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringbootExcelApplication.class, args);
    }

}

只定义一个任务时

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

import java.util.Date;
@Component
public class ScheduleTask {
    @Scheduled(cron = "*/1 * * * * ?")
    public void execute1(){
        System.out.println(Thread.currentThread().getName()+"任务2每隔1s就执行");
    }
}

执行结果如下:
在这里插入图片描述
当任务执行时间比定时周期要长时:

    @Scheduled(cron = "*/1 * * * * ?")
    public void execute1(){
        System.out.println(new Date());
        try {
            Thread.sleep(6000);
            System.out.println(Thread.currentThread().getName()+"任务1每隔1min就执行");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

执行结果:
在这里插入图片描述

多任务时

@Component
public class ScheduleTask {
    @Scheduled(cron = "0 0/1 * * * ?")
    public void execute1() throws InterruptedException {
        System.out.println(new Date());
        //任务1每次执行花6s种
        Thread.sleep(6000);
        System.out.println(Thread.currentThread().getName()+"任务1每隔1min就执行");
    }
    @Scheduled(cron = "0 0/1 * * * ?")
    public void execute2(){
        System.out.println(new Date());
        System.out.println(Thread.currentThread().getName()+"任务2每隔1min就执行");
    }
}

执行结果:可以看出任务一和任务二都是在同一线程下执行的,任务一执行完成后,在执行任务二,这与我们期望的不一样。所以多任务执行时,需要开启多线程。

Mon Jan 18 16:15:00 CST 2021
scheduling-1任务1每隔1min就执行
Mon Jan 18 16:15:06 CST 2021
scheduling-1任务2每隔1min就执行
Mon Jan 18 16:16:00 CST 2021
scheduling-1任务1每隔1min就执行
Mon Jan 18 16:16:06 CST 2021
scheduling-1任务2每隔1min就执行

开启多线程后:
定义一个配置类开启多线程:

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(3));
    }
}

执行结果:

Mon Jan 18 16:22:00 CST 2021
Mon Jan 18 16:22:00 CST 2021
pool-1-thread-2任务2每隔1min就执行
pool-1-thread-1任务1每隔1min就执行
Mon Jan 18 16:23:00 CST 2021
pool-1-thread-3任务2每隔1min就执行
Mon Jan 18 16:23:00 CST 2021
pool-1-thread-1任务1每隔1min就执行

cron表达式

https://www.bejson.com/othertools/cron/
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值