【问题标题】:Passing a dynamic file name to process from a post request从发布请求传递动态文件名以进行处理
【发布时间】:2019-09-23 16:31:34
【问题描述】:

上传后传一个动态文件名到spring batch进行处理

我是 Spring Batch 的新手,我想要完成的是从一个应用程序上传一个 csv 文件,然后使用上传文件的文件名向 Spring Batch 发送一个发布请求,并让 Spring Batch 从定位它并处理它。

我尝试将字符串值传递给阅读器,但我不知道如何在步骤中访问它

// controller where i want to pass the file name to the reader
@RestController

public class ProcessController {

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job importUserJob;

    @PostMapping("/load")
    public BatchStatus Load(@RequestParam("filePath") String filePath) throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {

        JobExecution jobExecution = jobLauncher.run(importUserJob, new JobParametersBuilder()
                .addString("fullPathFileName", filePath)
                .toJobParameters());

        return jobExecution.getStatus();

    }
}

//reader in my configuration class
 @Bean
    public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) 


// the problem is here at the .reader chain
    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1")
                .<Person, Person> chunk(10)
                .reader(reader(reader))
                .processor(processor())
                .writer(writer())
                .build();
    }

我希望将文件名传递给阅读器 amd spring batch 可以处理它

【问题讨论】:

  • 请分享csv文件和人物类

标签: java spring spring-batch


【解决方案1】:

您正确地将文件的完整路径作为作业参数传递。但是,您的阅读器需要在步骤范围内才能在运行时将作业参数绑定到 pathToFile 参数。这被称为late binding,因为它发生在运行时较晚,而不是配置时较早(当时我们还不知道参数值)。

所以在你的情况下,你的读者可能是这样的:

@Bean
@StepScope
public FlatFileItemReader<Person> reader(@Value("#{jobParameters[fullPathFileName]}") String pathToFile) {
   //  return reader;
}

然后你可以将null传递给步骤定义中的reader方法:

@Bean
public Step step1() {
    return stepBuilderFactory.get("step1")
            .<Person, Person> chunk(10)
            .reader(reader(null))
            .processor(processor())
            .writer(writer())
            .build();
}

【讨论】:

    猜你喜欢
    • 2015-09-17
    • 2018-07-31
    • 2016-04-09
    • 2015-06-29
    • 1970-01-01
    • 2020-06-07
    • 2019-06-07
    • 1970-01-01
    • 2021-02-03
    相关资源
    最近更新 更多