java - 使用 Spring Boot 的 Spring 批处理 : Read arguments from config or command line and use them in job

标签 java spring-boot spring-batch

我对 Spring 技术还很陌生。我正在尝试使用 Spring Batch 和 Spring Boot 构建一个类似 ETL 的应用程序。

能够运行基本作业(读取->处理->写入)。现在,我想从配置文件(稍后)或命令行(现在可以使用它)读取参数(如日期、文件名、类型等)并在我的工作中使用它们。

入口点:

// Imports
@SpringBootApplication
@EnableBatchProcessing
public class EtlSpringBatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(EtlSpringBatchApplication.class, args);
    }

}

我的批量配置

// BatchConfig.java
// Imports
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public MyDao myDao;

    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("job")
                .incrementer(new RunIdIncrementer())
                .listener(new Listener(myDao))
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<myModel, myModel>chunk(1000)
                .reader(Reader.reader("my_file_20200520.txt"))
                .processor(new Processor())
                .writer(new Writer(myDao))
                .build();
    }

我有基本步骤。

Reader.java 有读取平面文件的方法。

public static FlatFileItemReader<MyModel> reader(String path) {......}

Processor.java 定义了 process 方法。我添加了一个 @BeforeStep 从数据库中获取处理所需的一些详细信息。

public class Processor implements ItemProcessor<MyModel, MyModel> {

    private static final Logger log = LoggerFactory.getLogger(Processor.class);
    private Long id = null;

    @BeforeStep
    public void getId(StepExecution stepExecution) {
        this.id = stepExecution.getJobExecution().getExecutionContext().getLong("Id");
    }

    @Override
    public MyModel process(MyModel myModel) throws Exception {
    }
}

Writer.java正在实现ItemWriter并编写代码。

Listener.java 扩展了 JobExecutionListenerSupport 并重写了 afterJob 和 beforeJob 方法。 基本上尝试在 beforeJob 中使用这里的executioncontext。

@Override
public void beforeJob(JobExecution jobExecution) {
    log.info("Getting the id..");
    this.id = myDao.getLatestId();
    log.info("id retrieved is: " + this.id);
    jobExecution.getExecutionContext().putLong("Id", this.id);
}

现在,我正在寻找的是:

  • 读者应该从作业参数中获取文件名。即,当运行作业时,我应该能够给出一些参数,其中之一是文件路径。
  • 后来的一些方法(如 get id 等)需要更多变量,这些变量可以作为参数传递给作业,即 run_date、type 等。

简而言之,我正在寻找一种方法,

  • 将作业参数传递给我的应用(运行日期、类型、文件路径等)
  • 在阅读器和其他地方(监听器、写入器)使用它们

有人可以向我提供我应该在 BatchConfig.java 和其他地方执行哪些附加操作,以读取作业参数(从命令行或配置文件,以简单者为准)?

最佳答案

Spring Batch 和 Spring Boot 引用文档都展示了如何将参数传递给作业:

此外,Spring Batch 文档通过代码示例详细解释了如何在批处理组件(如读取器、写入器等)中使用这些参数:

关于java - 使用 Spring Boot 的 Spring 批处理 : Read arguments from config or command line and use them in job,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61943137/

相关文章:

spring - 在 spring 的每个请求上刷新 cookie

数据列表中每个项目的 Spring Batch-Repeat 步骤

java - Java中基于外部xml的运行时类实例化

java - 创建 SAAJ 对象模型的问题

java - 使用 Spring Boot 设置无状态身份验证

java - Jhipster Spring Boot 日志设置

java - 对 SpringBatch FieldSetMapper 进行单元测试

tomcat - 如何在 spring 批处理作业中添加监听器以捕获 tomcat 关闭事件

java - 从 Android 中的不同 Activity 更改 ImageResource

java - 我如何知道瓦丁的 window 是否打开?