Created
July 26, 2020 08:03
-
-
Save ru-rocker/83b39b0a511f5022ed12ae3b2aba9d63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@EnableBatchProcessing | |
@Configuration | |
@ConditionalOnProperty(value = "features.job-processor.hello-world", havingValue = "active") | |
public class HelloWorldJobProcessor { | |
private final Logger logger = LoggerFactory.getLogger(this.getClass()); | |
private JobBuilderFactory jobBuilderFactory; | |
private StepBuilderFactory stepBuilderFactory; | |
@Autowired | |
public void setJobBuilderFactory(final JobBuilderFactory jobBuilderFactory) { | |
this.jobBuilderFactory = jobBuilderFactory; | |
} | |
@Autowired | |
public void setStepBuilderFactory(final StepBuilderFactory stepBuilderFactory) { | |
this.stepBuilderFactory = stepBuilderFactory; | |
} | |
@Bean("hello-world-step1") | |
public Step step1() { | |
// @formatter:off | |
return this.stepBuilderFactory | |
.get("hello-world-step1") | |
.tasklet((contribution, chunkContext) -> { | |
logger.info("Step 1: Hello World!"); | |
return RepeatStatus.FINISHED; | |
}) | |
.build(); | |
// @formatter:on | |
} | |
@Bean("hello-world-step2") | |
public Step step2() { | |
// @formatter:off | |
return this.stepBuilderFactory | |
.get("hello-world-step2") | |
.tasklet((contribution, chunkContext) -> { | |
logger.info("Step 2: Hello World"); | |
return RepeatStatus.FINISHED; | |
}) | |
.build(); | |
// @formatter:on | |
} | |
@Bean("hello-world-job") | |
public Job job() { | |
// @formatter:off | |
return this.jobBuilderFactory | |
.get("hello-world-job") | |
.incrementer(new RunIdIncrementer()) | |
.start(step1()) | |
.next(step2()) | |
.build(); | |
// @formatter:on | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Job Processor test: HelloWorldJobProcessor | |
@SpringBatchTest | |
@EnableAutoConfiguration | |
@ContextConfiguration(classes = { HelloWorldJobProcessor.class }) | |
@RunWith(SpringRunner.class) | |
@TestPropertySource(properties = { "features.job-processor.hello-world=active" }) | |
public class HelloWorldJobProcessorTest { | |
@Autowired | |
private JobLauncherTestUtils jobLauncherTestUtils; | |
@Autowired | |
private JobRepositoryTestUtils jobRepositoryTestUtils; | |
@Test | |
public void test() throws Exception { | |
final JobExecution jobExecution = jobLauncherTestUtils.launchJob(); | |
final JobInstance actualJobInstance = jobExecution.getJobInstance(); | |
final ExitStatus actualJobExitStatus = jobExecution.getExitStatus(); | |
// then | |
Assertions.assertEquals(actualJobInstance.getJobName(), "hello-world-job"); | |
Assertions.assertEquals(actualJobExitStatus.getExitCode(), "COMPLETED"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment