0

I developed a solution on Master/Slave steps (with chunks) + a ClassifierCompositeItemWriter, but I crash into several issues due to the writer. The first one was

Commit failed while step execution data was already updated. Reverting to old version.

After some surfing on internet, I used the solution by @Mahmoud Ben Hassine https://stackoverflow.com/a/67635289/2794288

(...)
SimpleStepBuilder stepBuilder = stepBuilderFactory.get(NAME_SLAVE_STEP)
            .listener(stepExecutionListener)
            .chunk(searchFeedJobPropertiesProvider.getSendDocumentsChunkSizeByIndex(index))
            .reader(itemReader)
            .processor(itemProcessor)
            .writer(itemWriter)
            .faultTolerant()
            .skip(SearchFeedException.class)
            .skipLimit(searchFeedJobPropertiesProvider.getSendDocumentsSkipLimitByIndex(index))
            .listener(new FeedSkipListener<TotalDoc, TotalDoc>());

    // register writers as streams in the step so that open/update/close are called correctly
    Map<String, FeedItemWriter> beansOfType = applicationContext.getBeansOfType(FeedItemWriter.class);
    for (FeedItemWriter feedItemWriter : beansOfType.values()) {
        stepBuilder.stream((ItemStream) feedItemWriter);
    }
(...)

FeedItemWriter is an interface where classes extended FlatFileItemWriter, like FeedFlatFileItemWriter

(...)
            String name = siteCompanyFeed.getEntityId();
            GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
            beanDefinition.setBeanClassName(FeedFlatFileItemWriter.class.getName());
            MutablePropertyValues propertyValues = new MutablePropertyValues();
            propertyValues.addPropertyValue("name", name);
            propertyValues.addPropertyValue("fileBasePath", fileBasePath);
            propertyValues.addPropertyValue("siteCompanyFeed", siteCompanyFeed);
            beanDefinition.setPropertyValues(propertyValues);
            registry.registerBeanDefinition(name, beanDefinition);
(...)

For itemWriter:

(...)
@Bean(SEND_DOCUMENTS_WRITER)
@StepScope
ClassifierCompositeItemWriter sendDocumentsWriter(ConfigurableApplicationContext applicationContext) {
    // dynamically get writers from the application context and register them as delegates in the composite
    Map<String, FeedItemWriter> beansOfType = applicationContext.getBeansOfType(FeedItemWriter.class);
    // Classify students by group
    Classifier<JsonNode, FeedItemWriter> classifier =
            feed -> beansOfType.get(feed.get(ATTRIBUTE_ID_SITE_COMPANY_FEED).get(ENTITY_ID).asText());
    return new ClassifierCompositeItemWriterBuilder()
            .classifier(classifier)
            .build();
}
(...)

The problem is application cash, an error message appears:

Commit failed while step execution data was already updated. Reverting to old version.

And this one:

ERROR [] org.springframework.batch.core.step.AbstractStep [et.sendDocumentsTaskExecutor-3] - Exception while closing step execution resources in step sendDocumentsSlaveStep in job feedJob traceId=7ec385f0e5af851d, spanId=2dd123a73143a578 java.lang.NullPointerException: Cannot read field "linesWritten" because "this.state" is null

Some lines are writen to file but execution finished with ERROR status.

This problem disappears when none is inserted in .stream method of step:

(...)
SimpleStepBuilder stepBuilder = stepBuilderFactory.get(NAME_SLAVE_STEP)
        .listener(stepExecutionListener)
        .chunk(searchFeedJobPropertiesProvider.getSendDocumentsChunkSizeByIndex(index))
        .reader(itemReader)
        .processor(itemProcessor)
        .writer(itemWriter)
        .faultTolerant()
        .skip(SearchFeedException.class)
        .skipLimit(searchFeedJobPropertiesProvider.getSendDocumentsSkipLimitByIndex(index))
        .listener(new FeedSkipListener<TotalDoc, TotalDoc>());

// register writers as streams in the step so that open/update/close are called correctly
Map<String, FeedItemWriter> beansOfType = applicationContext.getBeansOfType(FeedItemWriter.class);
for (FeedItemWriter feedItemWriter : beansOfType.values()) {
    // stepBuilder.stream((ItemStream) feedItemWriter);
}
(...)

But I bumped into my first error:

Commit failed while step execution data was already updated

Any tip / clue / fix for that problem?

P.D.- H2 embedded DB

Thanks in advance!

mbracero
  • 77
  • 5
  • `java.lang.NullPointerException: Cannot read field "linesWritten" because "this.state" is null` Have you checked why this is happening? Please share the full stack-trace, or better, a [minimal complete example](https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the issue to be able to help you efficiently. – Mahmoud Ben Hassine Aug 29 '23 at 11:23

0 Answers0