Code Ease Code Ease
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档

神秘的鱼仔

你会累是因为你在走上坡路
  • 个人博客网站 (opens new window)
  • 好用的工具网站 (opens new window)
  • Java核心基础
  • 框架的艺术
  • 分布式与微服务
  • 开发经验大全
  • 设计模式
  • 版本新特性
数据库系列
大数据+AI
  • xxl-job
运维与Linux
  • 基于SpringBoot和BootStrap的论坛网址
  • 基于VuePress的个人博客网站
  • 基于SpringBoot开发的小功能
  • 做一个自己的IDEA插件
程序人生
关于我
  • 分类
  • 标签
  • 归档
服务器
  • Java核心基础

  • 框架的艺术

    • Spring

    • Mybatis

    • SpringBoot

      • 如何用SpringBoot(2.3.3版本)快速搭建一个项目
      • 一步步带你看SpringBoot(2.3.3版本)自动装配原理
      • SpringBoot配置文件及自动配置原理详解,这应该是SpringBoot最大的优势了吧
      • SpringBoot整合jdbc、durid、mybatis详解,数据库的连接就是这么简单
      • SpringBoot整合SpringSecurity详解,认证授权从未如此简单
      • SpringBoot整合Shiro详解,还在自己写登陆注册早落伍了
      • SpringBoot如何实现异步、定时任务?
        • (一)异步任务
        • (二)定时任务
      • 如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现
      • 如何使用SpringBoot写一个属于自己的Starter
      • SpringBoot请求日志,如何优雅地打印
      • 主线程的用户信息,到子线程怎么丢了
    • MQ

    • Zookeeper

    • netty

  • 分布式与微服务

  • 开发经验大全

  • 版本新特性

  • Java
  • 框架的艺术
  • SpringBoot
CodeEase
2023-09-28
目录

SpringBoot如何实现异步、定时任务?

作者:鱼仔
博客首页: codeease.top (opens new window)
公众号:Java鱼仔

# (一)异步任务

异步任务的需求在实际开发场景中经常遇到,Java实现异步的方式有很多,比如多线程实现异步。在SpringBoot中,实现异步任务只需要增加两个注解就可以实现。当前类添加@Async注解,启动类添加@EnableAsync

编写一个service,AsynService,让这个服务暂停3秒后再输出数据

@Service
public class AsynService {
    @Async
    public void async(){
        try {
            Thread.sleep(3000);
            System.out.println("执行结束");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

编写controller,调用这个服务类

@RestController
public class IndexController {
    @Autowired
    public AsynService asynService;
    @RequestMapping("/index")
    public String  asynctask(){
        asynService.async();
        return "async task";
    }
}
1
2
3
4
5
6
7
8
9
10

运行后在浏览器中访问http://localhost:8080/index ,会发现由于开启了异步,浏览器中会先输出async task,过了三秒后控制台才会输出执行结束。

# (二)定时任务

我在之前的秒杀开源项目中已经使用过定时任务,当时的场景时,每隔1分钟去轮询数据库查询过期的商品。定时任务的应用范围很广,比如每天12点自动打包日志,每天晚上12点备份等等。 在SpringBoot实现定时任务也只需要两个注解:@Scheduled和@EnableScheduling 和前面一样,@Scheduled用在需要定时执行的任务上,@EnableScheduling用在启动类上。 首先来编写定时任务类:

@Service
public class ScheduleService {

    @Scheduled(cron = "0/10 * * * * ? ")
    public void sayHello(){
        System.out.println("hello");
    }
}
1
2
3
4
5
6
7
8

@Scheduled注解中需要加入cron表达式,用来判断定时任务的执行时间,这里表示每10秒执行一次。

然后在启动类中加上注解@EnableScheduling。 运行项目,会发现每隔十秒会输出一条hello。

上次更新: 2025/02/18, 11:30:08
SpringBoot整合Shiro详解,还在自己写登陆注册早落伍了
如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现

← SpringBoot整合Shiro详解,还在自己写登陆注册早落伍了 如何在SpringBoot启动时执行初始化操作,两个简单接口就可以实现→

最近更新
01
AI大模型部署指南
02-18
02
半个月了,DeepSeek为什么还是服务不可用
02-13
03
Python3.9及3.10安装文档
01-23
更多文章>
Theme by Vdoing | Copyright © 2023-2025 备案图标 浙公网安备33021202002405 | 浙ICP备2023040452号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式