首页 > 解决方案 > Spring Batch 在 Spring Boot 中完成 Job 后发送响应

问题描述

我必须上传 CSV,将其转换为 Java 对象,然后保存在数据库中。我正在使用 Spring Boot 和 Spring Batch 来实现这一点。我已经阅读了多个教程。在分析了这些之后,Spring Batch Job 似乎在作业完成之前作为响应发送给客户端异步运行。但是我需要在作业执行完成后向客户端发送响应。有可能吗?请帮助解决此问题。谢谢我的控制器代码如下:

@RestController
public class AppRestCtrl {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    JobLauncher jobLauncher;

    @Autowired
    Job job;

    @PostMapping("/processFile")
    public ResponseEntity convertCsvToObject(@RequestParam("fileData") MultipartFile file) throws Exception {

        final Path rootLocation = Paths.get("uploads");

        if(!Files.exists(rootLocation)) {
            Files.createDirectories(rootLocation);
        }

        if(file.isEmpty()) {
            return ResponseEntity.badRequest().body("Empty File Not Allowed");
        }

        if(!file.getOriginalFilename().contains(".csv")) {
            return ResponseEntity.badRequest().body("File is Invalid!");
        }

        Files.deleteIfExists(rootLocation.resolve(file.getOriginalFilename()));
        Files.copy(file.getInputStream(), rootLocation.resolve(file.getOriginalFilename()));

        try {
            JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis())
                    .toJobParameters();
            jobLauncher.run(job, jobParameters);
        } catch (Exception e) {
            logger.info(e.getMessage());

        return ResponseEntity.ok("Batch Process Started Successfully!");
    }

}

批处理配置文件:

@Configuration
public class BatchConfig {

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

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

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").<ObjectNode, JsonNode>chunk(1)
                .reader(Reader.reader("uploads\\students.csv"))
                .processor(new Processor()).writer(new Writer()).build();
    }

}

标签: javaspring-bootasynchronousspring-batchbatch-processing

解决方案


似乎 Spring Batch Job 在作业完成之前作为响应发送给客户端异步运行

如果作业启动器配置了异步任务执行器,则这是真的。如果作业启动器使用同步任务执行器(这是默认设置),作业将一直执行直到完成。但在这种情况下,Web 客户端将一直等到工作完成,这可能是您不希望发生的事情(更多详细信息:https ://docs.spring.io/spring-batch/4.0.x/参考/html/job.html#runningJobsFromWebContainer)。

作业执行完成后,我需要向客户端发送响应

如果您的作业执行时间足够快,可以作为 Web 请求的执行时间,那么您可以使用(默认)同步任务执行器。在这种情况下,您可以在作业完成后发送响应。但如前所述,对于长时间运行的作业不建议这样做,因为在作业完成之前 http 请求可能会超时。

尝试使用org.springframework.web.context.request.async.DeferredResult(或类似的方法)将是一个丑陋的黑客,因为它不能解决问题。因此,对于您的用例,我看不到可行的选择。


推荐阅读