1

I have this post method:

 @PostMapping("/upload")
        public String singleFileUpload(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes)
                throws IOException {
    ExecutorService service= Executors.newSingleThreadExecutor();
            Future<String> future=service.submit(new Callable<String>() {
                @Override
                public String call() throws Exception {
                    //parse file
                    Thread.sleep(5000);
                    return "done";
                }
            });
    String result=future.get();
     service.shutdown();
    return "redirect:uploadState";
    }

I want to redirect to uploadState while executor parse my file and uploadState have long polling ajax to notify if parsing is done or not.Can help me with some hints.

1 Answer 1

2

Here future.get() is blocker.

You can use Java 8 CompletableFuture :

CompletableFuture.supplyAsync(() -> {
    try{
   Thread.sleep(5000);
   return "done";
 }catch(Exception ex){}
}).thenApply((res->)->{
  return res;
});

It will not block your control.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.