title: Spring boot Async + multithreading shuts down without completing all the tasks
body: <p>Spring boot shuts down without completing all the task.</p>
  
  <p>I want to collect the data from the MYSQL database and then export this data as csv file.. However Spring boot shuts down after some time..</p>
  
  <p>I tried following the example <a href="https://dzone.com/articles/spring-boot-creating-asynchronous-methods-using-as" rel="nofollow noreferrer">Here</a> and <a href="https://stackoverflow.com/questions/47351435/spring-async-with-completablefuture">Here</a> but it seems like </p>
  
  <pre><code> CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()])).join();
  </code></pre>
  
  <p>doesnot wait untill all the tasks is finished.</p>
  
  <p>NOTE: I have over 150 task for which i need to export data to the CSV file.</p>
  
  <p>Code: </p>
  
  <p>MainApplication.java</p>
  
  <pre><code>@SpringBootApplication
  @EnableAsync
  public class MainApplication implements CommandLineRunner {
  
  @Autowired
  private TaskRunner taskrunner;
  
  public static void main(String[] args) throws Exception{
      SpringApplication application = new SpringApplication(MainApplication.class);
      application.setBannerMode(Banner.Mode.OFF);
      application.run(args);
    }
  
   public void run(String... args) throws Exception {
      taskrunner.executeTasks();
      exit(0);
     }
  }
  </code></pre>
  
  <p>TaskRunner.java</p>
  
  <pre><code>@Service
  public class TaskRunner {
  
  @Autowired
  public DataCollector dataCollector;
  
  public void executeTask() throws Exception {
  
    final List&lt;String&gt; parameters = dataCollector.getParameters();
    List&lt;CompletableFuture&gt; tasks = new ArrayList&lt;CompletableFuture&gt;();
  
    for (String name : parameters) {
      try{
      tasks.add(CompletableFuture.runAsync(() -&gt; dataCollector.ExportDataToCsv(name)));
      }catch(){
        ex.printStackTrace();
        System.out.println("Export failed for Param: "+name);
      }
    }
  
    CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()])).join();
  
    System.out.println("All Task Finished");
    }
  }
  </code></pre>
  
  <p>DataCollector.java</p>
  
  <pre><code>public class DataCollector{
  
  @Autowired
  public DataRepository dataRepository;
  
   @Async("ThreadPoolTaskExecutor")
   public CompletableFuture&lt;String&gt; ExportDataToCsv(String tableName){
     // Code To export data to csv
   }
  }
  </code></pre>
  
  <p>DataRepository.java</p>
  
  <pre><code>@Repository
  public class DataRepository {
  
     @Qualifier("jdbcExportService")
     @Autowired
     public JdbcTemplate jdbcTemplate;
  
     public SqlRowSet getParamData(String param){
      String Statement = "select * FROM " + param;
      return jdbcTemplate.queryForRowSet(Statement);
    }
  }
  </code></pre>
  
  <p>DatabaseConfiguration.java</p>
  
  <pre><code> @Configuration
   public class DatabaseConfiguration {
  
  @Bean(name="db")
  @ConfigurationProperties(prefix = "spring.db")
  public DataSource createExportDataSource(){
      return DataSourceBuilder.create().build();
  }
  
  @Bean(name = "jdbcExportService")
  @Autowired
  public JdbcTemplate createJdbcTemplateExportService(@Qualifier("db") DataSource exportServiceDS){
      return new JdbcTemplate(exportServiceDS);
   }
  }
  </code></pre>
  
  <p>Output:   </p>
  
  <pre><code>2019-06-25 14:39:56.868  INFO 13163 --- [       Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'ThreadPoolTaskExecutor'
  2019-06-25 14:39:56.868  INFO 13163 --- [       Thread-2] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
  2019-06-25 14:39:56.997  INFO 13163 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
  2019-06-25 14:39:57.011 DEBUG 13163 --- [       Thread-2] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Before shutdown stats (total=10, active=5, idle=5, waiting=0)
  2019-06-25 14:39:57.371 DEBUG 13163 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection com.mysql.cj.jdbc.ConnectionImpl@2c373ded: (connection evicted)
  2019-06-25 14:39:57.991 DEBUG 13163 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection com.mysql.cj.jdbc.ConnectionImpl@4856fa0b: (connection evicted)
      2019-06-25 14:39:57.994 DEBUG 13163 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection com.mysql.cj.jdbc.ConnectionImpl@375c83e: (connection evicted)
  2019-06-25 14:39:57.995 DEBUG 13163 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection com.mysql.cj.jdbc.ConnectionImpl@417d7b5b: (connection evicted)
  2019-06-25 14:39:57.996 DEBUG 13163 --- [nnection closer] com.zaxxer.hikari.pool.PoolBase          : HikariPool-1 - Closing connection com.mysql.cj.jdbc.ConnectionImpl@77ebb3e9: (connection evicted)
  2019-06-25 14:39:58.094 DEBUG 13163 --- [       Thread-2] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - After shutdown stats (total=0, active=0, idle=0, waiting=0)
  2019-06-25 14:39:58.095  INFO 13163 --- [       Thread-2] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
  2019-06-25 14:39:58.651  WARN 13163 --- [ export-thread1] com.zaxxer.hikari.pool.ProxyConnection   : HikariPool-1 - Connection com.mysql.cj.jdbc.ConnectionImpl@4c18c2a2 marked as broken because of SQLSTATE(08003), ErrorCode(0)
  </code></pre>