Spring Boot的定时任务与异步任务

【第一部分】历史文章:
SpringBoot总结(一)——第一个SpringBoot项目
SpringBoot总结(二)——Spring Boot的自动配置
SpringBoot总结(三)——SpringBoot的配置文件
SpringBoot总结(四)——@Value和@ConfigurationProperties的区别
SpringBoot总结(五)——@PropertySource注解与@ImportResource注解
SpringBoot总结(六)——SpringBoot配置文件占位符
SpringBoot总结(七)——Profile的使用
SpringBoot总结(八)——配置文件的加载位置
SpringBoot总结(九)——@Conditional注解与自动配置报告
SpringBoot总结(十)——SpringBoot+Mybatis实现数据库的CRUD(从创建到实现【超详细附代码】)
SpringBoot总结(十一)——SpringBoot的静态资源映射规则
SpringBoot总结(十二)——登录界面的实现
SpringBoot总结(十三)——修改嵌入式Servlet容器配置
SpringBoot总结(十四)——SpringBoot整合JDBCTemplate及Druid连接池
SpringBoot总结(十五)——接口架构风格(RESTful)
SpringBoot总结(十六)——Spring Boot的邮件发送


Spring Boot的定时任务与异步任务

下面以简单的例子介绍Spring Boot的定时任务与异步任务:



前言

定时任务是企业级开发中的最常见的功能之一,例如:统计订单数、数据库的备份、定时发送短信和邮件、定时统计博客访客等;简单的定时任务可以通过Spring中的 @Scheduled注解来实现;而复杂的定时任务可以通过Quartz来实现。


一、定时任务

@Scheduled是由Spring提供的定时任务注解,使用较为方便、配置简单,可以很好的解决大部分的定时任务需求。
下面进行简单介绍定时任务的使用:

1.创建项目

创建一个Spring Boot的Web工程,具体创建过程这里不再介绍。

2.开启定时任务

在项目的启动类上加上@EnableScheduling 注解来开启定时任务。

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
 * @author 
 */
@EnableScheduling 
@SpringBootApplication
public class SpringbootTaskApplication {

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

}

3.配置定时任务

注:定时任务主要通过 @Scheduled注解来进行配置,示例代码如下所示:

package com.example.service;

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

import java.util.Date;

/**
 * @author 
 */
@Service
public class ScheduledService {

    @Scheduled(fixedDelay = 1000)
    public void scheduledTask1(){
        System.out.println("fixedDelay:"+new Date());
    }

    @Scheduled(fixedRate = 2000)
    public void scheduledTask2(){
        System.out.println("fixedRate:"+new Date());
    }

    @Scheduled(initialDelay = 1000,fixedRate = 2000)
    public void scheduledTask3(){
        System.out.println("initialDelay:"+new Date());
    }

    @Scheduled(cron = "0 * * * * ?")
    public void scheduledTask4(){
        System.out.println("cron:"+new Date());
    }

}

注:

  • @Scheduled注解来标注一个定时任务。
  • fixedDelay=1000表示:在当前任务执行结束1秒后开启另外一个任务
  • fixedRate=2000表示:当前任务开始执行2秒后开启另外一个定时任务
  • initialDelay=1000表示:首次执行的延迟时间
  • 在@Scheduled注解中也可以使用cron表达式。
  • 表达式:cron = "0 * * * * ?"表示该定时任务每分钟执行一次

启动项目,控制台打印如下:
在这里插入图片描述

二、异步任务

两个注解:

  • @EnableAsync 开启异步注解功能
  • @Async 一般用在类的方法上;若用在类上,表示这个类的所有的方法都是异步执行。

1.开启异步任务

在项目的启动类上加上@EnableAsync 注解。

package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
 * @author 
 */
@EnableAsync 
@EnableScheduling   
@SpringBootApplication
public class SpringbootTaskApplication {

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

}

2.简单使用示例

package com.example.task;

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

/**
 * @author 
 */

@Component
public class AsyncTask {

    @Async
    public void Task1() throws Exception{
        long start = System.currentTimeMillis();
        Thread.sleep(1000);
        long end = System.currentTimeMillis();
        System.out.println("task1耗时:"+(end-start)+"毫秒!");
    }
    public void Task2() throws Exception{
        long start = System.currentTimeMillis();
        Thread.sleep(2000);
        long end = System.currentTimeMillis();
        System.out.println("task2耗时:"+(end-start)+"毫秒!");
    }
    public void Task3() throws Exception{
        long start = System.currentTimeMillis();
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        System.out.println("task3耗时:"+(end-start)+"毫秒!");
    }
}

3.测试

    @GetMapping("/asynctasktest")
    public String AsyncTaskTest() throws Exception {
        long start = System.currentTimeMillis();
        asyncTask.Task1();
        asyncTask.Task2();
        asyncTask.Task3();
        long end = System.currentTimeMillis();
        System.out.println("全部完成一共耗时:" + (end - start) + "毫秒");
        return "执行完成!!!";
    }

总结

以上分别介绍了Spring Boot的定时任务与异步任务,以及用简单的示例进行了演示。希望通过以上例子能给予大家帮助。😊

关于本项目的示例代码获取方式:关注+私信并回复:【定时任务与异步任务】即可获取哦!!!
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小小Java开发者

“是一种鼓励,你懂的”

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

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

打赏作者

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

抵扣说明:

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

余额充值