spring - 为什么 Spring Batch 计划作业只运行一次,尽管调度程序运行并且作业侦听器每次运行都说 FINISHED
问题描述
我已经创建了 Spring Batch(RESTReader、自定义处理器和自定义 ItemWriter) 我安排了它。Spring Batch 工作正常。调度似乎起作用,因为侦听器在每个预定间隔打印作业完成,但似乎没有读取或写入。
我的 Sprint 启动应用程序
@EnableScheduling
@SpringBootApplication
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class,args);
}
}
我的作业调度器
@Component
public class BatchScheduler {
Logger log = Logger.getLogger(BatchScheduler.class);
@Autowired
private JobLauncher jobLauncher;
@Autowired
private JobCompletionNotificationListener listener;
@Autowired
private Step step1;
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
private CreateRegistrationsBatchConfiguration job;
@Scheduled(fixedRate = 85000)
public void runJob() {
try{
JobExecution execution = jobLauncher.run(
job.importRegistrationJob(jobBuilderFactory, listener,
step1),
new JobParametersBuilder().addLong("uniqueness",
System.nanoTime()).toJobParameters()
);
log.info("Job finished with status :" + execution.getStatus());
}catch(Exception exception) {
log.error(exception.getMessage());
}
}
}
我的 Spring 批处理配置
@Configuration
@EnableBatchProcessing
public class CreateRegistrationsBatchConfiguration {
Logger log =
Logger.getLogger(CreateRegistrationsBatchConfiguration.class);
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private Environment environment;
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment
environment,
RestTemplate restTemplate) {
return new RESTEmployeeReader(
environment.getRequiredProperty("rest.api.url"),
restTemplate
);
}
@Bean
public RegistrationItemProcessor processor() {
return new RegistrationItemProcessor();
}
@Bean
public ItemWriter<List<Registration>> writer() {
return new MultiOutputItemWriter();
}
@Bean
public Job importRegistrationJob(JobBuilderFactory jobs, JobCompletionNotificationListener listener, Step step1) {
return jobBuilderFactory.get("importRegistrationJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
.end()
.build();
}
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory,
ItemReader<EmployeeEmploymentDTO> reader,
ItemWriter<List<Registration>> writer,
ItemProcessor<EmployeeEmploymentDTO, List<Registration>>
processor) {
return stepBuilderFactory.get("step1").allowStartIfComplete(true)
.<EmployeeEmploymentDTO, List<Registration>> chunk(10)
.reader(restEmployeeReader(environment,restTemplate()))
.processor(processor())
.writer(writer)
.build();
}
}
批处理表数据 batch_job_execution
+==+=====+===+=====+=====================+=====================+=====================+===========+===========+==+=====================+==+
| | 338 | 2 | 337 | 2018-05-18 14:36:36 | 2018-05-18 14:36:36 | 2018-05-18 14:37:47 | COMPLETED | COMPLETED | | 2018-05-18 14:37:47 | |
+==+=====+===+=====+=====================+=====================+=====================+===========+===========+==+=====================+==+
| | 339 | 2 | 338 | 2018-05-18 14:38:01 | 2018-05-18 14:38:01 | 2018-05-18 14:38:01 | COMPLETED | COMPLETED | | 2018-05-18 14:38:01 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 340 | 2 | 339 | 2018-05-18 14:39:26 | 2018-05-18 14:39:26 | 2018-05-18 14:39:26 | COMPLETED | COMPLETED | | 2018-05-18 14:39:26 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 341 | 2 | 340 | 2018-05-18 14:40:51 | 2018-05-18 14:40:51 | 2018-05-18 14:40:51 | COMPLETED | COMPLETED | | 2018-05-18 14:40:51 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 342 | 2 | 341 | 2018-05-18 14:42:16 | 2018-05-18 14:42:16 | 2018-05-18 14:42:16 | COMPLETED | COMPLETED | | 2018-05-18 14:42:16 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 343 | 2 | 342 | 2018-05-18 14:43:41 | 2018-05-18 14:43:41 | 2018-05-18 14:43:41 | COMPLETED | COMPLETED | | 2018-05-18 14:43:41 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 344 | 2 | 343 | 2018-05-18 14:45:06 | 2018-05-18 14:45:06 | 2018-05-18 14:45:06 | COMPLETED | COMPLETED | | 2018-05-18 14:45:06 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 345 | 2 | 344 | 2018-05-18 14:46:31 | 2018-05-18 14:46:31 | 2018-05-18 14:46:31 | COMPLETED | COMPLETED | | 2018-05-18 14:46:31 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 346 | 2 | 345 | 2018-05-18 14:47:56 | 2018-05-18 14:47:56 | 2018-05-18 14:47:56 | COMPLETED | COMPLETED | | 2018-05-18 14:47:56 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 347 | 2 | 346 | 2018-05-18 14:49:21 | 2018-05-18 14:49:21 | 2018-05-18 14:49:21 | COMPLETED | COMPLETED | | 2018-05-18 14:49:21 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
| | 348 | 2 | 347 | 2018-05-18 14:50:46 | 2018-05-18 14:50:46 | 2018-05-18 14:50:46 | COMPLETED | COMPLETED | | 2018-05-18 14:50:46 | |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+
批处理步骤执行
+==========================================================================================================================================================================================================================================================+
| STEP_EXECUTION_ID, VERSION, STEP_NAME, JOB_EXECUTION_ID, START_TIME, END_TIME, STATUS, COMMIT_COUNT, READ_COUNT, FILTER_COUNT, WRITE_COUNT, READ_SKIP_COUNT, WRITE_SKIP_COUNT, PROCESS_SKIP_COUNT, ROLLBACK_COUNT, EXIT_CODE, EXIT_MESSAGE, LAST_UPDATED |
+==========================================================================================================================================================================================================================================================+
| '338', '16', 'step1', '338', '2018-05-18 14:36:36', '2018-05-18 14:37:47', 'COMPLETED', '14', '132', '33', '99', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:37:47' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '339', '3', 'step1', '339', '2018-05-18 14:38:01', '2018-05-18 14:38:01', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:38:01' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '340', '3', 'step1', '340', '2018-05-18 14:39:26', '2018-05-18 14:39:26', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:39:26' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '341', '3', 'step1', '341', '2018-05-18 14:40:51', '2018-05-18 14:40:51', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:40:51' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '342', '3', 'step1', '342', '2018-05-18 14:42:16', '2018-05-18 14:42:16', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:42:16' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '343', '3', 'step1', '343', '2018-05-18 14:43:41', '2018-05-18 14:43:41', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:43:41' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '344', '3', 'step1', '344', '2018-05-18 14:45:06', '2018-05-18 14:45:06', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:45:06' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '345', '3', 'step1', '345', '2018-05-18 14:46:31', '2018-05-18 14:46:31', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:46:31' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '346', '3', 'step1', '346', '2018-05-18 14:47:56', '2018-05-18 14:47:56', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:47:56' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '347', '3', 'step1', '347', '2018-05-18 14:49:21', '2018-05-18 14:49:21', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:49:21' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '348', '3', 'step1', '348', '2018-05-18 14:50:46', '2018-05-18 14:50:46', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:50:46' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| '349', '3', 'step1', '349', '2018-05-18 14:52:11', '2018-05-18 14:52:11', 'COMPLETED', '1', '0', '0', '0', '0', '0', '0', '0', 'COMPLETED', '', '2018-05-18 14:52:11' |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
部分控制台日志
BatchApplication - 在 27.638 秒内启动 BatchApplication(JVM 运行时间为 49.853)13:30:04.905 [pool-11-thread-1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [FlowJob: [name= importRegistrationJob]] 使用以下参数启动:[{uniqueness=104422824800127}] 13:30:04.946
[pool-11-thread-1] INFO org.springframework.batch.core.job.SimpleStepHandler - 执行步骤:[step1] employeeData --> 13:31:14.002 [pool-11-thread-1] 信息
JobCompletionNotificationListener - !!! 工作完成!验证结果的时间 13:31:14.015[pool-11-thread-1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - Job: [FlowJob: [name=importRegistrationJob]] 使用以下参数完成: [{uniqueness=104507836733954}] 和以下状态:[COMPLETED] 13:31:29.766 [pool-11-thread-1] 信息
- 作业完成状态:COMPLETED 13:32:54.713[pool-11-thread-1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - 作业:[FlowJob:[name=importRegistrationJob]] 使用以下参数启动:[{uniqueness=104592836710544}] 13: 32:54.721
[pool-11-thread-1] INFO org.springframework.batch.core.job.SimpleStepHandler - 执行步骤:[step1] 13:32:54.750 [pool-11-thread-1] INFO batch.JobCompletionNotificationListener - !!!工作完成!验证结果的时间 13:32:54.750[pool-11-thread-1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - 作业:[FlowJob:[name=importRegistrationJob]] 使用以下参数启动:[{uniqueness=104677826302144}] 13: 34:19.722
[pool-11-thread-1] 信息 org.springframework.batch.core.job.SimpleStepHandler - 执行步骤:[step1]
解决方案
在我为阅读器、处理器和编写器添加@StepScope 后它起作用了
@Bean
@StepScope
ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment environment,
RestTemplate restTemplate) {
return new RESTEmployeeReader(
environment.getRequiredProperty("rest.api.url"),
restTemplate
);
}
@Bean
@StepScope
public RegistrationItemProcessor processor() {
return new RegistrationItemProcessor();
}
@Bean
@StepScope
public ItemWriter<List<Registration>> writer() {
return new MultiOutputItemWriter();
}
推荐阅读
- c# - 对soap服务中的外部api的异步请求
- c++ - WiFiClientSecure setCaCert 不接受我的参数
- python - 如何使用 REST API 更新 Jira 中的标签类型?
- reactjs - 日历标记 react-native-calendars
- maven - 当 Cucumber 场景失败时 mvn test 不会失败
- python - 从多个熊猫数据框创建一个矩阵
- python - 仅将选定信息从二维数组复制到另一个二维数组
- flutter - firestore 数组:“String”类型不是“int”类型的子类型
- android - Android:暗模式下的颜色反转
- ios - SwiftUI 中文本的顶部对齐/位置