springspring-bootspring-webrequest-mapping

Proper way of streaming using ResponseEntity and making sure the InputStream gets closed


One of our application leaks file handles and we have not yet found the cause for this.

In the code I can see several functions similar to this:

public ResponseEntity<InputStreamResource> getFoo( ... ) {
    InputStream content = getContent(...)
    InputStreamResource isr = new InputStreamResource(content);
    return ResponseEntity.status(HttpServletResponse.SC_OK).body(isr);
}

(if checks and try / catch removed for brevity)

I am sure this section causes the problem because when I loadtest this specific code with JMeter I can see that getContent() fails in this stage:

is = Files.newInputStream(f.toPath());

Normally I would close the InputStream but because this short and simply code I can't close the stream before return or the call of body.

When I run lsof (the code runs on Linux) I can see that thousands of files are open in read mode. So I am sure this problem is caused by the stream not getting closed.

Is there a best practice code I should trade in ?


Solution

  • you can try to use StreamingResponseBody

    StreamingResponseBody

    A controller method return value type for asynchronous request processing where the application can write directly to the response OutputStream without holding up the Servlet container thread.

    Because you are working on a separate thread, writing directly to the response, your problem to call close() before return is solved.

    probably you can start by the following example

    public ResponseEntity<StreamingResponseBody> export(...) throws FileNotFoundException {
        //...
    
        InputStream inputStream = new FileInputStream(new File("/path/to/example/file"));
    
    
        StreamingResponseBody responseBody = outputStream -> {
    
            int numberOfBytesToWrite;
            byte[] data = new byte[1024];
            while ((numberOfBytesToWrite = inputStream.read(data, 0, data.length)) != -1) {
                System.out.println("Writing some bytes..");
                outputStream.write(data, 0, numberOfBytesToWrite);
            }
    
            inputStream.close();
        };
    
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=generic_file_name.bin")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(responseBody);
    }
    

    You can also try to use Files (since java 7)

    so you don't have to manage InputStream

        File file = new File("/path/to/example/file");
    
        StreamingResponseBody responseBody = outputStream -> {
            Files.copy(file.toPath(), outputStream);
        };
    

    As @Stackee007 described in comment, under heavy load in production environment it's a good practice also to define a @Configuration class for a TaskExecutor to tune parameters and manage Async processes.

    @Configuration
    @EnableAsync
    @EnableScheduling
    public class AsyncConfiguration implements AsyncConfigurer {
    
        private final Logger log = LoggerFactory.getLogger(AsyncConfiguration.class);
    
        private final TaskExecutionProperties taskExecutionProperties;
    
        public AsyncConfiguration(TaskExecutionProperties taskExecutionProperties) {
            this.taskExecutionProperties = taskExecutionProperties;
        }
    
        //  ---------------> Tune parameters here
        @Override
        @Bean(name = "taskExecutor")
        public Executor getAsyncExecutor() {
            log.debug("Creating Async Task Executor");
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(taskExecutionProperties.getPool().getCoreSize());
            executor.setMaxPoolSize(taskExecutionProperties.getPool().getMaxSize());
            executor.setQueueCapacity(taskExecutionProperties.getPool().getQueueCapacity());
            executor.setThreadNamePrefix(taskExecutionProperties.getThreadNamePrefix());
            return executor;
        }
        
        //  ---------------> Use this task executor also for async rest methods
        @Bean
        protected WebMvcConfigurer webMvcConfigurer() {
            return new WebMvcConfigurer() {
                @Override
                public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
                    configurer.setTaskExecutor(getTaskExecutor());
                }
            };
        }
    
        @Bean
        protected ConcurrentTaskExecutor getTaskExecutor() {
            return new ConcurrentTaskExecutor(this.getAsyncExecutor());
        }
    
        @Override
        public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
            return new SimpleAsyncUncaughtExceptionHandler();
        }
    }
    

    How to test with mockMvc

    You can simply follow this sample code in your integration test as:

        .andExpect(request().asyncStarted())
        .andDo(MvcResult::getAsyncResult)
        .andExpect(status().isOk()).getResponse().getContentAsByteArray();
    

    Content type of ResponseEntity<StreamingResponseBody> is a MediaType.APPLICATION_OCTET_STREAM in this example and you can get byte[] (.getContentAsByteArray()) but you can get String/Json/plaintext of everything depending of your body response content type.