Home > other >  Springboot batch - Using JdbcPagingItemReader and the job is getting is getting hung up when there i
Springboot batch - Using JdbcPagingItemReader and the job is getting is getting hung up when there i

Time:05-05

I am having a problem with what looks like a method using a JdbcPagingItemReader getting hung up.

All works great when there is information in the reader to return but when the below select operates when there is no information to return, things don't work that great (i.e. the job hangs).

Also, when the taskExecutor (and throttleLimit) steps are removed, the reader step runs to completion and all looks good even when there is no information to select. With this in mind, I am not sure what role multi-threading is playing in this (if at all) or if my reader method is deficient.

In the below example, you will see that the select is pretty simple as it reads from one table, joins to another, and selects the rows with a certain status and within a given date range.

Additional information if this helps...

Springboot version 2.5.6, using java 17, and MySql for the database.

Any suggestions or insight you are able to offer is appreciated as I am stumped at the moment.

Thank you.


@Configuration
@EnableBatchProcessing
public class BuildOpenStatusConfigurationJob {

    @Autowired
    JobBuilderFactory jobBuilderFactory;

    @Autowired
    StepBuilderFactory stepBuilderFactory;

    @Autowired
    DataSource dataSource;
....

    @Bean
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(12);
        executor.setMaxPoolSize(16);
        executor.setQueueCapacity(12);
        executor.initialize();
        return executor;
    }

    @Bean
    public Step createStsActivity() {
        return this.stepBuilderFactory.get("createStsActivity")
                .<StsActivity, StsActivity>chunk(1000)
                .reader(stsActivityItemReader(null, null))
                .processor(stsActivityItemProcessor())
                .writer(stsActivityItemWriter())
                .taskExecutor(taskExecutorRows())
                .throttleLimit(16)
                .listener(statusStepExecutionListener())
                .build();
    }

    @Bean
    @StepScope
    public JdbcPagingItemReader<StsActivity> stsActivityItemReader(
            @Value("#{jobParameters['lowerDateThreshold']}") String lowerDateThreshold,
            @Value("#{jobParameters['upperDateThreshold']}") String upperDateThreshold) {

        JdbcPagingItemReader<StsActivity> pagingItemReader = new JdbcPagingItemReader<>();

        pagingItemReader.setDataSource(dataSource);
        pagingItemReader.setFetchSize(10000);
        pagingItemReader.setPageSize(10000);
        pagingItemReader.setRowMapper(new BeanPropertyRowMapper<>(StsActivity.class));

        MySqlPagingQueryProvider mySqlPagingQueryProvider = new MySqlPagingQueryProvider();

        mySqlPagingQueryProvider.setSelectClause(ta.statusId as statusId, tb.eventDate as maxEventDate, tb.id as maxEventId ");
        mySqlPagingQueryProvider.setFromClause("FROM TableA ta" 
                   "join TableB tb on tb.statusId = ta.statusId");
        mySqlPagingQueryProvider.setWhereClause("WHERE tb.status = 'OPEN' "
                  "and (date(tb.eventDate) >= date('"   lowerDateThreshold   "') "
                  "and date(tb.eventDate) < date('"   upperDateThreshold   "'))");

        Map<String, Order> orderByKeys = new HashMap<>();
        orderByKeys.put("rbpai.statusId", Order.ASCENDING);
        mySqlPagingQueryProvider.setSortKeys(orderByKeys);

        pagingItemReader.setQueryProvider(mySqlPagingQueryProvider);
        
        return pagingItemReader;
    }

CodePudding user response:

This has been fixed in v4.3.4: https://github.com/spring-projects/spring-batch/issues/3898.

With Spring Boot 2.5.6, you get Spring Batch v4.3.3 which does not contain the fix.

If you upgrade to a Boot version that brings Batch 4.3.4 , your issue should be fixed. Otherwise, it is probably a regression. In this case, please add a comment on Github with a minimal complete example that reproduces the issue and we will re-open it for investigation. Thank you upfront.

  • Related