【问题标题】:How to trigger a job using a rest web service?如何使用 REST Web 服务触发作业?
【发布时间】:2013-12-13 11:24:09
【问题描述】:

我想使用 CXF 或 jersey 创建一个 REST Web 服务来调用 Spring 批处理作业。可能吗。如果是这样,我该怎么做?

【问题讨论】:

  • 这里不是要代码的地方!尝试自己,如果您在发布问题时遇到问题,我们将很乐意为您提供帮助!

标签: web-services spring-batch


【解决方案1】:

您可以从您的其他 Put/Post 方法启动 spring 批处理。由于 CXF 使用 spring,因此使用 cxf 使用 spring 批处理更简单

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job job;


public boolean startJob()
            throws Exception {

        try {

                final JobParameters jobParameters = new JobParametersBuilder().addLong("time", System.nanoTime()).toJobParameters();

                final JobExecution execution = jobLauncher.run(job, jobParameters);
                final ExitStatus status = execution.getExitStatus();

                if (ExitStatus.COMPLETED.getExitCode().equals(status.getExitCode())) {
                    result = true;
                }
            }
        } catch (JobExecutionAlreadyRunningException ex) {

        } catch (JobRestartException ex) {

        } catch (JobInstanceAlreadyCompleteException ex) {

        } catch (JobParametersInvalidException ex) {

        }catch (IOException ex) {

        }

        return false;
    }

【讨论】:

  • 我认为这个解决方案只有在JobLauncher.run()启动一个同步线程时才有效(来自JobLauncher.run() javadocJobExecution如果它同步返回。如果实现是异步的,状态很可能是未知。)
  • 是 JobLauncher.run() 在同步线程中启动。如果您需要异步,您可以在另一个线程中开始工作(如果您有更好的解决方案,请让我知道@bellabax 以了解我的知识)。您可以使用 Async webservice 方法更新状态(cxf 提供注释 Async 使用它您可以实现 aync webservice)
猜你喜欢
  • 1970-01-01
  • 2015-05-13
  • 2023-03-17
  • 2018-04-18
  • 1970-01-01
  • 1970-01-01
  • 2016-06-22
  • 2017-03-28
  • 1970-01-01
相关资源
最近更新 更多