我正在并行运行大约 18.000 个弹簧作业,每个作业都有一个步骤。每个步骤都包括从文件中读取、转换和操作这些值并将它们写入 Mongo 和 MySql 数据库,这没什么不寻常的。在所有作业完成后,内存消耗保持在 20GB USED并保持在那里。我按如下方式构建我的春季批次成员:
@Autowired
public ArchiveImportManager(final JobRepository jobRepository, final BlobStorageConfiguration blobConfiguration,
final JobBuilderFactory jobBuilderFactory, final StepBuilderFactory stepBuilderFactory,
final ArchiveImportSettings settings) {
this.jobBuilderFactory = jobBuilderFactory;
this.stepBuilderFactory = stepBuilderFactory;
this.jobLauncher = new SimpleJobLauncher();
final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(THREAD_POOL_SIZE);
threadPoolTaskExecutor.setMaxPoolSize(THREAD_POOL_SIZE);
threadPoolTaskExecutor.setQueueCapacity(THREAD_POOL_QUEUE);
threadPoolTaskExecutor.initialize();
this.jobLauncher.setTaskExecutor(threadPoolTaskExecutor);
this.jobLauncher.setJobRepository(jobRepository);
}
我创建一个工作如下:
private Job createImportJob(final ArchiveResource archiveResource, final int current, final int archiveSize) {
final String name = "ImportArchiveJob[" + current + "|" + archiveSize + "]"
+ new Date(System.currentTimeMillis());
final Step step = this.stepBuilderFactory
.get(name)
.<ArchiveResource, ArchiveImportSaveData> chunk(1)
.reader(getReader(archiveResource, current, archiveSize))
.processor(getProcessor(current, archiveSize))
.writer(getWriter(current, archiveSize))
.build();
return this.jobBuilderFactory
.get(name)
.flow(step)
.end()
.build();
}
并循环启动所有作业:
private void startImportJobs(final List<ArchiveResource> archives) {
final int size = archives.size();
for (int i = 0; i < size; i++) {
final ArchiveResource ar = archives.get(i);
final Job j = createImportJob(ar, i, size);
try {
this.jobLauncher.run(j, new JobParametersBuilder()
.addDate("startDate", new Date(System.currentTimeMillis()))
.addString("progress", "[" + i + "|" + size + "]")
.toJobParameters());
} catch (final JobExecutionAlreadyRunningException e) {
log.info("Already running", e);
} catch (final JobRestartException e) {
log.info("Restarted", e);
} catch (final JobInstanceAlreadyCompleteException e) {
log.info("ALready completed", e);
} catch (final JobParametersInvalidException e) {
log.info("Parameters invalid", e);
}
}
}
我是否必须以某种方式释放内存或在作业完成后删除作业?我不明白为什么内存消耗保持那么高。
此致