How to Apply a Spring Batch RetryTemplate to a Chunk based Step / ItemWriter?

254 Views Asked by At

I am working on a Spring Batch Program, that should Read a CSV file, Process it and then send it insert its content into a Database. The Jobs are startet via rest-Joblauncher (in case that helps).

Its working most of the Time, but the SQL Connection sometimes sends an unknown "CannotCreateTransactionException" (see this post). As a workaround i want the BatchWriter to retry the writing process at least three times before it finally sends the Email.

my step Configuration :

    @Bean
public Step stepTwo() {
    StepBuilder stepBuilder = stepBuilderFactory.get("Datenimport: " + currentJobName);

    SimpleStepBuilder<TransferLsAuftrag, TransferLsAuftrag> chunk = stepBuilder
            .<TransferLsAuftrag, TransferLsAuftrag>chunk(1337);

    chunk.reader(reader());
    chunk.processor(processor());
    chunk.writer(writer());

    return chunk.build();
}

the writer implementation:

public ItemWriter<TransferLsAuftrag> writer() {
    final JdbcBatchItemWriter<TransferLsAuftrag> writer = new JdbcBatchItemWriter<TransferLsAuftrag>();
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<TransferLsAuftrag>());
    writer.setSql(
            "<long sql insert Statement>");
    writer.setDataSource(dataSource);

    return writer;
}

my custom Listener that sends (annoying) emails when the Job Fails:

    @Override
public void afterJob(JobExecution jobExecution) {
    String jobName = jobExecution.getJobInstance().getJobName();

    switch (jobExecution.getStatus()) {
    case COMPLETED:
        LOG.info(jobName + " ERFOLGREICH BEENDET");
        successfullJobs += 1;
        break;

    case STOPPED:
        // LOG.info(jobName + " wurde gestoppt");
        stoppedJobs.add(jobExecution.toString());
        break;

    case FAILED:
        failedJobs.add(jobExecution.getJobInstance().getJobName());
        Email.sendEmail("Information: " + jobName + " ist fehlgeschlagen !", jobExecution.toString());
        break;

    default:
        Email.sendEmail("Information: " + jobName + " ist fehlgeschlagen !", jobExecution.toString());
        break;
    }

I hope you guys can help me. Thank You !

0

There are 0 best solutions below