forked from helidon-io/helidon
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WebServer Threadpool example (helidon-io#2836)
* Add WebServer threadpool example
- Loading branch information
Showing
10 changed files
with
696 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Helidon WebServer Thread Pool Example | ||
|
||
This example shows how to use an application specific threadpool. | ||
|
||
With the Helidon WebServer you do not want to block the Netty thread that is executing | ||
your Handler. So you either need to use WebServer's reactive APIs: | ||
|
||
``` | ||
request.content().as(JsonObject.class) | ||
.thenAccept(jo -> doSomething(jo, response)) | ||
``` | ||
|
||
Or pass the request off to a thread pool dedicated to your business logic. This | ||
example shows how to do this in `getMessageSlowlyHandler`. | ||
|
||
Helidon's `ThreadPoolSupplier` provides thread pools that automatically propagate | ||
request `Context` so that tracing and authentication information is preserved across | ||
threads. | ||
|
||
You can use the `Context` registry to propagate your own data across threads. You can | ||
also do this by passing the information directly to your `Runnable`. The | ||
example shows both techniques. | ||
|
||
## Configuration | ||
|
||
See `application.yaml` for an example of how you can configure the number of Netty | ||
worker threads, as well as provide a configuration for the dedicated application | ||
threadpool. | ||
|
||
## Build and run | ||
|
||
With JDK11+ | ||
```bash | ||
mvn package | ||
java -jar target/helidon-examples-webserver-threadpool.jar | ||
``` | ||
|
||
When the server starts up you will see it log some information about the application | ||
thread pool. This should reflect what is specified in `application.yaml`. For example: | ||
|
||
``` | ||
2021.03.16 13:50:31 FINE io.helidon.common.configurable.ThreadPool Thread[main,5,main]: ThreadPool 'helidon-thread-pool-2' {corePoolSize=5, maxPoolSize=50, queueCapacity=10000, growthThreshold=1000, growthRate=0%, averageQueueSize=0.00, peakQueueSize=0, averageActiveThreads=0.00, peakPoolSize=0, currentPoolSize=0, completedTasks=0, failedTasks=0, rejectedTasks=0} | ||
``` | ||
|
||
See `logging.properties` for the logging configuration. | ||
|
||
## Exercise the application | ||
|
||
Each request will return the name of the thread the created the response. For example: | ||
|
||
``` | ||
$ curl -X GET http://localhost:8080/greet/Jane | ||
{"message":"Hello Jane!","thread":"Thread[nioEventLoopGroup-3-2,10,main]"} | ||
``` | ||
|
||
`nioEventLoopGroup-` indicates that this is a Netty worker thread. To exercise | ||
the application thread pool do this: | ||
|
||
``` | ||
curl -X GET http://localhost:8080/greet/slowly/Jane | ||
{"message":"Hello Jane!","thread":"Thread[my-thread-1,5,helidon-thread-pool-2]"} | ||
``` | ||
|
||
You'll notice that the response takes ~3 seconds to return -- that's an artificial delay | ||
we have in our handler. Also note that the thread name starts with `my-thread-`. That indicates | ||
this is a thread from the dedicated application thread pool. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2021 Oracle and/or its affiliates. All rights reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.helidon.applications</groupId> | ||
<artifactId>helidon-se</artifactId> | ||
<version>2.3.0-SNAPSHOT</version> | ||
<relativePath>../../../applications/se/pom.xml</relativePath> | ||
</parent> | ||
<groupId>io.helidon.examples.webserver</groupId> | ||
<artifactId>helidon-examples-webserver-threadpool</artifactId> | ||
<name>Helidon WebServer Examples Thread Pools</name> | ||
|
||
<properties> | ||
<mainClass>io.helidon.examples.webserver.threadpool.Main</mainClass> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.helidon.webserver</groupId> | ||
<artifactId>helidon-webserver</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.media</groupId> | ||
<artifactId>helidon-media-jsonp</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.config</groupId> | ||
<artifactId>helidon-config-yaml</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.health</groupId> | ||
<artifactId>helidon-health</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.health</groupId> | ||
<artifactId>helidon-health-checks</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.metrics</groupId> | ||
<artifactId>helidon-metrics</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.helidon.webclient</groupId> | ||
<artifactId>helidon-webclient</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-dependency-plugin</artifactId> | ||
<executions> | ||
<execution> | ||
<id>copy-libs</id> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
205 changes: 205 additions & 0 deletions
205
...erver/threadpool/src/main/java/io/helidon/examples/webserver/threadpool/GreetService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
/* | ||
* Copyright (c) 2021 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.examples.webserver.threadpool; | ||
|
||
import java.util.Collections; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonBuilderFactory; | ||
import javax.json.JsonException; | ||
import javax.json.JsonObject; | ||
|
||
import io.helidon.common.configurable.ThreadPoolSupplier; | ||
import io.helidon.common.context.Contexts; | ||
import io.helidon.common.http.Http; | ||
import io.helidon.config.Config; | ||
import io.helidon.webserver.Routing; | ||
import io.helidon.webserver.ServerRequest; | ||
import io.helidon.webserver.ServerResponse; | ||
import io.helidon.webserver.Service; | ||
|
||
/** | ||
* A simple service to greet you. Examples: | ||
* | ||
* Get default greeting message: | ||
* curl -X GET http://localhost:8080/greet | ||
* | ||
* Get greeting message for Joe: | ||
* curl -X GET http://localhost:8080/greet/Joe | ||
* | ||
* Change greeting | ||
* curl -X PUT -H "Content-Type: application/json" -d '{"greeting" : "Howdy"}' http://localhost:8080/greet/greeting | ||
* | ||
* The message is returned as a JSON object | ||
*/ | ||
|
||
public class GreetService implements Service { | ||
|
||
/** | ||
* The config value for the key {@code greeting}. | ||
*/ | ||
private final AtomicReference<String> greeting = new AtomicReference<>(); | ||
|
||
private static final JsonBuilderFactory JSON = Json.createBuilderFactory(Collections.emptyMap()); | ||
|
||
private static final Logger LOGGER = Logger.getLogger(GreetService.class.getName()); | ||
|
||
private static ExecutorService myThreadPool; | ||
|
||
GreetService(Config config) { | ||
greeting.set(config.get("app.greeting").asString().orElse("Ciao")); | ||
|
||
// Build a thread pool using the configuration | ||
myThreadPool = ThreadPoolSupplier.builder().config(config.get("application-thread-pool")).build().get(); | ||
} | ||
|
||
/** | ||
* A service registers itself by updating the routing rules. | ||
* @param rules the routing rules. | ||
*/ | ||
@Override | ||
public void update(Routing.Rules rules) { | ||
rules | ||
.get("/", this::getDefaultMessageHandler) | ||
.get("/slowly/{name}", this::getMessageSlowlyHandler) | ||
.get("/{name}", this::getMessageHandler) | ||
.put("/greeting", this::updateGreetingHandler); | ||
|
||
} | ||
|
||
/** | ||
* Return a worldly greeting message. | ||
* @param request the server request | ||
* @param response the server response | ||
*/ | ||
private void getDefaultMessageHandler(ServerRequest request, ServerResponse response) { | ||
sendResponse(response, "World"); | ||
} | ||
|
||
/** | ||
* Return a greeting message using the name that was provided. | ||
* @param request the server request | ||
* @param response the server response | ||
*/ | ||
private void getMessageHandler(ServerRequest request, ServerResponse response) { | ||
String name = request.path().param("name"); | ||
sendResponse(response, name); | ||
} | ||
|
||
/** | ||
* Slowly Return a greeting message using the name that was provided. | ||
* @param request the server request | ||
* @param response the server response | ||
*/ | ||
private void getMessageSlowlyHandler(ServerRequest request, ServerResponse response) { | ||
|
||
String name = request.path().param("name"); | ||
|
||
// One way to pass data to new thread is to use Context | ||
request.context().register("NAME_PARAM", name + "_from_context"); | ||
|
||
// Another way, just pass via Runnable. | ||
myThreadPool.submit(() -> sendResponseSlowly(response, name, 3)); | ||
} | ||
|
||
/** | ||
* Send a response slowly. This simulates blocking business logic. | ||
* | ||
* @param response server response | ||
* @param name name to greet | ||
* @param sleepSeconds artificial delay to simulate blocking business logic | ||
*/ | ||
private void sendResponseSlowly(ServerResponse response, String name, int sleepSeconds) { | ||
|
||
// Fetch NAME_PARAM from Context | ||
String nameFromContext = Contexts.context() | ||
.flatMap(ctx -> ctx.get("NAME_PARAM", String.class)) | ||
.orElseThrow(() -> new IllegalStateException("No NAME_PARAM in current context")); | ||
|
||
LOGGER.info("Name from method parameter: " + name + ". Name from context: " + nameFromContext); | ||
|
||
// Simulate blocking business logic | ||
try { | ||
Thread.sleep(sleepSeconds * 1000); | ||
} catch (InterruptedException ex) { } | ||
|
||
sendResponse(response, name); | ||
} | ||
|
||
private void sendResponse(ServerResponse response, String name) { | ||
LOGGER.info("Response sent by thread " + Thread.currentThread().toString()); | ||
|
||
String msg = String.format("%s %s!", greeting.get(), name); | ||
|
||
JsonObject returnObject = JSON.createObjectBuilder() | ||
.add("message", msg) | ||
.add("thread", Thread.currentThread().toString()) | ||
.build(); | ||
response.send(returnObject); | ||
} | ||
|
||
private static <T> T processErrors(Throwable ex, ServerRequest request, ServerResponse response) { | ||
|
||
if (ex.getCause() instanceof JsonException){ | ||
|
||
LOGGER.log(Level.FINE, "Invalid JSON", ex); | ||
JsonObject jsonErrorObject = JSON.createObjectBuilder() | ||
.add("error", "Invalid JSON") | ||
.build(); | ||
response.status(Http.Status.BAD_REQUEST_400).send(jsonErrorObject); | ||
} else { | ||
|
||
LOGGER.log(Level.FINE, "Internal error", ex); | ||
JsonObject jsonErrorObject = JSON.createObjectBuilder() | ||
.add("error", "Internal error") | ||
.build(); | ||
response.status(Http.Status.INTERNAL_SERVER_ERROR_500).send(jsonErrorObject); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void updateGreetingFromJson(JsonObject jo, ServerResponse response) { | ||
if (!jo.containsKey("greeting")) { | ||
JsonObject jsonErrorObject = JSON.createObjectBuilder() | ||
.add("error", "No greeting provided") | ||
.build(); | ||
response.status(Http.Status.BAD_REQUEST_400) | ||
.send(jsonErrorObject); | ||
return; | ||
} | ||
|
||
greeting.set(jo.getString("greeting")); | ||
response.status(Http.Status.NO_CONTENT_204).send(); | ||
} | ||
|
||
/** | ||
* Set the greeting to use in future messages. | ||
* @param request the server request | ||
* @param response the server response | ||
*/ | ||
private void updateGreetingHandler(ServerRequest request, | ||
ServerResponse response) { | ||
request.content().as(JsonObject.class) | ||
.thenAccept(jo -> updateGreetingFromJson(jo, response)) | ||
.exceptionally(ex -> processErrors(ex, request, response)); | ||
} | ||
} |
Oops, something went wrong.