From 53313fdbc3f6db90054abf58f6046530c55f68ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Willi=20Sch=C3=B6nborn?= Date: Mon, 15 Jan 2018 14:02:40 +0100 Subject: [PATCH] Restructured auto configuration --- .../spring/TracerAsyncAutoConfiguration.java | 40 --------- .../spring/TracerAutoConfiguration.java | 83 +++++++++++++++++++ .../TracerSchedulingAutoConfiguration.java | 72 ---------------- .../main/resources/META-INF/spring.factories | 5 +- .../zalando/tracer/spring/Application.java | 5 +- 5 files changed, 85 insertions(+), 120 deletions(-) delete mode 100644 tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAsyncAutoConfiguration.java delete mode 100644 tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerSchedulingAutoConfiguration.java diff --git a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAsyncAutoConfiguration.java b/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAsyncAutoConfiguration.java deleted file mode 100644 index 3f34e4be..00000000 --- a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAsyncAutoConfiguration.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.zalando.tracer.spring; - -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Primary; -import org.springframework.core.task.TaskExecutor; -import org.springframework.scheduling.annotation.Async; -import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; -import org.zalando.tracer.Tracer; - -import java.util.concurrent.ExecutorService; - -import static java.util.concurrent.Executors.newCachedThreadPool; -import static org.springframework.aop.interceptor.AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME; -import static org.zalando.tracer.concurrent.TracingExecutors.tryPreserve; - -@Configuration -@ConditionalOnClass(Async.class) -@ConditionalOnProperty(name = "tracer.async.enabled", havingValue = "true", matchIfMissing = true) -@ConditionalOnMissingBean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME) -@AutoConfigureAfter(TracerAutoConfiguration.class) -public class TracerAsyncAutoConfiguration { - - @Bean(destroyMethod = "shutdown") - @ConditionalOnMissingBean(name = "taskExecutorService") - public ExecutorService taskExecutorService() { - return newCachedThreadPool(); - } - - @Primary - @Bean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME) - public TaskExecutor taskExecutor(final ExecutorService taskExecutorService, final Tracer tracer) { - return new ConcurrentTaskExecutor(tryPreserve(taskExecutorService, tracer)); - } - -} diff --git a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAutoConfiguration.java b/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAutoConfiguration.java index 4dfb50e0..10e8a24d 100644 --- a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAutoConfiguration.java +++ b/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerAutoConfiguration.java @@ -3,6 +3,8 @@ import org.aspectj.lang.annotation.Aspect; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -15,7 +17,17 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Primary; import org.springframework.core.Ordered; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.annotation.Async; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.scheduling.annotation.SchedulingConfiguration; +import org.springframework.scheduling.annotation.SchedulingConfigurer; +import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; +import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; +import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.zalando.tracer.LoggingTraceListener; import org.zalando.tracer.MDCTraceListener; import org.zalando.tracer.StackedMDCTraceListener; @@ -30,10 +42,18 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledThreadPoolExecutor; import static java.util.Collections.emptyList; +import static java.util.concurrent.Executors.newCachedThreadPool; import static javax.servlet.DispatcherType.ASYNC; import static javax.servlet.DispatcherType.REQUEST; +import static org.springframework.aop.interceptor.AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME; +import static org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.DEFAULT_TASK_SCHEDULER_BEAN_NAME; +import static org.zalando.tracer.concurrent.TracingExecutors.manage; +import static org.zalando.tracer.concurrent.TracingExecutors.tryPreserve; @Configuration @ConditionalOnClass(Tracer.class) @@ -120,4 +140,67 @@ public LoggingTraceListener loggingTraceListener() { new LoggingTraceListener(LoggerFactory.getLogger(category)); } + @Configuration + @ConditionalOnClass(Scheduled.class) + @ConditionalOnProperty(name = "tracer.scheduling.enabled", havingValue = "true", matchIfMissing = true) + @AutoConfigureAfter(SchedulingConfiguration.class) + static class SchedulingAutoConfiguration implements SchedulingConfigurer { + + /** + * Needed because we require it inside {@link #configureTasks(ScheduledTaskRegistrar)} already. + */ + @Configuration + static class TaskSchedulerConfiguration { + + @Bean(destroyMethod = "shutdown") + @ConditionalOnMissingBean(name = "taskSchedulerService") + public ScheduledExecutorService taskSchedulerService( + @Value("${tracer.scheduling.pool-size:0}") final int poolSize) { + final int corePoolSize = poolSize > 0 ? poolSize : Runtime.getRuntime().availableProcessors(); + return new ScheduledThreadPoolExecutor(corePoolSize); + } + + @Bean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME) + @ConditionalOnMissingBean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME) + public TaskScheduler taskScheduler(final ScheduledExecutorService taskSchedulerService, final Tracer tracer) { + return new ConcurrentTaskScheduler(manage(taskSchedulerService, tracer)); + } + + } + + private final TaskScheduler scheduler; + + @Autowired + public SchedulingAutoConfiguration( + @Qualifier(DEFAULT_TASK_SCHEDULER_BEAN_NAME) final TaskScheduler scheduler) { + this.scheduler = scheduler; + } + + @Override + public void configureTasks(final ScheduledTaskRegistrar registrar) { + registrar.setTaskScheduler(scheduler); + } + + } + + @Configuration + @ConditionalOnClass(Async.class) + @ConditionalOnProperty(name = "tracer.async.enabled", havingValue = "true", matchIfMissing = true) + static class AsyncAutoConfiguration { + + @Bean(destroyMethod = "shutdown") + @ConditionalOnMissingBean(name = "taskExecutorService") + public ExecutorService taskExecutorService() { + return newCachedThreadPool(); + } + + @Primary + @Bean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME) + @ConditionalOnMissingBean(name = DEFAULT_TASK_EXECUTOR_BEAN_NAME) + public TaskExecutor taskExecutor(final ExecutorService taskExecutorService, final Tracer tracer) { + return new ConcurrentTaskExecutor(tryPreserve(taskExecutorService, tracer)); + } + + } + } diff --git a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerSchedulingAutoConfiguration.java b/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerSchedulingAutoConfiguration.java deleted file mode 100644 index 8ca43e3f..00000000 --- a/tracer-spring-boot-starter/src/main/java/org/zalando/tracer/spring/TracerSchedulingAutoConfiguration.java +++ /dev/null @@ -1,72 +0,0 @@ -package org.zalando.tracer.spring; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; -import org.springframework.scheduling.TaskScheduler; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.scheduling.annotation.SchedulingConfiguration; -import org.springframework.scheduling.annotation.SchedulingConfigurer; -import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; -import org.springframework.scheduling.config.ScheduledTaskRegistrar; -import org.zalando.tracer.Tracer; - -import java.util.concurrent.RejectedExecutionHandler; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.ScheduledThreadPoolExecutor; -import java.util.concurrent.ThreadPoolExecutor; - -import static java.util.concurrent.Executors.newScheduledThreadPool; -import static org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.DEFAULT_TASK_SCHEDULER_BEAN_NAME; -import static org.zalando.tracer.concurrent.TracingExecutors.manage; - -@Configuration -@ConditionalOnClass(Scheduled.class) -@ConditionalOnProperty(name = "tracer.scheduling.enabled", havingValue = "true", matchIfMissing = true) -@AutoConfigureAfter({TracerAutoConfiguration.class, SchedulingConfiguration.class}) -@Import(TracerSchedulingAutoConfiguration.TaskSchedulerConfiguration.class) -public class TracerSchedulingAutoConfiguration implements SchedulingConfigurer { - - /** - * Needed because we require it inside {@link #configureTasks(ScheduledTaskRegistrar)} already. - */ - @Configuration - @ConditionalOnMissingBean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME) - public static class TaskSchedulerConfiguration { - - @Bean(destroyMethod = "shutdown") - @ConditionalOnMissingBean(name = "taskSchedulerService") - public ScheduledExecutorService taskSchedulerService( - @Value("${tracer.scheduling.pool-size:0}") final int poolSize) { - final int corePoolSize = poolSize > 0 ? poolSize : Runtime.getRuntime().availableProcessors(); - return new ScheduledThreadPoolExecutor(corePoolSize); - } - - @Bean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME) - public TaskScheduler taskScheduler(final ScheduledExecutorService taskSchedulerService, final Tracer tracer) { - return new ConcurrentTaskScheduler(manage(taskSchedulerService, tracer)); - } - - } - - private final TaskScheduler scheduler; - - @Autowired - public TracerSchedulingAutoConfiguration( - @Qualifier(DEFAULT_TASK_SCHEDULER_BEAN_NAME) final TaskScheduler scheduler) { - this.scheduler = scheduler; - } - - @Override - public void configureTasks(final ScheduledTaskRegistrar registrar) { - registrar.setTaskScheduler(scheduler); - } - -} diff --git a/tracer-spring-boot-starter/src/main/resources/META-INF/spring.factories b/tracer-spring-boot-starter/src/main/resources/META-INF/spring.factories index 73bb21b5..9aa10ec9 100644 --- a/tracer-spring-boot-starter/src/main/resources/META-INF/spring.factories +++ b/tracer-spring-boot-starter/src/main/resources/META-INF/spring.factories @@ -1,4 +1 @@ -org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ -org.zalando.tracer.spring.TracerAutoConfiguration,\ -org.zalando.tracer.spring.TracerSchedulingAutoConfiguration,\ -org.zalando.tracer.spring.TracerAsyncAutoConfiguration \ No newline at end of file +org.springframework.boot.autoconfigure.EnableAutoConfiguration = org.zalando.tracer.spring.TracerAutoConfiguration diff --git a/tracer-spring-boot-starter/src/test/java/org/zalando/tracer/spring/Application.java b/tracer-spring-boot-starter/src/test/java/org/zalando/tracer/spring/Application.java index c3efcbc3..db6edf9c 100644 --- a/tracer-spring-boot-starter/src/test/java/org/zalando/tracer/spring/Application.java +++ b/tracer-spring-boot-starter/src/test/java/org/zalando/tracer/spring/Application.java @@ -4,9 +4,6 @@ import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling -@ImportAutoConfiguration({ - TracerAutoConfiguration.class, - TracerSchedulingAutoConfiguration.class -}) +@ImportAutoConfiguration(TracerAutoConfiguration.class) public class Application { }