浅谈一下SpringBoot中的异步任务

 更新时间:2023年10月17日 10:38:36   作者:yuhuofei2021  
这篇文章主要介绍了浅谈一下SpringBoot中的异步任务,SpringBoot 中的异步任务主要是指在 SpringBoot 中使用异步线程完成处理任务,在 SpringBoot 中使用异步线程非常简单,只需要两个注解就可以搞定,需要的朋友可以参考下

SpringBoot异步任务

SpringBoot 中的异步任务主要是指在 SpringBoot 中使用异步线程完成处理任务。

在 SpringBoot 中使用异步线程非常简单,只需要两个注解就可以搞定。一个是 @EnableAsync (用于开启异步注解),另一个是 @Async (一般加在方法上,表示该方法异步执行)。

1、使用自带的线程池实现异步任务

第一步

在启动类上加上注解 @EnableAsync ,如下所示

在这里插入图片描述

第二步

写接口,并将接口的实现方法,用注解 @Async 标识。

  • controller层
package com.yuhuofei.controller;

import com.yuhuofei.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description
 * @ClassName AsyncController
 * @Author yuhuofei
 * @Date 2022/8/22 21:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/async")
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping("/getAsyncHello")
    public String getAsyncHello(){
        return asyncService.getHello();
    }
}

service层接口

package com.yuhuofei.service;

/**
 * @Description
 * @ClassName AsyncService
 * @Author yuhuofei
 * @Date 2022/8/22 21:55
 * @Version 1.0
 */
public interface AsyncService {

    String getHello();
}

service层接口实现类

package com.yuhuofei.service;

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

/**
 * @Description
 * @ClassName AsyncServiceImpl
 * @Author yuhuofei
 * @Date 2022/8/22 21:55
 * @Version 1.0
 */
@Service
public class AsyncServiceImpl implements AsyncService {

    @Override
    @Async
    public String getHello() {
        return "hello,测试一下";
    }
}

至此完成异步任务的简单实现。

2、使用自定义的线程池实现异步任务

第一步

自定义一个线程池,如下所示

package com.yuhuofei.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author yuhuofei
 * @version 1.0
 * @description 自定义异步线程池
 * @date 2022/8/22 21:55
 */
@Configuration
@EnableAsync
public class AsyncExecutorConfig {
    private static final int CORE_POOL_SIZE = 10;

    private static final int MAX_POOL_SIZE = 20;

    private static final int QUEUE_CAPACITY = 2000;

    private static final String THREAD_NAME_PREFIX = "AsyncExecutor-self";

    private static final int KEEP_ALIVE_SECONDS = 60 * 10;

    @Bean("asyncExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_POOL_SIZE);
        executor.setMaxPoolSize(MAX_POOL_SIZE);
        executor.setQueueCapacity(QUEUE_CAPACITY);
        executor.setThreadNamePrefix(THREAD_NAME_PREFIX);
        executor.setAllowCoreThreadTimeOut(true);
        executor.setKeepAliveSeconds(KEEP_ALIVE_SECONDS);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}

第二步

使用自定义线程池,如下所示

在这里插入图片描述

package com.yuhuofei.service;

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

/**
 * @Description
 * @ClassName AsyncServiceImpl
 * @Author yuhuofei
 * @Date 2022/8/22 21:55
 * @Version 1.0
 */
@Service
public class AsyncServiceImpl implements AsyncService {

    @Override
    @Async("asyncExecutor")
    public String getHello() {
        return "hello,测试一下";
    }
}

由于自定义线程池时已经开启了异步注解,因此可以不用在启动类上加了,至此完成使用自定义线程池实现异步任务。

到此这篇关于浅谈一下SpringBoot中的异步任务的文章就介绍到这了,更多相关SpringBoot异步任务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • idea新建聚合项目并附上标签的详细过程

    idea新建聚合项目并附上标签的详细过程

    这篇文章主要介绍了idea新建聚合项目并附上标签的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-08-08
  • SpringBoot使用阿里OSS实现文件云存储的方法

    SpringBoot使用阿里OSS实现文件云存储的方法

    这篇文章主要介绍了SpringBoot使用阿里OSS实现文件云存储,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • SpringBoot 微信退款功能的示例代码

    SpringBoot 微信退款功能的示例代码

    这篇文章主要介绍了SpringBoot 微信退款功能的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • java8新特性教程之time包使用总结

    java8新特性教程之time包使用总结

    Java8新增了date和time的util包,下面这篇文章主要给大家介绍了关于java8新特性教程之time包使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • 详解Java两种方式简单实现:爬取网页并且保存

    详解Java两种方式简单实现:爬取网页并且保存

    本篇文章主要介绍了Java两种方式简单实现:爬取网页并且保存 ,主要用UrlConnection、HttpClient爬取实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-12-12
  • SpringBoot Test的webEnvironment源码解读

    SpringBoot Test的webEnvironment源码解读

    这篇文章主要为大家介绍了SpringBoot Test的webEnvironment源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • Spring Boot Gateway 从入门到精通全面指南

    Spring Boot Gateway 从入门到精通全面指南

    Spring Cloud Gateway 作为微服务架构中的重要组件,提供了强大而灵活的路由与过滤功能,通过本教程,您应该已经了解了如何集成 Spring Cloud Gateway、如何配置路由规则以及如何利用其丰富的功能来满足各种业务需求,感兴趣的朋友跟随小编一起看看吧
    2025-09-09
  • springboot通过jar包启动中文日志乱码问题及解决

    springboot通过jar包启动中文日志乱码问题及解决

    这篇文章主要介绍了springboot通过jar包启动中文日志乱码问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-06-06
  • 在Spring项目开发中Domain层的使用解析

    在Spring项目开发中Domain层的使用解析

    Domain是领域层的核心数据载体,主要在领域层内部及与Service层之间传递,用于封装核心业务概念和规则,这篇文章给大家介绍在Spring项目开发中,Domain层的作用详解,感兴趣的朋友跟随小编一起看看吧
    2026-02-02
  • 搭建SpringBoot项目三种方式(图文教程)

    搭建SpringBoot项目三种方式(图文教程)

    Springboot作为当下最主流的java开发框架,已成为IT从业人员的入门必备技能,本文主要介绍了搭建SpringBoot项目三种方式,感兴趣的可以了解一下
    2023-09-09

最新评论