Container platforms and edge computing continue to grow, powering major networks and applications across the globe, and Java technologies have evolved new features and improved performance to match steps with modern infrastructure. Java 17 (OpenJDK 17) was released recently (September 2021) with the following major features:
- Restore Always-Strict Floating-Point Semantics
- Enhanced Pseudo-Random Number Generators
- Strongly Encapsulate JDK Internals
- Pattern Matching for switch (Preview)
- Foreign Function & Memory API (Incubator)
- Vector API (Second Incubator)
- Context-Specific Deserialization Filters
Developers are wondering how to start implementing application logic using the new features of Java 17 and then build and run them on the same OpenJDK 17 runtime. Luckily, Quarkus enables the developers to scaffold a new application with Java 17. It also provides a live coding capability that allows developers to focus only on implementing business logic instead of compiling, building, deploying, and restarting runtimes to apply code changes.
Note: If you haven’t already installed OpenJDK 17, download a binary on your operating system.
This tutorial teaches you to use the Pseudo-Random Number Generators (PRNG) algorithms of Java 17 on Quarkus. Get started by scaffolding a new project using the Quarkus command-line tool (CLI):
$ quarkus create app prng-example --java=17
The output looks like this:
...
[SUCCESS] ✅ quarkus project has been successfully generated in:
--> /Users/danieloh/quarkus-demo/prng-example
...
Unlike traditional Java frameworks, Quarkus provides live coding features for developers to rebuild and deploy while the code changes. In the end, this capability accelerates inner loop development for Java developers. Run your Quarkus application using Dev mode:
$ cd prng-example
$ quarkus dev
The output looks like this:
...
INFO [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
INFO [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, resteasy, smallrye-context-propagation, vertx]
--
Tests paused
Press [r] to resume testing, [o] Toggle test output, [:] for the terminal, [h] for more options>
Java 17 enables developers to generate random integers within a specific range based on the Xoshiro256PlusPlus PRNG algorithm. Add the following code to the hello()
method in the src/main/java/org/acme
directory:
RandomGenerator randomGenerator = RandomGeneratorFactory.of("Xoshiro256PlusPlus").create(999);
for ( int i = 0; i < 10 ; i++) {
int result = randomGenerator.nextInt(11);
System.out.println(result);
}
Next, invoke the RESTful API (/hello
) to confirm that random integers are generated. Execute the following cURL command line in your local terminal or access the endpoint URL using a web browser:
$ curl localhost:8080/hello
Go back to the terminal where you’re running Quarkus Dev mode. There you’ll see the following ten random numbers:
4
6
9
5
7
6
5
0
6
10
Note: You don’t need to rebuild code and restart the Java runtime at all. You’ll also see the output Hello RESTEasy in the terminal where you ran the curl
command line.
Wrap up
This article shows how Quarkus allows developers to start a new application development based on OpenJDK 17. Furthermore, Quarkus increases developers’ productivity by live coding. For a production deployment, developers can make a native executable based on OpenJDK 17 and GraalVM.
Comments are closed.