Springboot实现异步

1:配置异步线程池,设置启用

package com.epsoft.gas.web.config;

import java.util.concurrent.Executor;

import javax.annotation.Resource;

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

/**
 * 多线程执行配置(通过 @Async 注解来声明一个异步任务)
 * @author Administrator
 */
@Configuration
@EnableAsync
public class GasAsync implements AsyncConfigurer {
    
    @Resource
    private Environment env;
    
    @Override
    public Executor getAsyncExecutor() {
        
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        int corePoolSize = 8;
        if(env!=null) {
            try {
                corePoolSize = Integer.parseInt(env.getProperty("check.thread.size"));
            }
            catch(Exception er) {
                
            }
        }
        //线程池维护线程的最少数量  
        taskExecutor.setCorePoolSize(corePoolSize);
        //线程池所使用的缓冲队列
        taskExecutor.setQueueCapacity(150);
        //线程池维护线程的最大数量  
        taskExecutor.setMaxPoolSize(1000);
        //线程池维护线程所允许的空闲时间
        taskExecutor.setKeepAliveSeconds(30000);
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return null;
    }

}

2:在接口增加   @Async注解即可

   @Async
    public Future<Integer> asyncTransExtract(String transId) throws Exception;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值