【问题标题】:Return Spring Batch Job ID before the job completes在作业完成之前返回 Spring Batch 作业 ID
【发布时间】:2018-07-11 17:31:01
【问题描述】:
  • 我正在尝试在作业完成之前返回 Spring Batch 作业 ID。

  • 我当前的实现只在作业完成后返回信息 完成。

  • 我使用批处理程序控制器和批处理服务,发布在下面。 谢谢,我是 Spring Batch 的新手,经过详尽的搜索 找不到与我的问题相关的太多内容。有一个帖子 有人使用 Apache Camel,但我不是。

  • 我知道 SimpleAsyncTaskExecutor(),但不知道如何在这里应用它

控制器

@RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
    public ResponseEntity<JobResults> process(@PathVariable("progName") String progName,
                                              @RequestHeader("Accept") MediaType accept) throws Exception {
        HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
        responseHeaders.setContentType(MediaType.APPLICATION_XML);
        if(!accept.toString().equals("*/*")) {
            responseHeaders.setContentType(accept);
        }
        LOGGER.info("Running batch program " + progName);
        JobResults response = batchService.processProgName(progName);
        return new ResponseEntity<JobResults>(response, responseHeaders, HttpStatus.OK);
    }

服务

@Override
    public JobResults processProgName(String progName) throws Exception {

        String jobName = "call" + progName.toUpperCase() + "Job";
        JobExecution jobExecution = null;
        String result = "";
        Long jobId = null;
        JobResults results = new JobResults();
        BatchStatus jobStatus = null;
        try {
            // Launch the appropriate batch job.
            Map<String, Job> jobs = applicationContext.getBeansOfType(Job.class);
            LOGGER.info("BATCHSTART:Starting sync batch job:" + jobName);
            JobParametersBuilder builder = new JobParametersBuilder();
            // Pass in the runtime to ensure a fresh execution.
            builder.addDate("Runtime", new Date());
            jobExecution = jobLauncher.run(jobs.get(jobName), builder.toJobParameters());
            jobId = jobExecution.getId();
            jobStatus = jobExecution.getStatus();
            results.setName(jobName);
            results.setId(jobId);
            results.setMessage("The job has finished.");
            results.setStatus(jobStatus);
            LOGGER.info("The job ID is " + jobId);
            LOGGER.info("BATCHEND:Completed sync batch job:" + jobName);
            LOGGER.info("Completion status for batch job " + jobName + " is " + jobExecution.getStatus().name());
            List<Throwable> failures = jobExecution.getAllFailureExceptions();
            if (failures.isEmpty()) {
                result = jobExecution.getExecutionContext().getString(AbstractSetupTasklet.BATCH_PROGRAM_RESULT);
            } else {
                for (Throwable fail : failures) {
                    result += fail.getMessage() + "\n";
                }
            }
            LOGGER.info("The job results are: " + result);
        } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
                | JobParametersInvalidException e) {
            throw new RuntimeException("An error occurred while attempting to execute a job. " + e.getMessage(), e);
        }
        return results;
    }

再次感谢。

编辑

我已将此添加到我的批处理配置中

@Bean(name = "AsyncJobLauncher")
    public JobLauncher simpleJobLauncher(JobRepository jobRepository){
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
        return jobLauncher;
    }

编辑

我在 Mahmoud Ben Hassine 的评论的帮助下解决了这个问题 :)

我不得不从服务中删除结果变量和信息,并添加上面看到的代码。当我现在到达终点时,我会立即收到工作信息。

【问题讨论】:

    标签: spring-boot asynchronous spring-batch


    【解决方案1】:

    您可以做的是在您的BatchService 中注入一个JobLauncher 实现,该实现由异步任务执行器(例如SimpleAsyncTaskExecutorThreadPoolTaskExecutor)支持。这将异步运行您的作业,并立即返回作业执行(您可以从中获取 id),而无需等待作业完成。

    因此,通过在您的BatchService 中注入正确的JobLauncher 实现,您将能够在不更改processProgName 方法的代码的情况下实现您的要求。

    您可以在此处找到有关如何同步或异步运行作业的更多详细信息:https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#configuringJobLauncher

    【讨论】:

    • 你确定我不需要修改processProgName吗?它返回一个名为 result 的变量,即BATCH_PROGRAM_RESULTS。我注入了我已经修改为异步但在运行作业时出错的 JobLauncher
    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2016-12-07
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多