使用 Spring Batch 处理记录时的不同行为 [已关闭]

Different behavior when processing records with Spring Batch [closed]

提问人:igarciadev 提问时间:11/14/2023 更新时间:11/14/2023 访问量:28

问:


想改进这个问题吗?通过编辑这篇文章添加详细信息并澄清问题。

4天前关闭。

在Spring Batch项目中,与后续记录相比,我在处理第一个块中的记录时遇到了不同的行为。在 InputReader 中读取数据库时检索到的每个对象 (Input) 都有一个子对象 (InputEvent)。在 InputProcessor 类中,将向每个 Input 对象添加一个 id = null 的新 InputEvent 对象。新的 InputEvent 将保持其 null ID,直到执行数据库查询。此时,null id 将替换为表中的自动递增值。所有这些都发生在处理器中。

执行查询前对象检查 (repository.findFirstByOrderByFechaEjecucionDesc()):

enter image description here

执行查询后对象检查 (repository.findFirstByOrderByFechaEjecucionDesc()):

enter image description here

此行为仅发生在处理的第一个区块的记录中;它不会在后续块的记录中发生。这种行为可能是什么原因?

pom.xml

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath />

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

BatchConfig.java

@Configuration
@EnableBatchProcessing
public class BatchConfig
{}

BatchCore.java

@Service
public class BatchCore
{
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private InputSkipListener inputSkipListener;

    @Bean
    public Step inputStep(InputReader reader, InputProcessor processor, InputWriter writer)
    {
        //@f:off
        return stepBuilderFactory.get("inputStep")
                .<Input, Input>chunk(20)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .faultTolerant().skipPolicy(new ExceptionSkipPolicy())
                .listener(inputSkipListener)
                .build();
        //@f:on
    }

    @Bean("mainJob")
    public Job mainJob(Step inputStep)
    {
        //@f:off
        return jobBuilderFactory.get("mainJob")
                .incrementer(new RunIdIncrementer())
                .start(inputStep)
                .build();
        //@f:on
    }
}

输入处理器.java

@Component
public class InputProcessor implements ItemProcessor<Input, Input>
{    
    @Autowired
    private RMRepository repository;

    @Override
    public Input process(Input item)
    {
        InputEvent inputEvent = new InputEvent(new State(4));
        if (item.getId() != null)
        {
            inputEvent.setInput(item);
        }

        Date lastDate = repository.findFirstByOrderByFechaEjecucionDesc();
        item.setFechaEjecucion(lastDate);
        
        return item;
    }
}
java mysql spring-boot spring-batch

评论


答: 暂无答案