Skip to content

Commit bf25199

Browse files
committed
Add threading
1 parent 799b135 commit bf25199

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ hs_err_pid*
2626
*.idea
2727
*.iml
2828

29-
/src/tests
29+
test/

src/express/Express.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.io.IOException;
1515
import java.net.InetSocketAddress;
1616
import 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

Comments
 (0)