0

I have tried to find the answer for seemingly simple but the evasive question. How does spring identify jobs in the batch configuration. Everything is annotated with @Bean and nothing to identify. Does the spring identify those with keywords in the name itself? Like xyzStep xzyJob etc? I'm trying to follow the official documentation at here

Thanks in advance.

Yakup Türkan
  • 576
  • 2
  • 6
  • 21

2 Answers2

2

Everything is annotated with @Bean and nothing to identify. Does the spring identify those with keywords in the name itself?

Yes, as documented in the Javadoc of the @Bean annotation:

the default strategy for determining the name of a bean is to use the name of the @Bean method

That said, it should be noted that there is a difference between the Spring bean name and the Spring Batch job/step name (those can be different):

@Bean
public Job job(JobBuilderFactory jobBuilderFactory) {
    return jobBuilderFactory.get("myJob")
            //...
            .build();
}

In this example, the Spring bean name is job and the Spring Batch job name is myJob.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
1

Spring identifies the job on the event when we try starting the job. Below is a small snippet for reference,

This is a bean definition which returns of type Job and contains all the orchestration of steps in it,

  @Bean
  public Job testJob() throws Exception {

    Job job = jobBuilderFactory.get("testJob").incrementer(new RunIdIncrementer())
        .start(step1()).next(step2()).next(step3()).end()
        .listener(jobCompletionListener()).build();

    ReferenceJobFactory referenceJobFactory = new ReferenceJobFactory(job);
    registry.register(referenceJobFactory);

    return job;
}

Below would make use of the bean and start the job which means that the workflow defined as a part of job would get executed,

@Autowired
JobLauncher jobLauncher;

@Autowired
Job testJob;

// eliminating the method defnition for simplicity,

  try {
        jobLauncher.run(testJob, jobParameters);
  } catch (Exception e) {
        logger.error("Exception while running a batch job {}", e.getMessage());
  } 
Vignesh T I
  • 782
  • 1
  • 7
  • 22