• 【SpringBoot】8.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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • service层接口
    package com.yuhuofei.service;
    
    /**
     * @Description
     * @ClassName AsyncService
     * @Author yuhuofei
     * @Date 2022/8/22 21:55
     * @Version 1.0
     */
    public interface AsyncService {
    
        String getHello();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 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,测试一下";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

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

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    第二步

    使用自定义线程池,如下所示
    在这里插入图片描述

    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,测试一下";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

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

  • 相关阅读:
    互联网摸鱼日报(2022-11-22)
    PandoraBox版本及已安装软件包
    Android Activity跳转
    计算机程序语言的执行过程(个人简单理解)
    图像处理之matlab中fspecial函数用法详解
    微信小程序打开弹出层页面还可以滚动,导致弹出层和内容分离。不如筛选的时候。
    抖音矩阵系统源码,抖音矩阵系统独立部署定制开发。
    STM32F411配置GPIO输入输出(标准库)
    使用mysql-explain 查询sql执行计划,你会用了吗
    说一下 session 的工作原理?
  • 原文地址:https://blog.csdn.net/Crezfikbd/article/details/126474550