Пакет Spring с использованием CompositeItemWriter и CompositeItemProcessor

Используя Spring Batch, я должен писать в двух разных таблицах, но используя один и тот же ItemReader.

Я не могу понять, как использовать один ItemReader и CompositeItemWriter.

Вот JobConfiguration:

      public class JobConfiguration {

    @Autowired
    @SuppressWarnings("squid:S3305")
    private ItemReaderSurveillance itemReaderSurveillance;

    @Autowired
    @SuppressWarnings("squid:S3305")
    private ItemWriterSurveillance itemWriterSurveillance;

    @Autowired
    @SuppressWarnings("squid:S3305")
    private ItemWriterSurveillanceEcheance itemWriterSurveillanceEcheance;

    @Autowired
    @SuppressWarnings("squid:S3305")
    private CompositeItemProcessorSurveillance compositeItemProcessor;

    @Bean(name = "importSurveillanceJob")
    public Job job(JobBuilderFactory jobs) {

        return jobs.get("importSurveillanceStep")
                .listener(jobListener)
                .start(stepTaskletCreationRepertoireReport())
                .next(stepTaskletCreationRepertoireArchive())
                .next(stepSurveillanceReadProcessWrite())
                .next(stepZipFile())
                .build();
    }

    @Bean
    protected Step stepSurveillanceReadProcessWrite() {
        return stepBuilderFactory.get("stepSurveillanceReadProcessWrite")
                .<SurveillanceLineFile, CompositeResultSurveillance>chunk(Integer.valueOf(commitInterval))
                .reader(itemReaderSurveillance)
                .processor(compositeItemProcessor)
                .writer(compositeItemWriter())
                .faultTolerant()
                .retryLimit(0)
                .build();
    }

    @Bean
    public CompositeItemWriter<CompositeResultSurveillance> compositeItemWriter(){
        CompositeItemWriter compositeItemWriter = new CompositeItemWriter();
        compositeItemWriter.setDelegates(Arrays.asList(itemWriterSurveillance, itemWriterSurveillanceEcheance));
        return compositeItemWriter;
    }
}

Авторы предметов:

      @Slf4j
@StepScope
@Component
public class ItemWriterSurveillance implements ItemWriter<FoaSurveillance>, StepExecutionListener {
String fileName;
    JobExecution mJobExecution;
    StepExecution stepExecution;

    @Autowired
    private FoaSurveillanceDao foaSurveillanceDao;

    @Override
    public void write(List<? extends FoaSurveillance> foaSurveillances) {

        ExecutionContext executionContext = stepExecution.getExecutionContext();
        
        // Process data
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        mJobExecution = stepExecution.getJobExecution();
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return ExitStatus.COMPLETED;
    }
}

@Slf4j
@StepScope
@Component
public class ItemWriterSurveillanceEcheance implements ItemWriter<FoaSurveillanceEcheance>, StepExecutionListener {

    String fileName;
    JobExecution mJobExecution;
    StepExecution stepExecution;

    @Autowired
    private FoaSurveillanceEcheanceDao foaSurveillanceEcheanceDao;

    @Override
    public void write(List<? extends FoaSurveillanceEcheance> foaSurveillanceEcheances) {

        ExecutionContext executionContext = stepExecution.getExecutionContext();
        // Process data
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        mJobExecution = stepExecution.getJobExecution();
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return ExitStatus.COMPLETED;
    }
}

Составьте два элемента itemProcessor:

      @Slf4j
@Component
public class CompositeItemProcessorSurveillance implements ItemProcessor<SurveillanceLineFile, CompositeResultSurveillance>, StepExecutionListener {

    private StepExecution stepExecution;

    @Autowired
    ItemProcessorSurveillance itemProcessorSurveillance;

    @Autowired
    ItemProcessorSurveillanceEcheance itemProcessorSurveillanceEcheance;

    @Override
    public CompositeResultSurveillance process(SurveillanceLineFile surveillanceLineFile) throws Exception {
        CompositeResultSurveillance compositeResultSurveillance = new CompositeResultSurveillance();
        compositeResultSurveillance.setFoaSurveillance(itemProcessorSurveillance.process(surveillanceLineFile));
        compositeResultSurveillance.setFoaSurveillanceEcheance(itemProcessorSurveillanceEcheance.process(surveillanceLineFile));
        return compositeResultSurveillance;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}

ItemProcessorSurveillance:

      @Slf4j
@Component
public class ItemProcessorSurveillance implements ItemProcessor<SurveillanceLineFile, FoaSurveillance>, StepExecutionListener {

    String fileName;

    private StepExecution stepExecution;

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return ExitStatus.COMPLETED;
    }

    @Override
    public FoaSurveillance process(SurveillanceLineFile surveillanceLineFile) throws Exception {
        ExecutionContext executionContext = stepExecution.getExecutionContext();
        
        // Process Data
    }

И CompositeResult, возвращаемый процессором:

      @Getter
@Setter
public class CompositeResultSurveillance {

    private FoaSurveillance foaSurveillance;
    private FoaSurveillanceEcheance foaSurveillanceEcheance;
}

На данный момент у меня есть NPE для ItemProcessorSurveillance, потому что stepExecution имеет значение NULL в методе процесса.

Я не могу понять, что случилось. Любая помощь ?

1 ответ

Решение

Это потому, что ваш ItemProcessorSurveillance реализует два интерфейса: и StepExecutionListener но зарегистрирован только как ItemProcessorв шаге. Он также должен быть зарегистрирован как слушатель, чтобы beforeStep вызывается при необходимости для установки stepExecution поле.

Другие вопросы по тегам