package de.rwth.idsg.bikeman.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

import de.rwth.idsg.bikeman.async.ExceptionHandlingAsyncTaskExecutor;

@Configuration
@EnableAsync
@EnableScheduling
@Profile("!" + Constants.SPRING_PROFILE_FAST)
public class AsyncConfiguration implements AsyncConfigurer, EnvironmentAware {

    private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class);

    private RelaxedPropertyResolver propertyResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "async.");
    }

    @Override
    @Bean
    public Executor getAsyncExecutor() {
        log.debug("Creating Async Task Executor");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(propertyResolver.getProperty("corePoolSize", Integer.class, 2));
        executor.setMaxPoolSize(propertyResolver.getProperty("maxPoolSize", Integer.class, 50));
        executor.setQueueCapacity(propertyResolver.getProperty("queueCapacity", Integer.class, 10000));
        executor.setThreadNamePrefix("bikeman-Executor-");
        return new ExceptionHandlingAsyncTaskExecutor(executor);
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new SimpleAsyncUncaughtExceptionHandler();
    }
}

@@ -31,16 +31,149 @@
 onfig;%0A%0A
+import com.google.common.util.concurrent.ThreadFactoryBuilder;%0Aimport de.rwth.idsg.bikeman.async.ExceptionHandlingAsyncTaskExecutor;%0A
 import o
@@ -185,24 +185,24 @@
 f4j.Logger;%0A
-
 import org.s
@@ -972,24 +972,60 @@
 kExecutor;%0A%0A
+import javax.annotation.PreDestroy;%0A
 import java.
@@ -1054,77 +1054,189 @@
 or;%0A
-%0A
 import 
-de.rwth.idsg.bikeman.async.ExceptionHandlingAsyncTaskExecutor
+java.util.concurrent.ExecutorService;%0Aimport java.util.concurrent.ScheduledThreadPoolExecutor;%0Aimport java.util.concurrent.ThreadFactory;%0Aimport java.util.concurrent.TimeUnit
 ;%0A%0A@
@@ -1489,16 +1489,66 @@
 lass);%0A%0A
+    private ScheduledThreadPoolExecutor executor;%0A
     priv
@@ -2463,24 +2463,24 @@
 Handler() %7B%0A
-
         retu
@@ -2530,11 +2530,1172 @@
 );%0A    %7D
+%0A%0A    /**%0A     * TODO:%0A     * In an ideal world, we should converge getAsyncExecutor() and scheduledExecutorService(),%0A     * so that we have only one global executor service for the whole application%0A     */%0A    @Bean%0A    public ScheduledThreadPoolExecutor scheduledExecutorService() %7B%0A        ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat(%22bikeman-Executor-Service-%25d%22)%0A                                                                .build();%0A%0A        executor = new ScheduledThreadPoolExecutor(5, threadFactory);%0A        return executor;%0A    %7D%0A%0A    @PreDestroy%0A    public void shutDown() %7B%0A        if (executor != null) %7B%0A            gracefulShutDown(executor);%0A        %7D%0A    %7D%0A%0A    private void gracefulShutDown(ExecutorService executor) %7B%0A        try %7B%0A            executor.shutdown();%0A            executor.awaitTermination(30, TimeUnit.SECONDS);%0A%0A        %7D catch (InterruptedException e) %7B%0A            log.error(%22Termination interrupted%22, e);%0A%0A        %7D finally %7B%0A            if (!executor.isTerminated()) %7B%0A                log.warn(%22Killing non-finished tasks%22);%0A            %7D%0A            executor.shutdownNow();%0A        %7D%0A    %7D
 %0A%7D%0A

add custom scheduled executor service