Skip to content

Commit 1758fab

Browse files
committed
Feat. New options. Application Host and custom HTTP Server Executor
1 parent 763d822 commit 1758fab

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/main/java/io/eigr/spawn/api/Spawn.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.net.InetSocketAddress;
2020
import java.util.*;
21+
import java.util.concurrent.Executor;
2122
import java.util.concurrent.Executors;
2223
import java.util.function.Function;
2324
import java.util.stream.Collectors;
@@ -27,24 +28,31 @@
2728
*/
2829
public final class Spawn {
2930
private static final Logger log = LoggerFactory.getLogger(Spawn.class);
31+
private static final String HTTP_ACTORS_ACTIONS_URI = "/api/v1/actors/actions";
3032

3133
private final SpawnClient client;
3234

3335
private final int port;
36+
37+
private String host;
38+
3439
private final String proxyHost;
3540
private final int proxyPort;
3641
private final String system;
3742
private final List<Entity> entities;
3843

39-
private String host;
44+
private Optional<Executor> optionalExecutor;
45+
4046

4147
private Spawn(SpawnSystem builder) {
4248
this.system = builder.system;
4349
this.entities = builder.entities;
4450
this.port = builder.port;
51+
this.host = builder.host;
4552
this.proxyHost = builder.proxyHost;
4653
this.proxyPort = builder.proxyPort;
4754
this.client = builder.client;
55+
this.optionalExecutor = builder.optionalExecutor;
4856
}
4957

5058
public int getPort() {
@@ -78,9 +86,12 @@ public void start() throws Exception {
7886

7987
private void startServer() throws IOException {
8088
HttpServer httpServer = HttpServer.create(new InetSocketAddress("127.0.0.1", this.port), 0);
81-
httpServer.createContext("/api/v1/actors/actions", new ActorServiceHandler(this, this.entities));
82-
//httpServer.setExecutor(Executors.newVirtualThreadPerTaskExecutor());
83-
httpServer.setExecutor(Executors.newCachedThreadPool());
89+
httpServer.createContext(HTTP_ACTORS_ACTIONS_URI, new ActorServiceHandler(this, this.entities));
90+
if (this.optionalExecutor.isPresent()) {
91+
httpServer.setExecutor(this.optionalExecutor.get());
92+
} else {
93+
httpServer.setExecutor(Executors.newCachedThreadPool());
94+
}
8495
httpServer.start();
8596
}
8697

@@ -201,9 +212,9 @@ public String toString() {
201212
return new StringJoiner(", ", Spawn.class.getSimpleName() + "[", "]")
202213
.add("system='" + system + "'")
203214
.add("port=" + port)
215+
.add("host='" + host + "'")
204216
.add("proxyHost='" + proxyHost + "'")
205217
.add("proxyPort=" + proxyPort)
206-
.add("host='" + host + "'")
207218
.toString();
208219
}
209220

@@ -212,10 +223,13 @@ public static final class SpawnSystem {
212223
private SpawnClient client;
213224
private final List<Entity> entities = new ArrayList<>();
214225
private int port = 8091;
226+
private String host = "127.0.0.1";
215227
private String proxyHost = "127.0.0.1";
216228
private int proxyPort = 9001;
217229
private String system = "spawn-system";
218230

231+
private Optional<Executor> optionalExecutor;
232+
219233
public SpawnSystem create(String system) {
220234
this.system = system;
221235
return this;
@@ -226,6 +240,11 @@ public SpawnSystem withPort(int port) {
226240
return this;
227241
}
228242

243+
public SpawnSystem withHost(String host) {
244+
this.host = host;
245+
return this;
246+
}
247+
229248
public SpawnSystem withProxyHost(String host) {
230249
this.proxyHost = host;
231250
return this;
@@ -236,6 +255,11 @@ public SpawnSystem withProxyPort(int port) {
236255
return this;
237256
}
238257

258+
public SpawnSystem withHttpHandlerExecutor(Executor executor) {
259+
this.optionalExecutor = Optional.ofNullable(executor);
260+
return this;
261+
}
262+
239263
public SpawnSystem addActor(Class<?> actorKlass) {
240264
Optional<Entity> maybeEntity = getEntity(actorKlass);
241265
if (maybeEntity.isPresent()) {

0 commit comments

Comments
 (0)