基于 Spring Boot 的后台定时任务 与 异步调用

定时任务  就是在后台按照我们设定的时间和频率  来开启线程执行  我们定义好的相关代码。

比如:定时分析器,定时统计器,定时发送邮件等等

那么基于Spring Boot 我们如何开发,下面介绍最简单快速的方法

先简单了解几个注解:

@Scheduled:定时任务的配置

@Async:异步执行

@EnableScheduling: 开启对定时任务的支持

@EnableAsync:开启异步事件的支持

〇、同步定时任务。先上个完整的代码

下面是spring boot  的启动程序(@EnableScheduling使@Scheduled注解生效)

@EnableScheduling   // 开启对定时任务的支持
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class WebApplication {

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

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

/**
 * @ClassName: Schedule1
 * @Description: 任务
 * @Author Tan
 * @Date 2019/7/5
 */
@Component
public class Schedule1 {
        
    //corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
    @Scheduled(cron = "0/5 * * * * *")
    public void calculate1() {
        System.out.println("定时任务1执行开始!");

        System.out.println("这里是定时任务1执行的内容。。。。。。");

        System.out.println("定时任务1执行成功!");
    }

    @Scheduled(cron = "0/5 * * * * *")
    public void calculate2() {
        System.out.println("定时任务2执行开始!");

        System.out.println("这里是定时任务2执行的内容。。。。。。");

        System.out.println("定时任务2执行成功!");
    }


}

这样,一个简单的定时任务就完成了。

但是,这样每个Scheduled方法是同步执行的,万一有一个发生死锁,那么其他任务就没法执行,下面介绍异步定时任务

一、异步定时任务

Spring为任务调度与异步方法执行提供了注解支持,即通过在方法上设置@Async注解,可使得方法被异步调用。

异步调用的实现就是交给Spring的TaskExecutor来完成。

而这个TaskExecutor我们可以自定义

1、配置文件 application.properties

加上内容:

corePoolSize=10
maxPoolSize = 50
queueCapacity = 10

2、添加线程池配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 注解式配置文件
 *
 * @ClassName: AsyncConfig
 * @Description: TODO
 * @Author Tan
 * @Date 2019/7/5
 */
@Configuration  //表明该类是一个配置类
@EnableAsync    //开启异步事件的支持
@PropertySource(value = "classpath:application.properties")
public class AsyncConfig {

    /**
     * 线程池维护线程的最少数量
     */
    @Value("${corePoolSize}")
    private int corePoolSize;

    /**
     * 线程池维护线程的最大数量
     */
    @Value("${corePoolSize}")
    private int maxPoolSize;

    /**
     * 缓存队列
     */
    @Value("${corePoolSize}")
    private int queueCapacity;

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.initialize();
        return executor;
    }
}

3、然后跟之前一样

启动类【多了@EnableAsync】

@EnableAsync        // 使Async生效
@EnableScheduling   // 开启对定时任务的支持
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class WebApplication {

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

}

任务类【每个方法加上注解@Async,如果加到类上,表明所有方法都异步执行】

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

/**
 * @ClassName: Schedule1
 * @Description: 任务
 * @Author Tan
 * @Date 2019/7/5
 */
@Component
public class Schedule1 {
        
    @Async
    //corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
    @Scheduled(cron = "0/5 * * * * *")
    public void calculate1() {
        System.out.println("定时任务1执行开始!");

        System.out.println("这里是定时任务1执行的内容。。。。。。");

        System.out.println("定时任务1执行成功!");
    }

    @Async
    @Scheduled(cron = "0/5 * * * * *")
    public void calculate2() {
        System.out.println("定时任务2执行开始!");

        System.out.println("这里是定时任务2执行的内容。。。。。。");

        System.out.println("定时任务2执行成功!");
    }


}

二、一些说明

上面两个例子基本满足我们对定时任务的开发需求

执行终止请参考:SpringBoot 使用 @Scheduled 定时任务突然不执行了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cy谭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值