我正在运行一个Spring批处理作业,每当我将块大小增加到大于1的任何值时,让chunk size = 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();
}
答案 0 :(得分:0)
在上面的代码中,我不小心将原型bean设置为我的单例阅读器。所以只有一个bean并且它被重用,因为当一个块进程发生时,同一个bean被覆盖,并且集合中的所有“n”对象都指向同一个bean。
每次在阅读器内检索新鲜bean或创建新实例都可以解决问题