我有一个包含许多条目的Zip文件,我想用Spring Batch处理它。在阅读文档时,我还没有看到如何基于zip条目拆分执行。
没有Spring Batch
ZipFile zipFile = new ZipFile("./large-files.zip");
Enumeration<? extends ZipEntry> entries = zipFile.entries();
int entriesCount = 0;
while (entries.hasMoreElements()) {
entriesCount++;
processInParallel(zipFile, entries.nextElement());
// executorService.submit(()-> { //do something with entries.nextElement() });
}
如何使用Spring Batch实现这一目标?
答案 0 :(得分:0)
您可以使用spring的TaskExecutor启用并行处理。在您的itemReader中,请确保您有一个同步方法,以确保没有两个线程将从.zip中提取相同文件以进行处理。
/**
* Enables parallel processing based on configured number of threads
*
* @return
*/
@Bean
public TaskExecutor taskExecutor() {
SimpleAsyncTaskExecutor taskExecutor = new SimpleAsyncTaskExecutor();
taskExecutor.setConcurrencyLimit(5);
return taskExecutor;
}
@Bean
protected Step step1() {
return stepBuilderFactory.get("step1")
.<DOMAIN_OBJ, DOMAIN_OBJ>chunk(1).reader(myReader)
.chunk(1).processor(myProcessor).chunk(1).writer(myWriter).chunk(1).taskExecutor(taskExecutor())
.throttleLimit(maxThreads).build();
}