1414import java .io .IOException ;
1515import java .net .InetSocketAddress ;
1616import java .util .ArrayList ;
17+ import java .util .concurrent .Executor ;
18+ import java .util .concurrent .Executors ;
1719
1820/**
1921 * @author Simon Reinisch
@@ -27,6 +29,7 @@ public class Express extends ExpressMiddleware {
2729 private final ExpressFilterChain MIDDLEWARE_CHAIN = new ExpressFilterChain ();
2830 private final ExpressFilterChain FILTER_CHAIN = new ExpressFilterChain ();
2931
32+ private Executor executor ;
3033 private String hostname = "localhost" ;
3134 private HttpServer httpServer ;
3235
@@ -39,6 +42,23 @@ public class Express extends ExpressMiddleware {
3942 public Express (String hostname ) {
4043 if (hostname != null )
4144 this .hostname = hostname ;
45+
46+ this .executor = Executors .newCachedThreadPool ();
47+ }
48+
49+ /**
50+ * Set an executor service. Default is CachedThreadPool
51+ * Can only changed if the server isn't already stardet.
52+ *
53+ * @param executor The new executor.
54+ * @throws IOException If the server is currently running
55+ */
56+ public void setExecutor (Executor executor ) throws IOException {
57+ if (httpServer != null ) {
58+ throw new IOException ("Cannot set the executor after the server has starderd!" );
59+ } else {
60+ this .executor = executor ;
61+ }
4262 }
4363
4464 /**
@@ -218,7 +238,7 @@ public void listen(Action onStart, int port) throws IOException {
218238
219239 // Create http server
220240 httpServer = HttpServer .create (new InetSocketAddress (this .hostname , port ), 0 );
221- httpServer .setExecutor (null );
241+ httpServer .setExecutor (executor );
222242
223243 httpServer .createContext ("/" , httpExchange -> {
224244 Request request = new Request (httpExchange );
0 commit comments