From 94db1aab5c2bee89f2831589c30c3b73bcada652 Mon Sep 17 00:00:00 2001 From: hlafaille Date: Fri, 9 Aug 2024 15:13:47 +0000 Subject: [PATCH] #4 adding worker thread name generator --- .devcontainer/devcontainer.json | 3 +- pom.xml | 2 +- .../kerosenelabs/kindling/KindlingServer.java | 35 +++++++++++++++++-- .../java/io/kerosenelabs/kindling/Main.java | 4 +-- .../kindling/WorkerThreadNameGenerator.java | 5 +++ .../kindling/handler/RequestHandler.java | 3 +- 6 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/kerosenelabs/kindling/WorkerThreadNameGenerator.java diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8ec3e83..e1f2e3b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -56,6 +56,5 @@ }, "forwardPorts": [ 8443 - ], - "postAttachCommand": "gradle" + ] } \ No newline at end of file diff --git a/pom.xml b/pom.xml index 50a2952..942838d 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ io.kerosenelabs kindling jar - 0.1.1 + 0.1.2 kindling http://maven.apache.org diff --git a/src/main/java/io/kerosenelabs/kindling/KindlingServer.java b/src/main/java/io/kerosenelabs/kindling/KindlingServer.java index e768b32..4e0e194 100644 --- a/src/main/java/io/kerosenelabs/kindling/KindlingServer.java +++ b/src/main/java/io/kerosenelabs/kindling/KindlingServer.java @@ -5,10 +5,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; +import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; +import java.util.UUID; import java.util.concurrent.atomic.AtomicReference; import javax.net.ssl.SSLServerSocket; @@ -25,6 +26,12 @@ public class KindlingServer { private static KindlingServer instance = null; private List requestHandlers = new ArrayList<>(); + private WorkerThreadNameGenerator workerThreadNameGenerator = new WorkerThreadNameGenerator() { + @Override + public String generate() { + return "req:" + UUID.randomUUID().toString(); + } + }; private KindlingServer() { } @@ -36,6 +43,23 @@ public static KindlingServer getInstance() { return instance; } + /** + * Set the worker thread name generator. By default, this defaults to a + * anonymous class that generates a random UUID. + * + * @param workerThreadNameGeneratorClass + * @throws KindlingException + */ + public void setWorkerThreadNameGenerator(Class workerThreadNameGeneratorClass) + throws KindlingException { + try { + workerThreadNameGenerator = workerThreadNameGeneratorClass.getDeclaredConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e) { + throw new KindlingException(e); + } + } + /** * Install a {@link RequestHandler}. * @@ -99,7 +123,8 @@ public void serve(int port, Path keystorePath, String keyStorePassword) throws K * @param sslSocket */ private void dispatchWorker(SSLSocket sslSocket) { - Thread.startVirtualThread(() -> { + String workerThreadName = workerThreadNameGenerator.generate(); + Thread.ofVirtual().name(workerThreadName).start(() -> { try ( InputStream inputStream = sslSocket.getInputStream(); InputStreamReader inputStreamReder = new InputStreamReader(inputStream); @@ -113,7 +138,11 @@ private void dispatchWorker(SSLSocket sslSocket) { HttpResponse response = null; for (RequestHandler requestHandler : requestHandlers) { if (requestHandler.accepts(httpRequest.getHttpMethod(), httpRequest.getResource())) { - response = requestHandler.handle(httpRequest); + try { + response = requestHandler.handle(httpRequest); + } catch (Exception e) { + response = requestHandler.handleError(e); + } } } diff --git a/src/main/java/io/kerosenelabs/kindling/Main.java b/src/main/java/io/kerosenelabs/kindling/Main.java index 51dda6e..4e7f3c7 100644 --- a/src/main/java/io/kerosenelabs/kindling/Main.java +++ b/src/main/java/io/kerosenelabs/kindling/Main.java @@ -32,10 +32,10 @@ public HttpResponse handle(HttpRequest httpRequest) throws KindlingException { .status(HttpStatus.OK) .headers(new HashMap<>() { { - put("Content-Type", "text/html"); + put("Content-Type", "application/json"); } }) - .content("

Hello from Kindling!

") + .content("{\"key\": \"value\"}") .build(); } }); diff --git a/src/main/java/io/kerosenelabs/kindling/WorkerThreadNameGenerator.java b/src/main/java/io/kerosenelabs/kindling/WorkerThreadNameGenerator.java new file mode 100644 index 0000000..d302153 --- /dev/null +++ b/src/main/java/io/kerosenelabs/kindling/WorkerThreadNameGenerator.java @@ -0,0 +1,5 @@ +package io.kerosenelabs.kindling; + +public interface WorkerThreadNameGenerator { + public String generate(); +} diff --git a/src/main/java/io/kerosenelabs/kindling/handler/RequestHandler.java b/src/main/java/io/kerosenelabs/kindling/handler/RequestHandler.java index 2715e7b..9afd3e4 100644 --- a/src/main/java/io/kerosenelabs/kindling/handler/RequestHandler.java +++ b/src/main/java/io/kerosenelabs/kindling/handler/RequestHandler.java @@ -15,10 +15,11 @@ public abstract class RequestHandler { * Called from {@link io.kerosenelabs.kindling.Server} if an error occurs during * {@link RequestHandler#handle(HttpRequest)} * + * @param The throwable that occurred * @return * @throws KindlingException */ - public HttpResponse doError() { + public HttpResponse handleError(Throwable t) { return new HttpResponse.Builder() .status(HttpStatus.INTERNAL_SERVER_ERROR) .content("Internal Server Error")