块大小为n的Spring Batch作业仅写入第n行n次

时间:2019-06-14 19:09:20

标签: java spring spring-boot spring-batch

这是我的第一个春季批处理作业,当块大小为1时,我的读取器,处理器,写入器可以正常工作。但是当我将其设为10时,它将仅每10行写入10次,而不是唯一的10行。我了解这肯定是我的bean设置方式存在问题。我尝试将阅读器与@scope(“ prototype”)配合使用,但没有任何区别。我找到了,但是我不确定每次如何在阅读器中获取一个新bean。
    Spring Batch chunk size creating duplicates

在此批处理配置中,我在哪里/如何创建TEstLayout的新实例?

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Bean
    @Qualifier("callTestStep")
    public Step callTestStep(StepBuilderFactory stepBuilders) {
        return stepBuilders.get("callTestStep").tasklet(callTEstPgmTasklet()).build();
    }

    @Bean
    public CallTEstPgm callTEstPgmTasklet() {
        return new CallTEstPgm();
    }

    @Bean
    @Qualifier("clearOutFileStep")
    public Step clearOutFileStep(StepBuilderFactory stepBuilders) {
        return stepBuilders.get("clearOutFile").tasklet(clearOutFileTasklet()).build();
    }

    @Bean
    public ClearOutFile clearOutFileTasklet() {
        return new ClearOutFile();
    }

    // tag::readerwriterprocessor[]
    @Bean
    @Scope("prototype")
    JdbcCollectiveItemReader<TEstLayout> reader(DataSource dataSource) {
        JdbcCollectiveItemReader<TEstLayout> databaseReader = new JdbcCollectiveItemReader<>();
        databaseReader.setDataSource(dataSource);

        databaseReader
                .setSql(“select * from F33416BA order by x_id”)
);

        databaseReader.setRowMapper(new TEstMultitoOneRowMapper());
        return databaseReader;
    }

    @Bean
    public TEstProcessor processor() {
        return new TEstProcessor();
    }

    @Bean
    public JdbcBatchItemWriter<TestBalanceDetailLayout> writer(DataSource dataSource) {
        return new JdbcBatchItemWriterBuilder<TestBalanceDetailLayout>()
                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
                .sql("INSERT INTO " + outFileLib.trim() + "/" + outFile.trim() + " Values (:wholeDetailString)")
                .dataSource(dataSource).build();
    }

    @Bean
    public Job BalanceSummary(JobCompletionNotificationListener listener, @Qualifier("callTestStep") Step callTestStep,
            @Qualifier("clearOutFileStep") Step clearOutFileStep,
            /* Step writeHeader, */ @Qualifier("writeDetailStep") Step writeDetailStep /* , Step writeFooter */) {
        return jobBuilderFactory.get("Balance_Summary_File").incrementer(new RunIdIncrementer()).listener(listener)
                .start(callTestStep).next(clearOutFileStep)
                // .next(writeHeader)
                .next(writeDetailStep)
                // .next(writeFooter)
                .build();
    }

    @Bean
    @Qualifier("writeDetailStep")
    public Step writeDetailStep(JdbcBatchItemWriter<TestBalanceDetailLayout> writer,
            JdbcCollectiveItemReader<TEstLayout> reader, TEstProcessor processor) {
        return stepBuilderFactory.get("writeDetail").<TEstLayout, TestBalanceDetailLayout>chunk(chunkSize)
                .reader(reader).processor(processor).writer(writer).build();
    }
}

1 个答案:

答案 0 :(得分:0)

这不是春豆的问题!这是我的坏。块处理一直将数据添加到数组列表中,直到达到块大小为止。在我的处理器类中,我在process方法之外创建了输出实例。因此,在添加到arraylist时,它一直替换整个arraylist。

这就是我的块处理器正在发生的事情。 All elements of An ArrayList change when a new one is added?

我的处理器

  public class TestProcessor implements ItemProcessor<TestLayout, TestBalanceDetailLayout> {
                TestBalanceDetailLayout transformedDetail = new TestDetailLayout();
                @Override
            public TestBalanceDetailLayout process(final TestLayout testLayout) throws Exception {  processor code }

正确的代码

      public class TestProcessor implements ItemProcessor<TestLayout, TestBalanceDetailLayout> {

                @Override
            public TestBalanceDetailLayout process(final TestLayout testLayout) throws Exception {
                TestBalanceDetailLayout transformedDetail = new TestDetailLayout();
processor code }