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 asyncTransExtract(String transId) throws Exception;

你可能感兴趣的:(Springboot实现异步)