异步上传,数据从1万提高到5万

1、配置文件yaml :java

task:
core:
poolsize: 100
max:
poolsize: 200
queue:
capacity: 200
keepAlive:
seconds: 30
thread:
name:
prefix: msgcenterspring

2、java 代码配置异步

import org.springframework.beans.factory.annotation.Value;
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.ThreadPoolExecutor;br/>@Configuration
@EnableAsync
public class AsyncConfig {ide

//接收报文核心线程数
@Value("${task.core.poolsize}")
private int taskCorePoolSize;
//接收报文最大线程数
@Value("${task.max.poolsize}")
private int taskMaxPoolSize;
//接收报文队列容量
@Value("${task.queue.capacity}")
private int taskQueueCapacity;
//接收报文线程活跃时间(秒)
@Value("${task.keepAlive.seconds}")
private int taskKeepAliveSeconds;
//接收报文默认线程名称
@Value("${task.thread.name.prefix}")
private String taskThreadNamePrefix;

/**
 * bookTaskExecutor:(接口的线程池). <br/>
 *
 * @return TaskExecutor taskExecutor接口
 * @since JDK 1.8
 */
@Bean()
public ThreadPoolTaskExecutor bookTaskExecutor() {
    //newFixedThreadPool
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 设置核心线程数
    executor.setCorePoolSize(taskCorePoolSize);
    // 设置最大线程数
    executor.setMaxPoolSize(taskMaxPoolSize);
    // 设置队列容量
    executor.setQueueCapacity(taskQueueCapacity);
    // 设置线程活跃时间(秒)
    executor.setKeepAliveSeconds(taskKeepAliveSeconds);
    // 设置默认线程名称
    executor.setThreadNamePrefix(taskThreadNamePrefix);
    // 设置拒绝策略
    // rejection-policy:当pool已经达到max size的时候,如何处理新任务
    // CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待全部任务结束后再关闭线程池
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.initialize();
    return executor;

}

}线程

3、异步方法,添加@Async() 注解code

@Async() 
@Override
public void addAppExceldata(List<AppointUploadUserVo> list, String taskId, int type) throws Exception {

    }
}
    以上方法从一万上传效率提高到5万 ,而且在一分钟只能请求接口不超时。
相关文章
相关标签/搜索