This is a polyglot Helidon HTTP web service that demonstrates how multiple JavaScript Context
s can be executed in parallel to handle asynchronous operations with Helidon, mixing JavaScript Promise
and Java CompletableFuture
objects.
-
Download and install the latest GraalVM JDK using SDKMAN!.
sdk install java 21.0.1-graal
-
Download or clone the repository and navigate into the
js-java-async-helidon
directory:git clone https://github.com/graalvm/graalvm-demos
cd graalvm-demos/js-java-async-helidon
-
Build the application using Maven:
mvn clean package
Now you are all set to run the polyglot Helidon Web service.
You can run this Helidon HTTP web service with the following command:
mvn exec:exec
The application will create a new HTTP web service accepting requests on port 8080
.
Open http://localhost:8080/greet?request=42 in the browser to send a request.
To demonstrate error handling, the application will not accept requests with request
smaller than 42
.
For example, the following requests will return an error message:
curl http://localhost:8080/greet?request=41
curl http://localhost:8080/greet?request=foo
The Helidon HTTP server uses multiple Java threads.
Each thread runs a private JavaScript context.
To generate concurrent requests that will be handled by multiple threads, any workload generator can be used.
For example, using wrk
:
wrk -c 100 -t 10 -d 100 http://localhost:8080/greet?request=42
This is a sample application that, for brevity, contains reasonably large snippets of code inside the strings. This is not the best approach for structuring polyglot apps, but the easiest to show in a compact way.