spring-batch - Spring批量读取对文件夹中的大量文件进行分区
问题描述
我们的要求是将 XML 加载到 DB。文件夹有超过 50K XML 文件。MultiResourceParitioner 正在创建 50K slaveStep:partitionXXX 并且由于 ThreadPoolTaskExecutor 中的线程数量较少而失败。请告知最好的方法。为 Partitioner、master 和 slave 附加了代码片段。
@Bean("partitioner")
@JobScope
public Partitioner partitioner()throws Exception {
MultiResourcePartitioner partitioner = new MultiResourcePartitioner();
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
partitioner.setResources(resolver.getResources("file:" + resourcesPath + fileType));
partitioner.partition(10);
logger.info("--------partitioner()---end -No of files--->"+ resolver.getResources("file:" + resourcesPath + fileType).length);
return partitioner;
}
@Bean
public Step slaveStep() throws Exception{
return stepBuilderFactory.get("slaveStep")
.<UnifiedInvoiceDTO, UnifiedInvoiceDTO>chunk(1)
.reader(xmlItemReader(null))
.processor(xmlFileItemProcessor())
.writer(writer())
.build();
}
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(100);
taskExecutor.setCorePoolSize(70);
taskExecutor.setQueueCapacity(70);
taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
taskExecutor.afterPropertiesSet();
return taskExecutor;
}
@Bean
@Qualifier("masterStep")
public Step masterStep() throws Exception{
return stepBuilderFactory.get("masterStep")
.partitioner("slaveStep", partitioner())
.step(slaveStep())
.taskExecutor(taskExecutor()).listener(new InvoiceStepListener())
.build();
}
@Bean
@StepScope
@Qualifier("xmlItemReader")
@DependsOn("partitioner")
public StaxEventItemReader<Invoice> xmlItemReader(@Value("#{stepExecutionContext['fileName']}") String filename)throws MalformedURLException {
logger.info("----StaxEventItemReader----fileName--->" + filename);
StaxEventItemReader<Invoice> xmlFileReader = new StaxEventItemReader<>();
xmlFileReader.setStrict(false);
xmlFileReader.setResource(new UrlResource(filename));
xmlFileReader.setFragmentRootElementName("Invoice");
Jaxb2Marshaller invoiceMarshaller = new Jaxb2Marshaller();
invoiceMarshaller.setClassesToBeBound(Invoice.class);
xmlFileReader.setUnmarshaller(invoiceMarshaller);
return xmlFileReader;
}
解决方案
推荐阅读
- mongodb - 如何在颤动的屏幕上显示来自获取请求的图像?
- javascript - 警告:列表中的每个孩子都应该有一个唯一的“键”
- python - 用python win32打开并调整outlook模板邮件
- plupload - 图片未使用 plupload latest (3.1.3) 库调整大小
- html - 如何在没有间距的情况下定位 div 块
- java - 在进一步处理之前更新 InputStream
- flutter - 当我使用 Bloc 模式时,我的无限列表不会滚动
- reactjs - 无法解析 [TypeScript、ReactJS、tsconfig.json] 中的“../../slider/play.svg”
- python - 如何将列表的元素动态分配给满足熊猫特定条件的单元格?
- bash - 需要整数表达式;移动文件时出错