How to trigger a job using a rest web service?

为君一笑 提交于 2019-12-13 07:46:11

问题


I want to create a rest web service using CXF or jersey to invoke a spring batch job. Is it possible. If so, how can I do that?


回答1:


You can start the spring batch from your rest Put/Post method. Since CXF uses spring its simpler to use spring batch with cxf

@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;
    }


来源:https://stackoverflow.com/questions/20248910/how-to-trigger-a-job-using-a-rest-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!