浅谈一下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异步任务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • java通过注解实现分表详解

    java通过注解实现分表详解

    这篇文章主要为大家详细介绍了java如何通过注解实现分表,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2024-11-11
  • Mybatis查询多条记录并返回List集合的方法

    Mybatis查询多条记录并返回List集合的方法

    这篇文章主要介绍了Mybatis查询多条记录并返回List集合的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Spring及Mybatis整合占位符解析失败问题解决

    Spring及Mybatis整合占位符解析失败问题解决

    这篇文章主要介绍了Spring及Mybatis整合占位符解析失败问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-07-07
  • MyBatis-Plus中的逻辑删除使用详解

    MyBatis-Plus中的逻辑删除使用详解

    开发系统时,有时候在实现功能时,删除操作需要实现逻辑删除就是将数据标记为删除,而并非真的物理删除(非DELETE操作),查询时需要携带状态条件,确保被标记的数据不被查询到。这样做的目的就是避免数据被真正的删除
    2022-12-12
  • WxJava微信公众号开发入门实战

    WxJava微信公众号开发入门实战

    本文主要介绍了WxJava微信公众号开发入门实战,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • Java try()语句实现try-with-resources异常管理机制操作

    Java try()语句实现try-with-resources异常管理机制操作

    这篇文章主要介绍了Java try()语句实现try-with-resources异常管理机制操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • Mybatis主配置文件的properties标签详解

    Mybatis主配置文件的properties标签详解

    这篇文章主要介绍了Mybatis主配置文件的properties标签,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • SpringBoot RESTful风格入门讲解

    SpringBoot RESTful风格入门讲解

    RESTful是一种web软件风格,它不是标准也不是协议,它不一定要采用,只是一种风格,它倡导的是一个资源定位(url)及资源操作的风格,这篇文章主要介绍了SpringBoot使用RESTful接口
    2022-11-11
  • SpringBoot测试之高级配置方式

    SpringBoot测试之高级配置方式

    这篇文章主要介绍了SpringBoot测试之高级配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-04-04
  • springboot controller 增加指定前缀的两种实现方法

    springboot controller 增加指定前缀的两种实现方法

    这篇文章主要介绍了springboot controller 增加指定前缀的两种实现方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02

最新评论