Stream Support
Micronaut also supports binding the body to an InputStream
. If the method is reading the stream, the method execution must be offloaded to another thread pool to avoid blocking the event loop.
Performing Blocking I/O With InputStream
@Post(value = "/read", processes = MediaType.TEXT_PLAIN)
@ExecuteOn(TaskExecutors.IO) (1)
String read(@Body InputStream inputStream) throws IOException { (2)
return IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream))); (3)
}
Performing Blocking I/O With InputStream
@Post(value = "/read", processes = MediaType.TEXT_PLAIN)
@ExecuteOn(TaskExecutors.IO) (1)
String read(@Body InputStream inputStream) throws IOException { (2)
IOUtils.readText(new BufferedReader(new InputStreamReader(inputStream))) (3)
}
Performing Blocking I/O With InputStream
@Post(value = "/read", processes = [MediaType.TEXT_PLAIN])
@ExecuteOn(TaskExecutors.IO) (1)
fun read(@Body inputStream: InputStream): String { (2)
return IOUtils.readText(BufferedReader(InputStreamReader(inputStream))) (3)
}
1 | The controller method is executed on the IO thread pool |
2 | The body is passed to the method as an input stream |
3 | The stream is read |