0

I'm creating web application using spring boot, in this web application i need to run a command line using Runtime.getRuntime(), but the problem is that: this command is executed after the web app and this is not what i want to do, the question is :

"how can i make sure that the Runtime.getRuntime() instruction start when it's called in spring boot application (not at the end of it)"

the controller:

@PostMapping("/toLinPDf")
public ResponseEntity<ByteArrayResource> convertion(@RequestParam(value = "input", required = false) String in,
        @RequestParam(value = "output", required = false) String out) throws IOException, InterruptedException, ExecutionException {

    // the methode LinearizePDF contain the command line

    linearizeService.LinearizePDf(in, out);
    logger.warn("call the method linearizeService.LinearizePDf ");
    FileSystemResource result = new FileSystemResource(out);
    return ResponseEntity
            .ok()
            .contentLength(result.contentLength())
            .contentType(
                    MediaType.parseMediaType("application/pdf"))
            .body(new ByteArrayResource(IOUtils.toByteArray(result.getInputStream())));

}

the linearizeService ( contain the method LinearizePDf(in, out)):

@Async
public void LinearizePDf(String Input , String Output) throws IOException, InterruptedException {
     Runtime rt = Runtime.getRuntime();

     // the command line 
     String command = "qpdf --linearize " + Input + "  " + Output;

     Process pr = rt.exec(command );
     logger.warn("run the command");
     pr.destroy();   
}

Please, If there's any suggestion, do not hesitate.

Thank You !

1 Answer 1

1

Quoting the well known site

Simply put – annotating a method of a bean with @Async will make it execute in a separate thread i.e. the caller will not wait for the completion of the called method.

Remove the @Async from LinearizePDf

In addition, to wait for the completion of the external process you should use waitFor and not destroy, e.g. pr.waitFor()

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

5 Comments

Actually what i want to do is: " to run the command line and the java program in the same time " , in my case the command line is executed when the java app finish runnig
Did you remove the Async? It will give you the possibility to return right ResponseEntity from the controller
indeed , i just remove the @Async and i get the same result, it's not working .
Added waitFor - see the answer
Well i guess @Async is part of the problem you're right so i remove it and put it in the controller and also FileSystemResource somehow generate a problem : " file [D:\[Phenix-Monitor]1.pdf] cannot be resolved in the file system for checking its content length " so i remove it and it's working !

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.