How to run Spring Batch Jobs in certain order (Spring Boot)?

前端 未结 5 659
我寻月下人不归
我寻月下人不归 2020-12-21 09:48

I\'m developing with Spring Batch using Spring Boot.

I\'m with the minimal configuration provided by Spring Boot and defined some Jobs (no XML configuration at all).

5条回答
  •  没有蜡笔的小新
    2020-12-21 10:06

    I don't have enough rep to comment. But have you tried just to manually launch your jobs in the order you want?

    You need to set spring.batch.job.enabled=false in your application.properties, so that your jobs are not run automatically.

    Then just use a launcher to launch your jobs in the order you want.

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { TestConfiguration.class, TestDataSourceConfiguration.class, TestBatchConfig.class })
    public class JobOrderTest {
    
        @Autowired
        JobLauncher jobLauncher;
    
        @Mock
        Job firstJob;
    
        @Mock
        Job secondJob;
    
        @Mock
        Job thirdJob;
    
        @Mock
        JobParametersValidator jobParametersValidator;
    
        @Test
        public void jobInOrderTest() throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {
    
            when(firstJob.getName()).thenReturn(UUID.randomUUID().toString());
            when(secondJob.getName()).thenReturn(UUID.randomUUID().toString());
            when(thirdJob.getName()).thenReturn(UUID.randomUUID().toString());
            when(firstJob.getJobParametersValidator()).thenReturn(jobParametersValidator);
            when(secondJob.getJobParametersValidator()).thenReturn(jobParametersValidator);
            when(thirdJob.getJobParametersValidator()).thenReturn(jobParametersValidator);
    
            jobLauncher.run(firstJob, new JobParameters());
            jobLauncher.run(secondJob, new JobParameters());
            jobLauncher.run(thirdJob, new JobParameters());
        }
    
    }
    

    Here is the output

    2016-12-30 09:48:36.457  INFO 144860 --- [cTaskExecutor-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [firstJob] launched with the following parameters: ...
    2016-12-30 09:48:36.457  INFO 144860 --- [cTaskExecutor-1] o.s.b.c.l.support.SimpleJobLauncher      : Job: [firstJob] completed with the following parameters: ...
    2016-12-30 09:48:36.478  INFO 144860 --- [cTaskExecutor-2] o.s.b.c.l.support.SimpleJobLauncher      : Job: [secondJob] launched with the following parameters: ...
    2016-12-30 09:48:36.478  INFO 144860 --- [cTaskExecutor-2] o.s.b.c.l.support.SimpleJobLauncher      : Job: [secondJob] completed with the following parameters: ...
    2016-12-30 09:48:36.508  INFO 144860 --- [cTaskExecutor-3] o.s.b.c.l.support.SimpleJobLauncher      : Job: [thirdJob] launched with the following parameters: ...
    2016-12-30 09:48:36.508  INFO 144860 --- [cTaskExecutor-3] o.s.b.c.l.support.SimpleJobLauncher      : Job: [thirdJob] completed with the following parameters: ...
    

提交回复
热议问题