为什么Spring Batch预定作业只运行一次,尽管调度程序运行并且作业监听器说每次运行都完成

我已经安排了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 |  |
+--+-----+---+-----+---------------------+---------------------+---------------------+-----------+-----------+--+---------------------+--+

batch_step_execution

+==========================================================================================================================================================================================================================================================+
| 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 - 作业:[FlowJob:[name使用以下参数启动= {importRegistrationJob]]:[] 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] INFO JobCompletionNotificationListener - !!!工作完成了!验证结果的时间13:31:14.015 [pool-11-thread-1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - 作业:[FlowJob:[name = importRegistrationJob]]使用以下参数完成:[]和以下状态:[已完成] 13:31:29.766 [pool-11-thread-1] INFO - 作业已完成状态:已完成13:32:54.713 [pool-11-thread- 1] INFO org.springframework.batch.core.launch.support.SimpleJobLauncher - 作业:[FlowJob:[name = importRegistrationJob]]使用以下参数启动:[] 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]]使用以下参数启动:[] 13:34:19.722 [pool-11-thread-1] INFO org.springframework.batch.core.job.SimpleStepHandler - 执行步骤:[step1]

回答(1)

2 years ago

在我为读者,处理器和编写器添加@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();
    }