1111import java .io .IOException ;
1212import java .io .InputStream ;
1313import java .io .OutputStream ;
14- import java .util .concurrent .ExecutorService ;
15- import java .util .concurrent .Executors ;
14+ import java .util .concurrent .*;
1615
1716public class UndertowHTTPSocketServer implements IHTTPSocketServer {
1817
1918 private int port = 80 ;
19+ private int maxThreads = 64 ;
2020 private Undertow server ;
2121 private IHTTPSocketHandler handler ;
2222 private ExecutorService executorService ;
@@ -30,11 +30,12 @@ public int getPort() {
3030 }
3131
3232 public void start () throws IOException {
33- executorService = Executors . newCachedThreadPool ( );
33+ executorService = new ThreadPoolExecutor ( 1 , maxThreads , 60L , TimeUnit . SECONDS , new SynchronousQueue () );
3434 server = Undertow .builder ()
3535 .addHttpListener (port , "0.0.0.0" )
3636 .setServerOption (Options .KEEP_ALIVE , true )
3737 .setHandler (new BlockingHandler (httpServerExchange -> {
38+ httpServerExchange .setDispatchExecutor (executorService );
3839 if (httpServerExchange .getRequestHeaders ().contains ("sec-websocket-key" )) {
3940 httpServerExchange .upgradeChannel ((streamConnection , httpServerExchange1 ) -> {
4041 InputStream inputStream = new StreamSourceInputStream (streamConnection .getSourceChannel ());
@@ -68,6 +69,10 @@ public void setHandler(IHTTPSocketHandler handler) {
6869 this .handler = handler ;
6970 }
7071
72+ public void setMaxThreads (int maxThreads ) {
73+ this .maxThreads = maxThreads ;
74+ }
75+
7176 public boolean isWebSocketSupported () {
7277 return true ;
7378 }
0 commit comments