springboot使用@async执行任务有时会出现超时

时间:2018-12-26 04:07:21

标签: java spring-boot asynchronous

我编写了一个有关使用spring-boot框架转发HTTP请求的项目。 有时在负载测试中(大约100个并发请求),它会出现很长的延迟时间,例如我在0.01秒内响应了HTTP请求,但在10秒后发布了异步请求。 我使用默认的Spring Boot设置,这是否意味着Spring Boot将把异步方法放到ThreadPoolTask​​Executor.class中?默认核心大小为1,那么它将在负载测试中被阻止吗?

对不起,我英语不好 任何帮助将不胜感激

@Override
@Async
public void rabbit(String jsonString, String path) {
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Content-Type", "application/json");
    HttpEntity<String> requestEntity = new HttpEntity<>(jsonString, requestHeaders);
    RestTemplate template = new RestTemplate();
    ResponseEntity<String> postRespone = template.postForEntity(path, requestEntity, String.class);
    logger.info("PostApduScripts success!" + "\tResult:" + postRespone.getBody());
}

public class ThreadPoolTaskExecutor extends ExecutorConfigurationSupport       implements AsyncListenableTaskExecutor, SchedulingTaskExecutor {
private final Object poolSizeMonitor = new Object();
private int corePoolSize = 1;
private int maxPoolSize = 2147483647;
private int keepAliveSeconds = 60;
private int queueCapacity = 2147483647;
private boolean allowCoreThreadTimeOut = false;
private TaskDecorator taskDecorator;
private ThreadPoolExecutor threadPoolExecutor;

public ThreadPoolTaskExecutor() {
}

public void setCorePoolSize(int corePoolSize) {
    synchronized(this.poolSizeMonitor) {
        this.corePoolSize = corePoolSize;
        if (this.threadPoolExecutor != null) {
            this.threadPoolExecutor.setCorePoolSize(corePoolSize);
        }

    }
}

0 个答案:

没有答案