代码之家  ›  专栏  ›  技术社区  ›  Josh

在作业完成之前返回Spring批处理作业ID

  •  1
  • Josh  · 技术社区  · 6 年前
    • 我正在尝试在作业完成之前返回一个Spring批处理作业ID。

    • 我当前的实现仅在作业 完成。

    • 我使用一个批处理程序控制器和一个批处理服务,发布在下面。 谢谢,我是新来的春季批,经过彻底的搜索 找不到与我的问题有多大关系的。有一个帖子 有人在用阿帕奇骆驼,但我不是。

    • 我知道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;
        }
    

    编辑

    我在马哈茂德·本·哈辛的评论的帮助下解决了这个问题:)

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   Mahmoud Ben Hassine    6 年前

    你能做的就是注射 JobLauncher 在您的 BatchService 由异步任务执行器(例如 SimpleAsyncTaskExecutor ThreadPoolTaskExecutor )这将异步运行您的作业,并将立即返回一个作业执行(您可以从中获取ID),而无需等待作业完成。

    因此,通过注入正确的 作业启动程序 在你的 批处理服务 ,您将能够在不更改 processProgName 方法。

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