{{announcement.body}}
{{announcement.title}}

Spring Batch — Decider

DZone 's Guide to

Spring Batch — Decider

In this article, see how to make use of JobExecutionDecider.

· Java Zone ·
Free Resource

In this tutorial, we'll learn how to make the use of JobExecutionDecider.  This Interface allowing for programmatic access to the decision on what the status of a flow should be. 

For example, if some condition that's stored in the database indicates that the job should stop for a manual check, a decider implementation could check that value to determine the status of the flow. 

Reference links: https://docs.spring.io/spring-batch/docs/current/reference/html/index-single.html 

Java
 




xxxxxxxxxx
1
10


1
@Bean
2
public Job job() {
3
    return jobBuilderFactory.get("job")
4
            .start(step1())
5
            .next(decider())
6
                .on("YES").to(step2())
7
            .from(decider())
8
                .on("NO").to(step3()).end()
9
            .build();
10
}



MyJobExecutionDecider.java

Interface allowing for programmatic access to the decision on what the status of a flow should be. For example, if some condition that's stored in the database indicates that the job should stop for a manual check, a decider implementation could check that value to determine the status of the flow.

Java
 




xxxxxxxxxx
1


1
public class MyJobExecutionDecider implements JobExecutionDecider{
2
 
          
3
    @Override
4
    public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
5
        // Here you can conditional flow and based on status return you can call the specific Task
6
        return new FlowExecutionStatus("YES");
7
    }
8
}



Step 1: Tasklet.java

Below are the three different Tasklets created to show which tasklets gets called based on which condition.

Java
 




xxxxxxxxxx
1


1
public class Step1Tasklet implements Tasklet{
2
    
3
    @Override
4
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
5
        System.out.println("Step1Tasklet Execured ...");
6
        return RepeatStatus.FINISHED;
7
    }
8
}
9
 
          



Step 2: Tasklet.java

Java
 




xxxxxxxxxx
1


1
public class Step2Tasklet implements Tasklet{
2
    
3
    @Override
4
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
5
        System.out.println("Step2Tasklet executed...");
6
        return RepeatStatus.FINISHED;
7
    }
8
}



Step 3: Tasklet.java

Java
 




xxxxxxxxxx
1


1
public class Step3Tasklet implements Tasklet{
2
    
3
    @Override
4
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
5
        System.out.println("Step3Tasklet Executed !!");
6
        return RepeatStatus.FINISHED;
7
    }
8
}



MyJob.java

This is main job configuration details.

Java
 




xxxxxxxxxx
1
56


1
@Configuration
2
@EnableBatchProcessing
3
public class MyJob {
4
 
          
5
    @Autowired
6
    private JobBuilderFactory jobBuilderFactory;
7
 
          
8
    @Autowired
9
    private StepBuilderFactory stepBuilderFactory;
10
 
          
11
    @Bean
12
    public Step1Tasklet step1Tasklet() {
13
        return new Step1Tasklet();
14
    }
15
 
          
16
    @Bean
17
    public Step2Tasklet step2Tasklet() {
18
        return new Step2Tasklet();
19
    }
20
    
21
    @Bean
22
    public Step3Tasklet step3Tasklet() {
23
        return new Step3Tasklet();
24
    }
25
 
          
26
    @Bean
27
    public Step step1() {
28
        return stepBuilderFactory.get("step1").tasklet(step1Tasklet()).build();
29
    }
30
 
          
31
    @Bean
32
    public MyJobExecutionDecider decider() {
33
        return new MyJobExecutionDecider();
34
    }
35
 
          
36
    @Bean
37
    public Step step2() {
38
        return stepBuilderFactory.get("step2").tasklet(step2Tasklet()).build();
39
    }
40
 
          
41
    @Bean
42
    public Step step3() {
43
        return stepBuilderFactory.get("step3").tasklet(step3Tasklet()).build();
44
    }
45
 
          
46
    @Bean
47
    public Job job() {
48
        return jobBuilderFactory.get("job")
49
                .start(step1())
50
                .next(decider())
51
                    .on("YES").to(step2())
52
                .from(decider())
53
                    .on("NO").to(step3()).end()
54
                .build();
55
    }
56
}



SpringBatchDeciderApplication.java

Java
 




xxxxxxxxxx
1
25


