简体   繁体   English

Spring 批处理如何在 ExecutionContext 中设置文件路径以进行下一步

[英]Spring Batch how to set filepath in ExecutionContext for next step

I have a spring batch workflow where I read from a flat csv file and write it a csv file.我有一个 spring 批处理工作流,我从一个平面 csv 文件中读取并将其写入 csv 文件。

This is what my ItemWriter looks like:这就是我的 ItemWriter 的样子:

@Configuration
public class MyCSVFileWriter implements ItemWriter<RequestModel> {

    private final FlatFileItemWriter<RequestModel> writer;

    private ExecutionContext jobContext;

    public MyCSVFileWriter() throws Exception {

        this.writer = new FlatFileItemWriter<>();

        DelimitedLineAggregator<RequestModel> lineAggregator = new DelimitedLineAggregator<>();

        BeanWrapperFieldExtractor<RequestModel> extractor = new BeanWrapperFieldExtractor<>();
        extractor.setNames(new String[]{"id", "source", "date"});

        lineAggregator.setFieldExtractor(extractor);

        this.writer.setLineAggregator(lineAggregator);
        this.writer.setShouldDeleteIfExists(true);

        this.writer.afterPropertiesSet();
    }

    @Override
    public void write(List<? extends RequestModel> items) throws Exception {
        this.writer.open(jobContext);
        this.writer.write(items);
    }

    @BeforeStep
    public void beforeStepHandler(StepExecution stepExecution) {
        JobExecution jobExecution = stepExecution.getJobExecution();
        jobContext = jobExecution.getExecutionContext();

        this.writer.setResource(new FileSystemResource(getRequestOutputPathResource(jobContext)));
    }

    private String getRequestOutputPathResource(ExecutionContext jobContext) {
        //***
        return resourcePath;
    }

}

I use the executionContext to extract some data used to calculate my resourcePath for my writer.我使用 executionContext 来提取一些数据,用于为我的作者计算我的resourcePath

My next step after writing is uploading the file I have written to in previous step to a remote server.编写后的下一步是将我在上一步中写入的文件上传到远程服务器。 For this I need to store the file path which were calculated and store it ExecutionContext and make it available in the next step.为此,我需要存储计算的文件路径并将其存储为 ExecutionContext 并使其在下一步中可用。

What is the best way to do this?做这个的最好方式是什么? Should I be doing this in a @AfterStep handler?我应该在@AfterStep处理程序中执行此操作吗?

Should I be doing this in a @AfterStep handler?我应该在 @AfterStep 处理程序中执行此操作吗?

Yes, this is a good option.是的,这是一个不错的选择。 You can store the path of the file that has been written in the job execution context and read it from there in the next step that uploads the file.您可以存储已写入作业执行上下文的文件的路径,并在上传文件的下一步中从那里读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在春季批处理中编辑ExecutionContext - How to edit ExecutionContext spring batch Spring 批处理 - 如何从 ItemListenerSupport 访问 ExecutionContext - Spring Batch - how to access ExecutionContext from ItemListenerSupport Spring 批处理:在侦听器中获取 ExecutionContext - Spring batch: get ExecutionContext in the listener Spring Batch的ExecutionContext和线程安全 - ExecutionContext and Thread safety on Spring Batch 如何获得Spring Batch作业的下一步中上一步执行的ID? - How do I get the ID of the previous step execution in the next step of a spring batch job? 在 Spring Batch 中访问 HeaderCallBack 中的 ExecutionContext 值 - Accessing the ExecutionContext values in HeaderCallBack in Spring Batch Spring Batch 将资源的值从步骤 1 传递到下一步 - Spring batch pass value of resource from step 1 to the next step Spring Batch:如何在不持久化(即不使用ExecutionContext)的情况下将数据从StepExecution传递到JobExecution? - Spring Batch: How to pass data from StepExecution to JobExecution without persisting it (that is, without using ExecutionContext)? Spring Batch:将读入文件的名称传递给下一步 - Spring Batch: Pass over names of read-in files to the next step 春季批处理:填充n个文件,但下一步显示为空 - Spring Batch: Fill n files but the next step sees empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM