[JAVA] Spring Batch job launch parameters

How to use spring-batch job start parameter in spring-boot environment.

Source code etc.

build.gradle

build.gradle


plugins {
  id 'org.springframework.boot' version '2.2.6.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'java'
}

configurations {
  developmentOnly
  runtimeClasspath {
    extendsFrom developmentOnly
  }
  compileOnly {
    extendsFrom annotationProcessor
  }
}


repositories {
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-batch'

  compileOnly 'org.projectlombok:lombok'
  annotationProcessor 'org.projectlombok:lombok'
  testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.springframework.batch:spring-batch-test'
  implementation 'com.h2database:h2'
}

test {
  useJUnitPlatform()
}

How to specify job start parameters

When used with spring-boot, if specified as the following command line argument, it will be treated as a job start parameter of spring-batch.

java -jar springbatchsample.jar hoge.param001=hoge

How to receive

How to receive job start parameters in java. There are several variations of this. The reader implementation is appropriate, so skip it.

SpEL - jobParameters['hoge.param001'] How to use SpEL and @ Value. You can receive `hoge.param001 = hoge``` on the above command line by doing ``` @ Value ("# {jobParameters ['hoge.param001']} ")` as below.

import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@StepScope
public class MyReader implements ItemReader<Integer> {

  @Value("#{jobParameters['hoge.param001']}")
  String param001;

  @Override
  public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    System.out.println(param001);
    
    return null;
  }

}

As a precaution of this method, it is necessary to add @ StepScope. See https://terasoluna-batch.github.io/guideline/5.0.0.RELEASE/en/Ch04_JobParameter.html#Ch04_JobParameter_HowToUse_CLIArgs for the reason, "The scope of the bean that references JobParameters must be Step scope". ..

beforeStep How to use the listener @ BeforeStep and get the job launch parameter from its method argument stepExecution```.

@Component
public class MyReader2 implements ItemReader<Integer>  {
  
  @BeforeStep
  void beforeStep(StepExecution stepExecution) {
    String param001 = stepExecution.getJobParameters().getString("hoge.param001");
    System.out.println("##" + param001);
  }

  @Override
  public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    return null;
  }

}

As a variation of the above, there is also a way to implement the traditional `` StepExecutionListener``` instead of annotations.

public class MyReader3 implements ItemReader<Integer>, StepExecutionListener  {

  @Override
  public void beforeStep(StepExecution stepExecution) {
    String param001 = stepExecution.getJobParameters().getString("hoge.param001");
    System.out.println("###" + param001);
  }
  
  @Override
  public Integer read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
    return null;
  }

  @Override
  public ExitStatus afterStep(StepExecution stepExecution) {
    return stepExecution.getExitStatus();
  }

}

Recommended Posts

Spring Batch job launch parameters
[Tutorial] Spring Batch
I tried Spring Batch
How to use In-Memory Job repository in Spring Batch
Loop step in Spring Batch
Things to consider when running a specified job using Spring Batch