Quarkus is supersonic subatomic Java. Brief overview of the framework

Quarkus is supersonic subatomic Java. Brief overview of the framework

Introduction

March XNUMX, RedHat (soon - IBM) presented new framework - quarkus. According to the developers, this framework is based on GraalVM and OpenJDK HotSpot and is designed for Kubernetes. The Quarkus stack includes: JPA/Hibernate, JAX-RS/RESTEasy, Eclipse Vert.x, Netty, Apache Camel, Kafka, Prometheus and more.

The purpose of the creation is to make Java the leading platform for Kubernetes deployment and serverless application development, providing developers with a unified approach to development in both reactive and imperative styles.

If you look at this classification of frameworks, then Quarkus is somewhere between "Aggregators / Code Generators" and "High-level fullstack frameworks". This is already more than an aggregator, but it also falls short of full-stack, because. focused on backend development.

Promised a very high application startup speed and low memory consumption. Here is the data from the developer's site:

Time from start to first response (s):

Configuration
REST
REST+JPA

Quarkus + GraalVM
0.014
0.055

Quarkus+OpenJDK
0.75
2.5

Traditional Cloud Native Stack*
4.3
9.5

Memory consumption (Mb):

Configuration
REST
REST+JPA

Quarkus + GraalVM
13
35

Quarkus+OpenJDK
74
130

Traditional Cloud Native Stack*
140
218

Impressive, isn't it?

*I did not find information about this technology stack, we can assume that this is some kind of Spring Boot with an additional body kit.

Hello World!

The simplest application written in Quarkus would look like this:

@Path("/hello")
public class GreetingResource {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   public String hello() {
       return "hello";
   }
}

It's literally one class and it's enough! You can run the application with Maven in development mode:

mvn compile quarkus:dev
…
$ curl http://localhost:8080/hello
hello

The difference from the usual application is that there is no Application class! Quarkus supports hot reload, so you can change the application without restarting it, thereby making development even faster.

What's next? You can add a service to the controller using an annotation Inject. Service code:

@ApplicationScoped
public class GreetingService {

   public String greeting(String name) {
       return "Hello " + name + "!";
   }
}

Controller:

@Path("/hello")
public class GreetingResource {

   @Inject
   GreetingService service;

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/{name}")
   public String greeting(@PathParam("name") String name) {
       return service.greeting(name);
   }
}

$ curl http://localhost:8080/hello/developer
Hello developer!

Note that Quarkus uses standard annotations from familiar frameworks - CDI and JAX-RS. There is nothing new to learn if you have worked with CDI and JAX-RS before, of course.

Working with the database

Used by Hibernate and standard JPA entity annotations. As with REST controllers, you need to write a minimum of code. It is enough to specify dependencies in the assembly file, place annotations @Entity and configure datasource in application.properties.

All. No sessionFactory, persistence.xml and other service files. We write only the code that is needed. However, if necessary, you can create a persistence.xml file and fine-tune the ORM layer.

Quarkus supports caching of entities, collections for one-to-many relationships, and queries. At first glance, it looks great, but it local caching, for a single Kubernetes node. Those. caches of different nodes are not synchronized with each other. I hope this is temporary.

Asynchronous code execution

As mentioned above, Quarkus also supports a reactive programming style. The code of the previous application can be written in a different form.

@Path("/hello")
public class GreetingResource {

   @GET
   @Produces(MediaType.TEXT_PLAIN)
   @Path("/{name}")
   public CompletionStage<String> greeting(@PathParam("name") String name) {
       return CompletableFuture.supplyAsync(() -> {
           return "Hello " + name + "!";
       });
   }
}

Asynchronous code can also be moved to a service, the result will be the same.

The test is

Tests for Quarkus applications can be written in JUnit4 or JUnit5. Below is an example test for an endpoint, written with RestAssured, but other frameworks can be used:

@QuarkusTest
public class GreetingResourceTest {

   @Test
   public void testGreetingEndpoint() {
       String uuid = UUID.randomUUID().toString();
       given()
         .pathParam("name", uuid)
         .when().get("/hello/{name}")
         .then()
           .statusCode(200)
           .body(is("Hello " + uuid + "!"));
   }
}

The @QuarkusTest annotation instructs to run the application before running the tests. The rest is code familiar to all developers.

Platform-specific application

Since Quarkus is tightly integrated with GraalVM, it is of course possible to generate native code. To do this, you need to install GraalVM and specify the GRAALVM_HOME environment variable. Further prescribe a profile for assembly and specify it when building the application:

mvn package -Pnative

Interestingly, the generated application can be tested. And this is important, because the execution of native code may differ from the execution on the JVM. The @SubstrateTest annotation runs native application code. Reusing the existing test code can be done using inheritance, as a result, the code for testing a platform-specific application will look like this:

@SubstrateTest
public class GreetingResourceIT extends GreetingResourceTest {

}

The generated image can be packaged in Docker and run in Kubernetes or OpenShift, described in detail in instructions.

Π˜Π½ΡΡ‚Ρ€ΡƒΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ

The Quarkus framework can be used with Maven and Gradle. Maven is fully supported, unlike Gradle. Unfortunately, at the moment Gradle does not support generating an empty project, the site has a detailed ΡƒΡ‡Π΅Π±Π½ΠΈΠΊ.

Extensions

Quarkus is an extensible framework. At present, there are about 40 extensions, which add various functionality - from support Spring DI container ΠΈ Apache Camel to logging and publishing metrics for running services. And there is already an extension to support writing applications in the Kotlin language, in addition to Java.

Conclusion

In my opinion, Quarkus is quite in the trends of the time. Developing backend code is getting easier and easier, and this framework simplifies and speeds up service development even more by adding native support for Docker and Kubernetes. A huge plus is the built-in support for GraalVM and the generation of platform-specific images, which allows you to make services really fast starting and taking up little memory space. And this is very important in our time of mass enthusiasm for microservices and serverless architecture.

Official site - quarkus.io. Examples of projects for a quick start are already on GitHub.

Source: habr.com

Add a comment