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

一、配置文件yaml :

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

二、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 {

//接收报文核心线程数
@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;

}

}

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

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

    }
}
    以上方法从一万上传效率提升到5万 ,并且在一分钟只能请求接口不超时。

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

上一篇:容器云平台No.9~kubernetes日志收集系统EFK


下一篇:JS 时间戳转日期格式