Executing a job from a controller method

It's convenient to launch a job from a controller method when that job is triggered by a user action. For example, launching a job to process a video just uploaded by the user.

Getting ready

We'll use the job defined in the Creating a job recipe.

How to do it…

Follow these steps to execute the job from a controller method:

  1. Add the Spring Batch configuration class to the getServletConfigClasses()method in your class extending AbstractAnnotationConfigDispatcherServletInitializer:
    public class ServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{AppConfig.class, BatchConfig.class};
    }
  2. In your controller class, ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.