Spring Batch 块大小创建重复项

Spring Batch 块大小创建重复项

IT小君   2022-09-15T08:25:15

我正在运行一个 Spring 批处理作业,每当我将块大小增加到大于 1 的任何值时,假设块大小 = n,它只会复制“nth”记录“n”次,而不是添加 n 个记录。下面是我的配置,请大家指点一下?

@Bean
public JobLauncher jobLauncher(JobRepository jobRepository){
    SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
    jobLauncher.setJobRepository(jobRepository);
    return jobLauncher;
}


@Bean
public JobRepository jobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception{
    JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
    factoryBean.setDataSource(dataSource);
    factoryBean.setTransactionManager(transactionManager);
    factoryBean.setDatabaseType("mysql");
    return factoryBean.getObject();

}


@Bean
public PlatformTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public DataSource dataSource(){
    DriverManagerDataSource datasource = new DriverManagerDataSource();
    datasource.setDriverClassName("com.mysql.jdbc.Driver");
    datasource.setUrl("jdbc:mysql://localhost:3306/mysqltest?useSSL=false");
    datasource.setUsername("user");
    datasource.setPassword("pwd");
    return datasource;
}


@Bean
@Autowired
public Job projectMasterJob(JobBuilderFactory jobs, Step projectMasterDump) {
    return jobs.get("projectMasterJob").incrementer(new RunIdIncrementer())
            .flow(projectMasterDump).end().build();
}


@Bean
@Autowired
public Step projectMasterDump(StepBuilderFactory stepBuilderFactory,
                    FlatFileItemReader projectMasterReader, ItemWriter projectMasterDbWriter) {
    return stepBuilderFactory.get("projectMasterDump")
            .chunk(10).reader(projectMasterReader).writer(projectMasterDbWriter).build();
}

@Bean
@Autowired
public ItemWriter projectMasterDbWriter(DataSource dataSource){
    JdbcBatchItemWriter<ProjectMasterDTO> writer = new JdbcBatchItemWriter<ProjectMasterDTO>();
    writer.setSql(sql);
    writer.setDataSource(dataSource);
    writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
    return writer;
}

@Bean
@Autowired
public FlatFileItemReader projectMasterReader(ProjectMasterDTO projectMasterDTO, DataFormatter dataFormatter){
    ExcelItemReader<ProjectMasterDTO> reader = new ExcelItemReader<>();
    reader.setDataFormatter(dataFormatter);
    reader.setBean(projectMasterDTO);
    reader.setMapperFile(new File("../instance-config/projectMapper.json"));
    return reader;
}


@Bean
@Scope("prototype")
public ProjectMasterDTO projectMasterDTO(){
    return new ProjectMasterDTO();
}
服务器费用不足...
评论(1)
IT小君

在上面的代码中,我不小心将原型 bean 设置为我的单例阅读器。所以只有一个 bean 并且它正在被重用,因此当一个块过程发生时,同一个 bean 被覆盖并且集合中的所有“n”个对象都指向同一个 bean。

每次在阅读器中检索一个新的 bean 或创建一个新实例都可以解决问题

2022-09-15T08:25:15   回复