Quarkus

Language: Java

Web

Quarkus was created to optimize Java for containerized environments and serverless deployments. It leverages compile-time boot, GraalVM native image support, and reactive programming to provide a high-performance and developer-friendly framework for modern applications.

Quarkus is a Kubernetes-native Java framework designed for building cloud-native, reactive, and serverless applications with fast startup times and low memory footprint.

Installation

maven: Add io.quarkus:quarkus-bom and required extensions in pom.xml
gradle: Add implementation platform('io.quarkus:quarkus-bom:3.3.0.Final') and dependencies like implementation 'io.quarkus:quarkus-resteasy'

Usage

Quarkus allows building REST APIs, reactive services, microservices, and serverless applications. It provides seamless integration with Hibernate, JPA, messaging systems, and cloud-native features, with support for reactive and imperative programming models.

Creating a simple REST endpoint

import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello Quarkus!";
    }
}

Defines a basic REST endpoint that responds with 'Hello Quarkus!' on GET requests to `/hello`.

Starting a Quarkus application

import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.annotations.QuarkusMain;

@QuarkusMain
public class MainApp {
    public static void main(String... args) {
        Quarkus.run(args);
    }
}

Bootstraps a Quarkus application using the main method.

Dependency Injection

import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class MyService {
    public String getMessage() { return "Service Message"; }
}

@Path("/service")
public class ServiceResource {
    @Inject
    MyService myService;

    @GET
    public String message() {
        return myService.getMessage();
    }
}

Demonstrates injecting a service into a REST resource using Quarkus and Jakarta DI.

Reactive REST endpoint with Mutiny

import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;

@Path("/reactive")
public class ReactiveResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Uni<String> hello() {
        return Uni.createFrom().item("Hello Reactive Quarkus!");
    }
}

Defines a reactive REST endpoint using Mutiny for asynchronous, non-blocking responses.

Using Hibernate ORM with Panache

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;

@Entity
public class Person extends PanacheEntity {
    public String name;
    public int age;
}

Defines a simple entity with Quarkus Panache for ORM support with minimal boilerplate.

Configuration properties

import io.quarkus.arc.config.ConfigProperties;

@ConfigProperties(prefix="app")
public class AppConfig {
    public String name;
}

Maps configuration properties from application.properties or application.yml into a Java class.

Error Handling

Port already in use: Change the HTTP port in application.properties or ensure no other service is running on the same port.
Dependency injection failure: Check for missing beans, circular dependencies, or misconfigured @Inject annotations.
GraalVM native image build failure: Ensure all reflection and dynamic proxies are properly registered or use Quarkus native image extensions.

Best Practices

Use reactive programming with Mutiny for high-performance non-blocking services.

Leverage Panache for simpler and efficient ORM operations.

Externalize configuration and use type-safe config classes.

Organize code with REST resources, services, and entities for modularity.

Integrate Quarkus with CI/CD pipelines for containerized deployments.