Recommendations for Using Socket in Java

Writing any web server in Java (whether it is a web server, RESTful webapp or microservice) you can use Sockets for two-channel communication between the client and the server. Using the general classes Socketand ServerSockettrivial, but since Sockets blocked, you create a thread for each request. Using this multi-threaded system, your server will work fine, but will not scale very well. An alternative is to use threads with SocketChannel, ServerSocketChanneland is Selectorclearly not as trivial as shared sockets.

My question is: which of these two systems is used in the finished production code? Am I talking about medium to large projects like Tomcat, Jetty, Sparkjava and the like? I guess they all use the Stream approach, right?

+4
source share
1 answer

To make the web server truly scalable, you will have to implement it with non-blocking I / O - this means that you must make it so that the threads never block, waiting for the I / O to complete.

- . , . . , 1000 , ~ 1 .

( ). , .

, Tomcat Jetty, , , -.

- Tomcat: Tomcat NIO

/ Java Netty.

+3

All Articles