user990855
user990855

Reputation: 67

Spring Batch - Call as webservice

I have a Spring Batch job. I am new to Spring Batch and have always been called via the CommandLineJobRunner.

This is what my call looks like:

org.springframework.batch.core.launch.support.CommandLineJobRunner spring-batch-myProject.xml SpringJobBean.MyProjectImportDataJob

Now I have to call my batch job from within a webservice (Spring MVC). In my endpoint this is the invoke call. I need to call the batch job in the if statement. How would I do this? I read about JobLauncher...but not sure how to tell it what to launch?

protected Object invokeInternal(Object aObj) throws Exception {
    RunDataProcessingImportRequest request = (RunDataProcessingImportRequest) aObj;

    RunDataProcessingImportResponse response = new RunDataProcessingImportResponse();
    if (request.getDataProcessingType().equals(PROJECT_TYPE)){

        response.setResultCd(1);
    } else {
        response.setResultCd(0);
        response.setErrorCode(1l);
        response.setErrorMessage("Incorrect process type");
    }
    return response;
}

Upvotes: 1

Views: 2863

Answers (1)

Michael Minella
Michael Minella

Reputation: 21463

The answer to this really depends on the version of Spring Batch you're using.

If you're using 2.0.x or older you can use Spring Batch Admin to provide REST endpoints for starting/stopping/etc jobs. All you need to do is add the jars to your app and provide a small amount of configuration.

If you're using 2.2.x or newer and are allowed to use the snapshot versions of Spring Batch Admin, the same applies as mentioned above.

If you're not interested in using Spring Batch Admin, you'll need to write your own endpoint and launch the job from there. However, it should be fairly trivial (I haven't tested the code below):

@Controller
public class JobLaunchingController {
    @Autowire
    JobLauncher jobLauncher;

    @Autowire
    JobRegistry jobRegistry;

    @RequestMapping("/launch")
    public @ResponseBody JobExecution launch(
            @RequestParam(value="name", required=true) String name,
            @RequestParam(value="params", required=false) String params) {

        Job job = jobRegistry.getJob(name);
        JobParametersBuilder paramsBuilder = new JobParametersBuilder();

        if(params != null) {
            // parse job parameters
        }

        return jobLauncher.run(job, paramsBuilder.toJobParameters());
    }
}

The above code assumes you have more than one job to provide the ability to execute. If not, you could just @Autowire the Job itself into your controller if you wanted (instead of the JobRegistry).

You can read more about REST services in Spring here: https://spring.io/guides/gs/rest-service/

You can read more about the JobLauncher here: http://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/core/launch/JobLauncher.html

Finally, you can read more about the JobRegistry in section 4.6.2 here: http://docs.spring.io/spring-batch/reference/html/configureJob.html

Upvotes: 1

Related Questions