作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个简单的单步 Spring 批处理作业,它从数据库中读取项目、处理它们并将结果写入 csv。在运行时,我最终得到一个
org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to
相关代码:
@Configuration
@EnableBatchProcessing
@EnableAutoConfiguration
public class CleanEmailJob {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
public DataSource dataSource;
@Bean
public ResourcelessTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
@Bean
public MapJobRepositoryFactoryBean mapJobRepositoryFactory(ResourcelessTransactionManager txManager)
throws Exception {
MapJobRepositoryFactoryBean factory = new MapJobRepositoryFactoryBean(txManager);
factory.afterPropertiesSet();
return factory;
}
@Bean
public JobRepository jobRepository(MapJobRepositoryFactoryBean factory) throws Exception {
return factory.getObject();
}
@Bean
public JobExplorer jobExplorer(MapJobRepositoryFactoryBean factory) {
return new SimpleJobExplorer(factory.getJobInstanceDao(), factory.getJobExecutionDao(),
factory.getStepExecutionDao(), factory.getExecutionContextDao());
}
@Bean
public SimpleJobLauncher jobLauncher(JobRepository jobRepository) {
SimpleJobLauncher launcher = new SimpleJobLauncher();
launcher.setJobRepository(jobRepository);
return launcher;
}
@Bean
public Job cleanEmailAddressesJob() throws Exception {
return jobBuilderFactory.get("cleanEmailAddresses")
.incrementer(new RunIdIncrementer())
.start(processEmailAddresses())
.build();
}
@Bean
public Step processEmailAddresses() throws UnexpectedInputException, ParseException, Exception {
return stepBuilderFactory.get("processAffiliates")
.<AffiliateEmailAddress, VerifiedAffiliateEmailAddress> chunk(10)
.reader(reader())
.processor(processor())
.writer(report())
.build();
}
@Bean
public ItemWriter<VerifiedAffiliateEmailAddress> report(){
FlatFileItemWriter<VerifiedAffiliateEmailAddress> reportWriter = new FlatFileItemWriter<VerifiedAffiliateEmailAddress>();
reportWriter.setResource(new ClassPathResource("report.csv"));
DelimitedLineAggregator<VerifiedAffiliateEmailAddress> delLineAgg = new DelimitedLineAggregator<VerifiedAffiliateEmailAddress>();
delLineAgg.setDelimiter(",");
BeanWrapperFieldExtractor<VerifiedAffiliateEmailAddress> fieldExtractor = new BeanWrapperFieldExtractor<VerifiedAffiliateEmailAddress>();
fieldExtractor.setNames(new String[] {"uniekNr", "reason"});
delLineAgg.setFieldExtractor(fieldExtractor);
reportWriter.setLineAggregator(delLineAgg);
reportWriter.setShouldDeleteIfExists(true);
return reportWriter;
}
如文档中所述,我希望生命周期事件(打开、关闭)能够自动处理,因为我处于单线程和单写入器作业中?
最佳答案
为了详细说明左边的注释,Spring Batch 将在找到任何 ItemStream
实现时自动注册它们,以便在步骤开始时自动打开它们。使用 java config 时,Spring 只知道返回类型是什么。由于您要返回 ItemReader
,我们不知道您的实现是否也实现了 ItemStream
。使用 java 配置时,我通常建议返回已知的实现(而不是接口(interface))。这允许 Spring 对其进行全面的内省(introspection)。因此,在此示例中,返回 FlatFileItemReader
而不是 ItemReader
将解决问题。
关于java - Spring Batch WriterNotOpenException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43043554/
我创建了一个简单的单步 Spring 批处理作业,它从数据库中读取项目、处理它们并将结果写入 csv。在运行时,我最终得到一个 org.springframework.batch.item.Write
我创建了一个简单的单步 Spring 批处理作业,它从数据库中读取项目、处理它们并将结果写入 csv。在运行时,我最终得到一个 org.springframework.batch.item.Write
我是一名优秀的程序员,十分优秀!