Программа Springbatch не завершается изящно после завершения пакетного задания - PullRequest
1 голос
/ 06 марта 2019

У меня есть простой springbatch, который читает данные из БД и записывает их в eventhub. Я использую HikariCP для подключения к БД. Моя партия не заканчивается изящно после завершения работы. Каким-то образом HikariCP реинициализируется самостоятельно.

11:32:55.151 [main] DEBUG o.s.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
11:32:55.193 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Creating new transaction with name [org.springframework.batch.core.repository.support.SimpleJobRepository.update]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
11:32:55.194 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Acquired Connection [HikariProxyConnection@1438178266 wrapping ConnectionID:1 ClientConnectionId: ef2e5d07-0aed-417e-a571-2c9fec2fdf34] for JDBC transaction
11:32:55.194 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Switching JDBC Connection [HikariProxyConnection@1438178266 wrapping ConnectionID:1 ClientConnectionId: ef2e5d07-0aed-417e-a571-2c9fec2fdf34] to manual commit
11:32:55.236 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
11:32:55.236 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT VERSION FROM dse.BATCH_JOB_EXECUTION WHERE JOB_EXECUTION_ID=?]
11:32:55.275 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL query
11:32:55.275 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [SELECT COUNT(*) FROM dse.BATCH_JOB_EXECUTION WHERE JOB_EXECUTION_ID = ?]
11:32:55.303 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL update
11:32:55.303 [main] DEBUG o.s.jdbc.core.JdbcTemplate - Executing prepared SQL statement [UPDATE dse.BATCH_JOB_EXECUTION set START_TIME = ?, END_TIME = ?,  STATUS = ?, EXIT_CODE = ?, EXIT_MESSAGE = ?, VERSION = ?, CREATE_TIME = ?, LAST_UPDATED = ? where JOB_EXECUTION_ID = ? and VERSION = ?]
11:32:55.339 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Initiating transaction commit
11:32:55.339 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Committing JDBC transaction on Connection [HikariProxyConnection@1438178266 wrapping ConnectionID:1 ClientConnectionId: ef2e5d07-0aed-417e-a571-2c9fec2fdf34]
11:32:55.446 [main] DEBUG o.s.j.d.DataSourceTransactionManager - Releasing JDBC Connection [HikariProxyConnection@1438178266 wrapping ConnectionID:1 ClientConnectionId: ef2e5d07-0aed-417e-a571-2c9fec2fdf34] after transaction
11:32:55.447 [main] INFO  o.s.b.c.l.support.SimpleJobLauncher - Job: [FlowJob: [name=migrateEmailJob]] completed with the following parameters: [{run.id=34}] and the following status: [COMPLETED]
11:33:19.316 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=10, active=0, idle=10, waiting=0)
11:33:49.319 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=10, active=0, idle=10, waiting=0)
11:34:19.323 [HikariPool-1 housekeeper] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Pool stats (total=10, active=0, idle=10, waiting=0)

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.1.3.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependency>
   <groupId>com.zaxxer</groupId>
   <artifactId>HikariCP</artifactId>
   <version>3.3.1</version>
</dependency>

1 Ответ

1 голос
/ 06 марта 2019

сделал обходной путь, как показано ниже.

    public class JobCompletionNotificationListener extends JobExecutionListenerSupport {

     @Override
        public void afterJob(JobExecution jobExecution) {
            if (jobExecution.getStatus() == BatchStatus.COMPLETED) {
                   new Thread(() -> {
                    try {
                        houseKeeping.clean();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }

//method in the housekeeping class.

public void clean() throws Exception {
        log.info("Initiate Housekeeping Task");
        Thread.sleep(delayTime);
        jdbcTemplate.getDataSource().getConnection().close();
        eventHubClient.close();
        configurableApplicationContext.close();
        System.exit(0);
    }
...