1
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
2
@EnableBatchProcessing
3
public class SpringBatchDeciderApplication implements CommandLineRunner{
4
 
          
5
    public static void main(String[] args) {
6
        SpringApplication.run(SpringBatchDeciderApplication.class, args);
7
    }
8
    
9
    @Autowired
10
    private JobLauncher jobLauncher;
11
    
12
    @Autowired
13
    private Job job;
14
 
          
15
    @Override
16
    public void run(String... args) throws Exception {
17
        JobParameters jobParameters = new JobParametersBuilder()
18
                .addString("UUID", UUID.randomUUID().toString())
19
                .toJobParameters();
20
        
21
        JobExecution execution = jobLauncher.run(job, jobParameters);
22
        System.out.println("STATUS :: "+execution.getStatus()); 
23
    }
24
}
25
 
          



Logs:

Java
 




xxxxxxxxxx
1
23


1
   _____            _                    ____        _       _           _____            _     _           
2
  / ____|          (_)                  |  _ \      | |     | |         |  __ \          (_)   | |          
3
 | (___  _ __  _ __ _ _ __   __ _ ______| |_) | __ _| |_ ___| |__ ______| |  | | ___  ___ _  __| | ___ _ __ 
4
  \___ \| '_ \| '__| | '_ \ / _` |______|  _ < / _` | __/ __| '_ \______| |  | |/ _ \/ __| |/ _` |/ _ \ '__|
5
  ____) | |_) | |  | | | | | (_| |      | |_) | (_| | || (__| | | |     | |__| |  __/ (__| | (_| |  __/ |   
6
 |_____/| .__/|_|  |_|_| |_|\__, |      |____/ \__,_|\__\___|_| |_|     |_____/ \___|\___|_|\__,_|\___|_|   
7
        | |                  __/ |                                                                          
8
        |_|                 |___/                                                                           
9
2020-04-15 18:19:51.429  INFO 8104 --- [           main] c.example.SpringBatchDeciderApplication  : Starting SpringBatchDeciderApplication on with PID 8104 (C:\Learnings\spring-batch-latest\Minella\Spring-Batch-by-Michael-Minella\spring-batch-decider\target\classes started by e081155 in C:\Learnings\spring-batch-latest\Minella\Spring-Batch-by-Michael-Minella\spring-batch-decider)
10
2020-04-15 18:19:51.433  INFO 8104 --- [           main] c.example.SpringBatchDeciderApplication  : No active profile set, falling back to default profiles: default
11
2020-04-15 18:19:52.926  INFO 8104 --- [           main] c.example.SpringBatchDeciderApplication  : Started SpringBatchDeciderApplication in 2.188 seconds (JVM running for 4.938)
12
2020-04-15 18:19:52.933  WARN 8104 --- [           main] o.s.b.c.c.a.DefaultBatchConfigurer       : No datasource was provided...using a Map based JobRepository
13
2020-04-15 18:19:52.933  WARN 8104 --- [           main] o.s.b.c.c.a.DefaultBatchConfigurer       : No transaction manager was provided, using a ResourcelessTransactionManager
14
2020-04-15 18:19:52.957  INFO 8104 --- [           main] 
15
16
o.s.b.c.l.support.SimpleJobLauncher      : No TaskExecutor has been set, defaulting to synchronous executor.
17
2020-04-15 18:19:53.014  INFO 8104 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=job]] launched with the following parameters: [{UUID=723f017d-1242-4cb8-9761-c1e7e1e0829a}]
18
2020-04-15 18:19:53.119  INFO 8104 --- [           main] 
19
20
o.s.batch.core.job.SimpleStepHandler     : Executing step: [step1]
21
Step1Tasklet Execured ...
22
2020-04-15 18:19:53.156  INFO 8104 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step1] executed in 36ms
23
2020-04-15 18:19:53.184  INFO 8104 --- [           main] 
24
25
26
o.s.batch.core.job.SimpleStepHandler     : Executing step: [step2]
27
Step2Tasklet executed...
28
2020-04-15 18:19:53.193  INFO 8104 --- [           main] o.s.batch.core.step.AbstractStep         : Step: [step2] executed in 9ms
29
30
31
2020-04-15 18:19:53.200  INFO 8104 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=job]] completed with the following parameters: [{UUID=723f017d-1242-4cb8-9761-c1e7e1e0829a}] and the following status: [COMPLETED] in 103ms
32
STATUS :: COMPLETED



Thanks for reading.

Topics:
JobExecutionDecider, java, spring batch, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}