Spring Batch: starting a job from within a Spring MVC contorller WITH A NEW THREAD

前端 未结 3 1040
夕颜
夕颜 2021-01-01 03:45

I have a Spring-Batch job that I launch from a Spring MVC controller. The controller gets an uploaded file from the user and the job is supposed to process the file:

3条回答
  •  清酒与你
    2021-01-01 04:22

    The jobLauncher.run() method can be called in a new Thread like so:

    @RequestMapping(value = "/upload")
    public ModelAndView uploadInventory(UploadFile uploadFile, BindingResult bindingResult) {
      [...]
    
      final SomeObject jobLauncher = [...]
      Thread thread = new Thread(){
        @Override
        public void run(){
          jobLauncher.run([...]);
        }
      };
      thread.start();
    
      return mav;
    }
    

    The thread.start() line will spawn a new thread, and then continue to execute the code below it.

    Note that, if jobLauncher is a local variable, it must be declared final in order for it to be used inside of the anonymous Thread class.

提交回复
热议问题