I am wondering that if servlet containers like Tomcat, Jetty etc already use nio to read and write data back, is there really a need of using setWritelistner
and setReadListner
on servlet input and output streams? Is there any additional performance gain?
The benefit is not directly1 about "performance gain". The purpose of those methods is to avoid a request thread (in async mode) from blocking when it reads input (POST) data or writes the document.
There is an example in the Java EE7 tutorial: "17.13.1 Reading a Large HTTP POST Request Using Non-Blocking I/O" (link updated).
This is orthogonal to Tomcat's use of nio under the covers.
1 - There is an indirect performance benefit. When it is likely for threads to block on network I/O, an alternative strategy for increasing throughput is to increase the number of worker threads. But that increases the memory footprint (among other things) resulting in more "overheads".