spring boot 异步

配置config文件

package com.bootdo.common.config;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

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

/**
 * @author zz
 * @date 2020/7/9 21:55
 * @description spring异步支持配置类
 * 默认情况下,Spring使用 {@link org.springframework.core.task.SimpleAsyncTaskExecutor} 实际异步运行这些方法,
 * 可以在两个级别上覆盖默认值
 * 1:在应用级别,实现{@link AsyncConfigurer}并实现其getAsyncExecutor()方法
 * 2:在方法级别,@Async的value属性指定你配置的Executor的Bean名称
 */
@Configuration
// @EnableAsync 表示开启异步处理
@EnableAsync
public class SpringAsyncConfig implements AsyncConfigurer {
    /**
     * The {@link Executor} instance to be used when processing async
     * method invocations.
     */
    @Override
    public Executor getAsyncExecutor() {
        return taskExecutor();
    }


    /**
     * The {@link AsyncUncaughtExceptionHandler} instance to be used
     * when an exception is thrown during an asynchronous method execution
     * with {@code void} return type.
     */
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncExceptionHandler();
    }

    @Bean(name = "taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();

        // 核心线程数: 线程池创建时候初始化的线程数
        executor.setCorePoolSize(5);
        // 最大线程数: 线程池最大的线程数,只有在缓冲队列满了之后,才会申请超过核心线程数的线程
        executor.setMaxPoolSize(8);
        // 缓冲队列: 用来缓冲执行任务的队列
        executor.setQueueCapacity(1600);
        // 允许线程的空闲时间: 当超过了核心线程之外的线程,在空闲时间到达之后会被销毁
        executor.setKeepAliveSeconds(60);
        // 线程池名的前缀: 可以用于定位处理任务所在的线程池
        executor.setThreadNamePrefix("taskExecutor-");
        // 线程池对拒绝任务的处理策略: 这里采用CallerRunsPolicy策略,当线程池没有处理能力的时候,该策略会直接在execute方法的调用线程中运行被拒绝的任务;
        // 如果执行程序已关闭,则会丢弃该任务
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 该方法用来设置线程池关闭的时候等待所有任务都完成后,再继续销毁其他的Bean,这样这些异步任务的销毁就会先于数据库连接池对象的销毁
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 该方法用来设置线程池中任务的等待时间,如果超过这个时间还没有销毁就强制销毁,以确保应用最后能够被关闭,而不是阻塞住
        executor.setAwaitTerminationSeconds(120);
        // 必须初始化
        executor.initialize();

        return executor;
    }

}

编写异步方法

@Component
public class anyscTest{

  @Async
  public void test(){
    // 处理业务逻辑
  }
} 

调用

public class test{
 @Autowired
 anyscTest anyscTest;
public void t(){
   anyscTest.test();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值