Search code examples
javaspring-mvcprocessruntimethreadpool

Exit the method after call but method should continue processing submitted request in JAVA


Problem Statement - I have developed a web application which creates set of task based on user input and execute those task on remote server (linux) in sequentail order one by one. Each task takes around 10 minutes of complete. Normally user submit 5 task in one request which means web application takes around 50 minute to show success /failure message on user screen after task submission. I do not want the user to keep waiting for 50 minute in order to show output result , instead I want to render on message page and expecting the method should continue processing on passed data .

Current Implementation - Controller will get all required data to prepare task from user and pass to service class which will create task list - List and start executing them as below by calling executeTask method. Once the complete task get executed then only I can show final Success /Failure message to user as of now. But I want to exit after calling executeTask() by passing taskList and expecting below Outcome -

1) executeTask() method should continue processing submitted data even after exit from method.

2) I will render user to Success Page with message - Your request has been submitted successfully. You will get notification message after task execution completion.

public List<String> executeTask(List<Task> taskList){
        List<String> executionStatusList = new ArrayList<String>();

        Process process = null;

        for( Task task : taskList) {

          try {

                    process = Runtime.getRuntime().exec(task );

                    if (process.waitFor() == 0) {
                        int exitVal = process.exitValue();
                        executionStatusList.add("SUCCESS");

                } else {
                        executionStatusList.add("ERROR");
                break; //Since error occured while processing the task , exiting without processing other task with error status.
                    }
                } catch (IOException ioException) {
                //LOGS
                } catch (InterruptedException intrruptdExcptn) {
                //LOGS
                } finally {
                    if (null != process) {
                        process.destroy();
                        executionStatusList.add("SUCCESS");
                    } else {
                        executionStatusList.add("ERROR");
                       break; //Since error occured while processing the task , exiting without processing other task with error status.
                    }

                }



        }
        return executionStatusList;
    }

Solution

  • I have used spring supported Asynchronous feature to achieve given requirement. We need to add Asynchronous feature over the method which takes time to process. By doing so , that particular method will keep running on background and we can populate Submit message to user.

    Code Sample

      @EnableAsync
        public class ServiceClassName {
    
        /**
        *Method which takes long time to process request. Here Need to add @Asyncn
        */
        @Async("asyncExecutor")
        public void methodName ( DataaType inputParam) {
    
        //Your business logic goes here
    
        //If you want to return something, then do link  as below-
        //return CompletableFuture.completedFuture(returnValue);
        }
    
        }