|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2020 the original author or authors. |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -16,8 +16,12 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.scheduling.concurrent; |
|
|
|
package org.springframework.scheduling.concurrent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.BlockingQueue; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.FutureTask; |
|
|
|
import java.util.concurrent.FutureTask; |
|
|
|
|
|
|
|
import java.util.concurrent.RejectedExecutionHandler; |
|
|
|
|
|
|
|
import java.util.concurrent.ThreadFactory; |
|
|
|
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
import org.junit.jupiter.api.Test; |
|
|
|
|
|
|
|
|
|
|
|
@ -25,10 +29,16 @@ import org.springframework.context.ConfigurableApplicationContext; |
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Bean; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
|
|
|
import org.springframework.context.support.GenericApplicationContext; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.mock; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.verify; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* Tests for {@link ThreadPoolExecutorFactoryBean}. |
|
|
|
|
|
|
|
* |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class ThreadPoolExecutorFactoryBeanTests { |
|
|
|
class ThreadPoolExecutorFactoryBeanTests { |
|
|
|
@ -44,6 +54,27 @@ class ThreadPoolExecutorFactoryBeanTests { |
|
|
|
context.close(); |
|
|
|
context.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void executorWithDefaultSettingsDoesNotPrestartAllCoreThreads() { |
|
|
|
|
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
|
|
|
|
context.registerBean("taskExecutor", ThreadPoolExecutorFactoryBean.class, TestThreadPoolExecutorFactoryBean::new); |
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = context.getBean(ThreadPoolExecutor.class); |
|
|
|
|
|
|
|
verify(threadPoolExecutor, never()).prestartAllCoreThreads(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
void executorWithPrestartAllCoreThreads() { |
|
|
|
|
|
|
|
GenericApplicationContext context = new GenericApplicationContext(); |
|
|
|
|
|
|
|
context.registerBean("taskExecutor", ThreadPoolExecutorFactoryBean.class, () -> { |
|
|
|
|
|
|
|
TestThreadPoolExecutorFactoryBean factoryBean = new TestThreadPoolExecutorFactoryBean(); |
|
|
|
|
|
|
|
factoryBean.setPrestartAllCoreThreads(true); |
|
|
|
|
|
|
|
return factoryBean; |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
context.refresh(); |
|
|
|
|
|
|
|
ThreadPoolExecutor threadPoolExecutor = context.getBean(ThreadPoolExecutor.class); |
|
|
|
|
|
|
|
verify(threadPoolExecutor).prestartAllCoreThreads(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Configuration |
|
|
|
@Configuration |
|
|
|
static class ExecutorConfig { |
|
|
|
static class ExecutorConfig { |
|
|
|
@ -55,4 +86,15 @@ class ThreadPoolExecutorFactoryBeanTests { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class TestThreadPoolExecutorFactoryBean extends ThreadPoolExecutorFactoryBean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
protected ThreadPoolExecutor createExecutor( |
|
|
|
|
|
|
|
int corePoolSize, int maxPoolSize, int keepAliveSeconds, BlockingQueue<Runnable> queue, |
|
|
|
|
|
|
|
ThreadFactory threadFactory, RejectedExecutionHandler rejectedExecutionHandler) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return mock(ThreadPoolExecutor.class); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|