我有一个使用Spring Batch框架的Spring Boot应用程序。我的目标很简单-同时运行某个作业。我希望能够同时运行15个线程,并拒绝每一个过多的线程。这是我的配置类:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@EnableBatchProcessing
public class GeneratingReportJobConfiguration {
@Autowired
private GeneratingReportTask task;
@Autowired
private JobRepository jobRepository;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) {
JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor();
jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry);
return jobRegistryBeanPostProcessor;
}
@Bean
public TaskExecutor taskExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(15);
taskExecutor.setMaxPoolSize(15);
taskExecutor.setQueueCapacity(0);
return taskExecutor;
}
@Bean
public JobLauncher jobLauncher(TaskExecutor taskExecutor) {
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
jobLauncher.setTaskExecutor(taskExecutor);
return jobLauncher;
}
@Bean
public Job job() {
return jobBuilderFactory.get("generatingReportJob")
.start(step())
.build();
}
@Bean
public Step step() {
return stepBuilderFactory.get("generatingReportTask")
.tasklet(task)
.build();
}
}
无论我做什么,这个应用程序最多同时运行10个作业。我尝试使用
TaskExecutor
进行实验-我的第一个逻辑选择是SimpleAsyncTaskExecutor
,它似乎会将所有过度请求堆积起来,然后以不希望的随机顺序运行它们。所以后来我试图操纵那个限制,并且如你所见,在代码中我开始使用
ThreadPoolTaskExecutor
,它允许我将限制设置为5,并且它按预期工作-最多同时可以运行5个线程,而在这5个线程正在运行时,后续的线程都会被拒绝。但是,将限制设置为15会导致与上一个类似的行为。仍然会排队第11个请求,而第16个请求则被拒绝。这几乎是我的预期行为,但我需要能够完全控制线程执行。作业通过
@RestController
使用jobLauncher.run()
调用。每个过度请求都会导致浏览器加载,直到能够开始执行。似乎程序在jobLauncher.run()
内部某处被冻结(当能够启动作业执行时,它就退出了-这就是异步运行的方式)。
taskExecutor()
方法下看到这一点。 - dominikbrandonThread.sleep()
30 秒,所以我想这不可能是问题的原因。 - dominikbrandon