|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.batch.core.job.builder;
|
17 | 17 |
|
| 18 | +import java.util.Arrays; |
| 19 | + |
18 | 20 | import static org.junit.Assert.assertEquals;
|
19 | 21 |
|
20 | 22 | import org.junit.Before;
|
|
25 | 27 | import org.springframework.batch.core.JobExecution;
|
26 | 28 | import org.springframework.batch.core.JobInterruptedException;
|
27 | 29 | import org.springframework.batch.core.JobParameters;
|
| 30 | +import org.springframework.batch.core.JobParametersBuilder; |
| 31 | +import org.springframework.batch.core.Step; |
28 | 32 | import org.springframework.batch.core.StepExecution;
|
29 | 33 | import org.springframework.batch.core.UnexpectedJobExecutionException;
|
| 34 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 35 | +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; |
| 36 | +import org.springframework.batch.core.configuration.annotation.JobScope; |
| 37 | +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; |
30 | 38 | import org.springframework.batch.core.job.flow.Flow;
|
31 | 39 | import org.springframework.batch.core.job.flow.FlowExecutionStatus;
|
32 | 40 | import org.springframework.batch.core.job.flow.JobExecutionDecider;
|
| 41 | +import org.springframework.batch.core.launch.JobLauncher; |
33 | 42 | import org.springframework.batch.core.repository.JobRepository;
|
34 | 43 | import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
|
35 | 44 | import org.springframework.batch.core.step.StepSupport;
|
| 45 | +import org.springframework.batch.item.support.ListItemReader; |
| 46 | +import org.springframework.beans.factory.annotation.Value; |
| 47 | +import org.springframework.context.ApplicationContext; |
| 48 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 49 | +import org.springframework.context.annotation.Bean; |
| 50 | +import org.springframework.context.annotation.Configuration; |
36 | 51 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
37 | 52 | import org.springframework.lang.Nullable;
|
38 | 53 |
|
39 | 54 | /**
|
40 | 55 | * @author Dave Syer
|
| 56 | + * @author Mahmoud Ben Hassine |
41 | 57 | *
|
42 | 58 | */
|
43 | 59 | public class FlowJobBuilderTests {
|
@@ -238,4 +254,45 @@ public void testBuildWithStopAndRestart() throws Exception {
|
238 | 254 | assertEquals("step2", execution.getStepExecutions().iterator().next().getStepName());
|
239 | 255 | }
|
240 | 256 |
|
| 257 | + @Test |
| 258 | + public void testBuildWithJobScopedStep() throws Exception { |
| 259 | + // given |
| 260 | + ApplicationContext context = new AnnotationConfigApplicationContext(JobConfiguration.class); |
| 261 | + JobLauncher jobLauncher = context.getBean(JobLauncher.class); |
| 262 | + Job job = context.getBean(Job.class); |
| 263 | + JobParameters jobParameters = new JobParametersBuilder() |
| 264 | + .addLong("chunkSize", 2L) |
| 265 | + .toJobParameters(); |
| 266 | + |
| 267 | + // when |
| 268 | + JobExecution jobExecution = jobLauncher.run(job, jobParameters); |
| 269 | + |
| 270 | + // then |
| 271 | + assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 272 | + } |
| 273 | + |
| 274 | + @EnableBatchProcessing |
| 275 | + @Configuration |
| 276 | + static class JobConfiguration { |
| 277 | + |
| 278 | + @Bean |
| 279 | + @JobScope |
| 280 | + public Step step(StepBuilderFactory stepBuilderFactory, |
| 281 | + @Value("#{jobParameters['chunkSize']}") Integer chunkSize) { |
| 282 | + return stepBuilderFactory.get("step") |
| 283 | + .<Integer, Integer>chunk(chunkSize) |
| 284 | + .reader(new ListItemReader<>(Arrays.asList(1, 2, 3, 4))) |
| 285 | + .writer(items -> {}) |
| 286 | + .build(); |
| 287 | + } |
| 288 | + |
| 289 | + @Bean |
| 290 | + public Job job(JobBuilderFactory jobBuilderFactory) { |
| 291 | + return jobBuilderFactory.get("job") |
| 292 | + .flow(step(null, null)) |
| 293 | + .build() |
| 294 | + .build(); |
| 295 | + } |
| 296 | + } |
| 297 | + |
241 | 298 | }
|
0 commit comments