spring-batch icon indicating copy to clipboard operation
spring-batch copied to clipboard

Chunck Monitor always report a warn about restarting, even if restart is desactivated

Open pkernevez opened this issue 1 year ago • 0 comments

Bug description I use a RepositoryItemReader in a multi-thread scope (with a task executor) RepositoryItemReader is thread safe, and as documented I set up : .saveState(false) and manage my state in my own code.

Environment Java 21 SpringBoot 3.3.0 SpringBatch 5.1.2 Hibernate 6.5.2

Steps to reproduce Run my Job and the WARN logs are showed: ItemStream was opened in a different thread. Restart data could be compromised.

Expected behavior No warn about state when save state is disabled.

Minimal Complete Reproducible example Please provide a failing test or a minimal complete verifiable example that reproduces the issue. Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

My Step:

    @Bean(name = "snapshotAccountPositionStep")
    @JobScope
    public Step cobAccounts(JobRepository jobRepository,
                            PlatformTransactionManager transactionManager,
                            @Qualifier("batchThreadPool") TaskExecutor taskExecutor,
                            SnapshotAccountPositionProcessor processor,
                            @Qualifier("snapshotListener") SnapshotAccountPositionStepListener snapshotListener,
                            @Value("${job.skipLimit}") int skipLimit,
                            @Value("${job.chunkSize}") int chunkSize) {
        return new StepBuilder("snapshotAccountPositionStep", jobRepository)
                .<TSIDResult, String>chunk(chunkSize, transactionManager)
                .faultTolerant()
                .skip(JobErrorStepListener.JobProcessingException.class)
                .skipLimit(skipLimit)
                .taskExecutor(taskExecutor)
                .reader(workAccountReader(null, null, null))
                .processor(processor)
                .listener(snapshotListener)
                .listener(getPatchListener())
                .writer(accountWriter())
                .listener(getErrorListener(null, null))
                .build();
    }

My reader:

    @Bean
    @StepScope
    public RepositoryItemReader<TSIDResult> workAccountReader(CobAccountWorkRepository repository,
                                                                 @Value("${positionSnapshot.readerPageSize}") Integer readerPageSize,
                                                                 @Value("#{jobParameters['date']}") LocalDate date) {
        return new RepositoryItemReaderBuilder<TSIDResult>()
                .name("workAccountReader")
                .saveState(false)
                .repository(repository)
                .methodName("findByStatusInAndDate")
                .arguments(List.of(TO_DO, DONE), date)
                .sorts(Collections.singletonMap(date.ID, Sort.Direction.ASC))
                .pageSize(readerPageSize)
                .build();
    }
     // ChunkMonitor.java
     private ChunkMonitorData getData() {
		ChunkMonitorData data = holder.get();
		if (data == null) {
			if (streamsRegistered) {
				logger.warn("ItemStream was opened in a different thread.  Restart data could be compromised.");
			}
			data = new ChunkMonitorData(0, 0);
			holder.set(data);
		}
		return data;
	}

pkernevez avatar Aug 06 '24 09:08 pkernevez