0

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
    This is not a place where ask for code! Try yourself and if you will be in trouble post your problem, we will be happy to help you! – Luca Basso Ricci Nov 28 '13 at 09:18

1 Answers1

0

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;
    }
Karthik Prasad
  • 9,662
  • 10
  • 64
  • 112
  • I think this solution is valid only if `JobLauncher.run()` starts a synhronous thread (from `JobLauncher.run() javadoc` : _the JobExecution if it returns synchronously. If the implementation is asynchronous, the status might well be unknown._) – Luca Basso Ricci Nov 28 '13 at 09:15
  • Yes JobLauncher.run() starts in a sync thread. If you need a async you can start the job in another thread( if you have any better solution, please do let me know @bellabax for my knowlegde). you can update back the status using Async webservice method(cxf provides annotation Async using which you can implement aync webservice) – Karthik Prasad Nov 28 '13 at 09:22