Spring Boot异步+多线程关闭但未完成所有任务

时间:2019-06-25 12:37:32

标签: java spring-boot

Spring Boot在没有完成所有任务的情况下关闭。

我想从MYSQL数据库中收集数据,然后将这些数据导出为csv文件。但是,Spring引导会在一段时间后关闭。.

我尝试按照示例HereHere进行操作,但看起来像

 CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()])).join();

不要等到所有任务都完成。

注意:我有150多个任务,需要将数据导出到CSV文件。

代码:

MainApplication.java

@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);
   }
}

TaskRunner.java

@Service
public class TaskRunner {

@Autowired
public DataCollector dataCollector;

public void executeTask() throws Exception {

  final List<String> parameters = dataCollector.getParameters();
  List<CompletableFuture> tasks = new ArrayList<CompletableFuture>();

  for (String name : parameters) {
    try{
    tasks.add(CompletableFuture.runAsync(() -> 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");
  }
}

DataCollector.java

public class DataCollector{

@Autowired
public DataRepository dataRepository;

 @Async("ThreadPoolTaskExecutor")
 public CompletableFuture<String> ExportDataToCsv(String tableName){
   // Code To export data to csv
 }
}

DataRepository.java

@Repository
public class DataRepository {

   @Qualifier("jdbcExportService")
   @Autowired
   public JdbcTemplate jdbcTemplate;

   public SqlRowSet getParamData(String param){
    String Statement = "select * FROM " + param;
    return jdbcTemplate.queryForRowSet(Statement);
  }
}

DatabaseConfiguration.java

 @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);
 }
}

输出:

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)

1 个答案:

答案 0 :(得分:0)

我的猜测在读了一点之后,我绝对不是专家。因此,这很可能是错误的。

CompletableFuture#join等待,直到您可以从该CompletableFuture中提取结果值。 CompletableFuture#get引发检查异常,并且可中断,而CompletableFuture#join是不可中断的。但是两者都做同样的事情,从可完成的未来中提取价值,并在需要时阻止,直到他们这样做为止。

我想寻找的是因为要在完成打印后打印某些东西,是使用CompletableFuture#thenAccept,在完成所有操作后将执行“某些操作”。

CompletableFuture<Void> allFutures = CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()]));
allFutures.thenAccept(Void -> System.out.println("All Task Finished"));

我的猜测是,它将设置所有异步任务,然后将其全部传递,然后退出应用程序。

参考:

java-8-completablefuture-in-action

completablefuture-join-vs-get