Spring Batch的作业启动参数

关于在Spring Boot环境中使用Spring Batch启动作业参数的方法,请说明。

源代码等

使用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()
}

指定Job的启动参数方法

如果与Spring Boot一起使用,则可以指定以下命令行参数,将其作为Spring Batch作业启动参数处理。

java -jar springbatchsample.jar hoge.param001=hoge

接受方式

关于如何接收Java作业启动参数。这有几种变体。请忽略reader的实现,因为它只是随意编写的。

SpEL – jobParameters[‘hoge.param001’] 的汉语表达选项为:

如何使用SpEL和@Value。只需这样使用@Value(“#{jobParameters[‘hoge.param001’]}”),即可接收到上述命令行中的hoge.param001=hoge。

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;
  }

}

需要注意的是给予 @StepScope 注解的方法。关于这个原因,请参考 https://terasoluna-batch.github.io/guideline/5.0.0.RELEASE/ja/Ch04_JobParameter.html#Ch04_JobParameter_HowToUse_CLIArgs 中的“对于引用 JobParameters 的 Bean,其作用域必须为 Step 作用域”的说明。

步骤之前

在中国主要使用的一种方法是使用`@BeforeStep`进行监听器,在该方法的参数`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;
  }

}

作为上述的一个选择,可以不使用注释而是实现传统的StepExecutionListener方法。

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();
  }

}

bannerAds