From 4f23fb1377c61a81d2d6b8d83f98ce3c9f1d1b2c Mon Sep 17 00:00:00 2001 From: Kai Zimmermann Date: Wed, 8 Jun 2016 11:58:59 +0200 Subject: [PATCH] Created separate bean for poolexecutor. Signed-off-by: Kai Zimmermann --- ...ableDelegatingSecurityContextExecutor.java | 59 ------------------- .../scheduling/ExecutorAutoConfiguration.java | 26 +++++--- 2 files changed, 18 insertions(+), 67 deletions(-) delete mode 100644 hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/CloseableDelegatingSecurityContextExecutor.java diff --git a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/CloseableDelegatingSecurityContextExecutor.java b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/CloseableDelegatingSecurityContextExecutor.java deleted file mode 100644 index 1160e12e0..000000000 --- a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/CloseableDelegatingSecurityContextExecutor.java +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright (c) 2015 Bosch Software Innovations GmbH and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - */ -package org.eclipse.hawkbit.autoconfigure.scheduling; - -import java.util.List; -import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; - -import org.springframework.context.annotation.Bean; -import org.springframework.security.concurrent.DelegatingSecurityContextExecutor; -import org.springframework.security.core.context.SecurityContext; -import org.springframework.security.core.context.SecurityContextHolder; - -/** - * Extension for {@link DelegatingSecurityContextExecutor} to allow proper - * shutdown at {@link Bean} destruction time. - * - */ -public class CloseableDelegatingSecurityContextExecutor extends DelegatingSecurityContextExecutor { - - private final ThreadPoolExecutor executor; - - /** - * Creates a new {@link CloseableDelegatingSecurityContextExecutor} that - * uses the current {@link SecurityContext} from the - * {@link SecurityContextHolder} at the time the task is submitted. - * - * @param delegate - * the {@link Executor} to delegate to. Cannot be null. - */ - public CloseableDelegatingSecurityContextExecutor(final ThreadPoolExecutor delegate) { - super(delegate); - executor = delegate; - } - - /** - * Initiates an orderly shutdown in which previously submitted tasks are - * executed, but no new tasks will be accepted. - */ - public void shutdown() { - executor.shutdown(); - } - - /** - * Initiates an immediate shutdown. - * - * @return a list of the tasks that were awaiting execution - */ - public List shutdownNow() { - return executor.shutdownNow(); - } - -} diff --git a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/ExecutorAutoConfiguration.java b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/ExecutorAutoConfiguration.java index d5bcdd0f1..4fd55cbaa 100644 --- a/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/ExecutorAutoConfiguration.java +++ b/hawkbit-autoconfigure/src/main/java/org/eclipse/hawkbit/autoconfigure/scheduling/ExecutorAutoConfiguration.java @@ -25,6 +25,7 @@ import org.springframework.core.task.TaskExecutor; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.security.concurrent.DelegatingSecurityContextExecutor; import com.google.common.util.concurrent.ThreadFactoryBuilder; @@ -42,12 +43,21 @@ public class ExecutorAutoConfiguration { private AsyncConfigurerThreadpoolProperties asyncConfigurerProperties; /** - * @return ExecutorService for general purpose multi threaded operations. - * Tries an orderly shutdown when destroyed. + * @return ExecutorService with security context availability in thread + * execution.. */ - @Bean(destroyMethod = "shutdown") + @Bean @ConditionalOnMissingBean public Executor asyncExecutor() { + return new DelegatingSecurityContextExecutor(threadPoolExecutor()); + } + + /** + * @return central ThreadPoolExecutor for general purpose multi threaded + * operations. Tries an orderly shutdown when destroyed. + */ + @Bean(destroyMethod = "shutdown") + public ThreadPoolExecutor threadPoolExecutor() { final BlockingQueue blockingQueue = new ArrayBlockingQueue<>( asyncConfigurerProperties.getQueuesize()); final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(asyncConfigurerProperties.getCorethreads(), @@ -57,21 +67,21 @@ public class ExecutorAutoConfiguration { threadPoolExecutor.setRejectedExecutionHandler((r, executor) -> LOGGER.warn( "Reject runnable for centralExecutorService, reached limit of queue size {}", executor.getQueue().size())); - return new CloseableDelegatingSecurityContextExecutor(threadPoolExecutor); + + return threadPoolExecutor; } /** - * @return the executor for UI background processes. Run immediate shutdown - * when destroyed. + * @return the executor for UI background processes. */ - @Bean(name = "uiExecutor", destroyMethod = "shutdownNow") + @Bean(name = "uiExecutor") @ConditionalOnMissingBean(name = "uiExecutor") public Executor uiExecutor() { final BlockingQueue blockingQueue = new ArrayBlockingQueue<>(20); final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 10000, TimeUnit.MILLISECONDS, blockingQueue, new ThreadFactoryBuilder().setNameFormat("ui-executor-pool-%d").build()); threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); - return new CloseableDelegatingSecurityContextExecutor(threadPoolExecutor); + return new DelegatingSecurityContextExecutor(threadPoolExecutor); } /**