Merge pull request #213 from bsinno/fix_avoid_loosing_data_if_pool_maxed_out

Set new policy for central executor pool to avoid loosing data.
This commit is contained in:
Kai Zimmermann
2016-06-15 12:50:19 +02:00
committed by GitHub

View File

@@ -13,6 +13,7 @@ import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -60,15 +61,21 @@ public class ExecutorAutoConfiguration {
public ThreadPoolExecutor threadPoolExecutor() { public ThreadPoolExecutor threadPoolExecutor() {
final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>( final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(
asyncConfigurerProperties.getQueuesize()); asyncConfigurerProperties.getQueuesize());
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(asyncConfigurerProperties.getCorethreads(), return new ThreadPoolExecutor(asyncConfigurerProperties.getCorethreads(),
asyncConfigurerProperties.getMaxthreads(), asyncConfigurerProperties.getIdletimeout(), asyncConfigurerProperties.getMaxthreads(), asyncConfigurerProperties.getIdletimeout(),
TimeUnit.MILLISECONDS, blockingQueue, TimeUnit.MILLISECONDS, blockingQueue,
new ThreadFactoryBuilder().setNameFormat("central-executor-pool-%d").build()); new ThreadFactoryBuilder().setNameFormat("central-executor-pool-%d").build(),
threadPoolExecutor.setRejectedExecutionHandler((r, executor) -> LOGGER.warn( new PoolSizeExceededPolicy());
"Reject runnable for centralExecutorService, reached limit of queue size {}", }
executor.getQueue().size()));
return threadPoolExecutor; private static class PoolSizeExceededPolicy extends CallerRunsPolicy {
@Override
public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executor) {
LOGGER.warn(
"Caller has to run on its own instead of centralExecutorService, reached limit of queue size {}",
executor.getQueue().size());
super.rejectedExecution(r, executor);
}
} }
/** /**