AWS Batch 上的 SpringBatch

jvx*_*x23 3 spring-batch aws-batch

我可以创建 Spring Batch 并使用 AWS Batch 调用吗?如果是这样,建议采取步骤

根据此分析,我必须致电使用 Spring Batch 或使用 Java 应用程序使用 AWS Batch。

htt*_*nts 6

是的,您可以,并且使用 Spring Batch(和 Spring Boot)是构建在 aws Batch 中运行的 Java 应用程序的不错选择。AWS Batch 启动一个 docker 容器并在该容器中运行命令来运行 spring boot/batch 应用程序。

如果您还使用 Spring Boot 和 Spring Batch,您应该实现 aorg.springframework.boot.CommandLineRunner来启动您的作业。CommandLineRunnerrun(String ...args)方法接收配置 AWS Batch 作业的命令行参数。将CommandLineRunner这些命令行参数转换为JobParameters然后启动作业。

AWS Batch 使用 java 应用程序的退出代码来确定作业是成功还是失败。这意味着您需要将从JobExecution运行批处理作业返回的结果转换为退出代码,以告知 AWS Batch 作业是成功还是失败。

CommandLineRunner下面是启动 spring 批处理作业并将 JobExecution 转换为退出代码的示例...

@Component
@Profile("!test")
public class BatchJobCommandLineRunner implements CommandLineRunner, ExitCodeGenerator {

    private static final Logger log = LoggerFactory.getLogger(BatchJobCommandLineRunner.class);

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job awsBatchJob;

    @Autowired
    private BatchJobProperties batchJobProperties;

    private JobExecution jobExecution;

    @Override
    public void run(String... args) throws Exception {
        JobParameters jobParameters = new DefaultJobParametersConverter().getJobParameters(batchJobProperties);
        this.jobExecution = jobLauncher.run(awsBatchJob, jobParameters);
        for (StepExecution se : jobExecution.getStepExecutions()) {
            log.info(se.getSummary());
        }
    }

    @Override
    public int getExitCode() {
        int exitCode = getExitCode(jobExecution);
        log.debug("returning exitCode {}", exitCode);
        return exitCode;
    }

    private int getExitCode(JobExecution jobExecution) {
        if (jobExecution != null) {
            switch (jobExecution.getStatus()) {
            case COMPLETED:
            case STARTED:
            case STARTING:
            case STOPPING:
            case ABANDONED:
                return 0;

            case FAILED:
                return 1;

            case STOPPED:
                return 2;

            case UNKNOWN:
                return 3;
            }
        }

        return 0;
    }

}
Run Code Online (Sandbox Code Playgroud)

在上面,我使用一个BatchJobProperties类从命令行读取属性。任何形式的命令行参数都-Djob.property1 -Djob.property2 ...将使用ConfigurationProperties注释填充 BatchJobProperties 对象...

@Configuration
@ConfigurationProperties(prefix = "job")
public class BatchJobProperties extends Properties {}
Run Code Online (Sandbox Code Playgroud)

由于这是一个控制台应用程序,而不是 Web 应用程序,因此您将 Spring Boot 配置为不在 application.yml 文件中创建 Web 应用程序。另请注意,spring.batch.job.enabled设置为 false 因为该作业是使用CommandLineRunner...启动的

spring:
  main:
    web-application-type: NONE
  batch:
    job:
      enabled: false
Run Code Online (Sandbox Code Playgroud)

构建 Spring Boot 应用程序时,构建 jar 后,您将构建一个 docker 容器。下面是一个示例 Dockerfile...

FROM eclipse-temurin:17-jdk-alpine
RUN addgroup -S spring && adduser -S spring -G spring
RUN mkdir /app
RUN chown -R spring:spring /app
RUN chmod 755 /app
WORKDIR /app
USER spring:spring
COPY target/*.jar /app.jar
CMD ["java","-jar","/app.jar"]
Run Code Online (Sandbox Code Playgroud)

在 AWS Batch 中,当您创建作业实例时,您可以指定运行 docker 实例时使用的 CMD,并且您可以在此处指定命令行参数。下面是一个示例 AWS 作业实例,显示了配置用于运行 docker 容器的 CMD 的位置。您可以在此处配置 Spring Batch 作业使用的 JobParameters。

在此输入图像描述