【问题标题】:Sent Response after Spring Batch completed Job in Spring BootSpring Batch 在 Spring Boot 中完成 Job 后发送响应
【发布时间】:2019-03-04 03:46:33
【问题描述】:

我必须上传 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();
    }

}

【问题讨论】:

    标签: java spring-boot asynchronous spring-batch batch-processing


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-17
      • 2015-11-03
      • 1970-01-01
      • 2014-12-22
      • 2017-11-04
      • 2020-04-16
      • 2018-03-10
      • 2018-07-10
      相关资源
      最近更新 更多