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

以xml形式返回spring批处理作业信息

  •  0
  • Josh  · 技术社区  · 7 年前

    当我提出请求时,我想以xml的形式获取一些信息。下面是一些代码的示例

    @RequestMapping(value = "/batch/xml/", method = RequestMethod.GET, produces="application/xml")
        @ResponseBody
        public String getXML() {
            return xml here;
        }
    }
    

    我想将作业名称、作业执行ID和作业状态返回为XML。

    我试着搜索,但找不到任何与我的主题相关的东西。任何帮助都将不胜感激。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Josh    7 年前

    我已经解决了我的问题。代码如下。控制器调用服务来解析信息,然后将输出格式化为xml。

    控制器

     @RequestMapping(value = "/batch/{progName}", method = RequestMethod.GET)
        public ResponseEntity<JobResults> process(@PathVariable("progName") String progName,
                                                            @RequestHeader("Content-Type") MediaType contentType,
                                                            @RequestHeader("Accept") List<MediaType> accept) throws Exception {
            HttpHeaders responseHeaders = MccControllerUtils.createCacheDisabledHeaders();
            responseHeaders.setContentType(MediaType.APPLICATION_XML);
            if(!contentType.toString().equals("*/*") && !accept.toString().equals("*/*")) {
                responseHeaders.setContentType(contentType);
                responseHeaders.setAccept(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;
        }