Is there any template / method for step-by-step process execution?

I have an application with Java and Spring on the server side. This server has several modules, and one of them is responsible for the execution of one process. One of the modules has a process that starts at one of the endpoints and has several steps.

Something like that:

@RestController
@AllArgsConstructor
@RequestMapping(value = "movie")
public class MovieController {

    private final MovieService movieService;

    @GetMapping("step1")
    public ResponseEntity step1() {
        return movieService.step1();
    }

    @GetMapping("step2")
    public ResponseEntity step2() {
        return movieService.step2();
    }

    @GetMapping("step3")
    public ResponseEntity step3() {
        return movieService.step3();
    }

    @GetMapping("step4")
    public ResponseEntity step4() {
        return movieService.step4();
    }

    @GetMapping("step5")
    public ResponseEntity step5() {
        return movieService.step5();
    }
}

As you can see, each step can be launched from the user interface in this case. But now I need to add the ability to run all the steps from one endpoint. All steps depend on the state of the previous step, and the next step should never be started if the previous one was unsuccessful. That is why I am looking for the best way to complete all the steps.

I do not like it:

@PostMapping("run")
public ResponseEntity runProcess() {
    movieService.step1();
    movieService.step2();
    movieService.step3();
    movieService.step4();
    return movieService.step5();
}

Spring Batch, movieService Spring ?

?

+4
3

State pattern . , , , ( " - " ) .

+2

Spring MVC AbstractWizardFormController ;

. :

public class UserController extends AbstractWizardFormController{

    public UserController(){
        setCommandName("userForm");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request)
        throws Exception {

        return new User();
    }
    @Override
    protected ModelAndView processFinish(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)
        throws Exception {

        //Get the data from command object
        User user = (User)command;
        System.out.println(user);

        //where is the finish page?
        return new ModelAndView("ResultForm", "user", user);
    }

    @Override
    protected ModelAndView processCancel(HttpServletRequest request,
        HttpServletResponse response, Object command, BindException errors)
        throws Exception {

        //where is the cancel page?
        return new ModelAndView("WelcomePage");
    }

    @Override
    protected void validatePage(Object command, Errors errors, int page) {

        UserValidator validator = (UserValidator) getValidator();

        //page is 0-indexed
        switch (page) {
           case 0: //if page 1 , go validate with validatePage1Form
            validator.validatePage1Form(command, errors);
            break;
           case 1: //if page 2 , go validate with validatePage2Form
            validator.validatePage2Form(command, errors);
            break;
           case 2: //if page 3 , go validate with validatePage3Form
            validator.validatePage3Form(command, errors);
            break;
        }
    }
}
0

. .

:

   public ResponseEntity step...() {
        if(failure){
          throw new CustomException("Error can't complete step 1");
        }
        return movieService.step1();
    }

:

public String runProcess() {
    try {
        movieService.step1();
        movieService.step2();
        movieService.step3();
        movieService.step4();
        movieService.step5();
    }catch(CustomException e){
      return e.getMessage();
    }

}
0
source

Source: https://habr.com/ru/post/1691670/


All Articles