Возврат вывода и потока ошибок процесса в виде загружаемого файла через интерфейс REST - PullRequest
0 голосов
/ 22 марта 2020

Какой удобный способ создания конечной точки REST, которая выполняет процесс и обслуживает вывод stdout + stderr в качестве загружаемого файла?

1 Ответ

0 голосов
/ 22 марта 2020

Вот что я использовал в итоге:

public ResponseEntity<StreamingResponseBody> example() throws IOException {
    Process p = new ProcessBuilder(/*...*/).redirectErrorStream(true).start();

    //better to use a static executor, but here I'm creating a new one every time
    Executors.newSingleThreadScheduledExecutor().schedule(() -> {
        if (p != null && p.isAlive()) {
            p.destroyForcibly(); //Destroy the process after a timeout period if it's still running
        }
    }, 10, TimeUnit.SECONDS);

    //No need to read err stream separately, it's redirected to stdout due to "redirectErrorStream"
    InputStream inputStream = p.getInputStream(); 

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    response.setHeader("Content-Disposition", "attachment; filename=foo.gz");

    return new ResponseEntity<>(new StreamingResponseBody() {
        @Override
        public void writeTo(OutputStream outputStream) throws IOException {
            try (GZIPOutputStream gzOut = new GZIPOutputStream(outputStream)) {
                while (p.isAlive()) {
                    inputStream.transferTo(gzOut);
                    gzOut.flush();
                }
            }
        }
    }, headers, HttpStatus.OK);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...