Software Development

Debug Quarkus Apps Example

Quarkus is a modern Java framework that optimizes Java for cloud-native applications, microservices, and serverless environments. It aims to provide fast startup times and low memory usage, making it ideal for containerized environments like Docker and Kubernetes. Debugging Quarkus applications is crucial for efficient development and troubleshooting. Let us delve into understanding how to debug Quarkus apps and effectively troubleshoot issues in your Quarkus applications.

1. Debugging a Java Application

Debugging a Java application can be done using various tools. One of the most commonly used methods is through an Integrated Development Environment (IDE) like IntelliJ IDEA or Eclipse. IDEs provide a powerful set of debugging tools, including breakpoints, variable inspection, step execution, and stack trace analysis. Below, we will explain how to debug a simple Java application using IntelliJ IDEA.

1.1 Code Example – Simple Java Application

public class DebugExample {
  public static void main(String[] args) {
    int x = 10;
    int y = 20;
    int sum = add(x, y);
    System.out.println("Sum: " + sum);
  }

  public static int add(int a, int b) {
    return a + b;
  }
}

To debug this application in IntelliJ IDEA:

  • Set a breakpoint by clicking on the left margin of the line where you want to pause execution. For example, you can set a breakpoint on the line int sum = add(x, y);.
  • Click on the debug icon (a bug with a play button) or use the shortcut Shift + F9 to start debugging.
  • When the application hits the breakpoint, it will pause, and you can inspect variables in the Variables tab. You can also step through the code line by line using the step-over and step-into buttons.
  • Examine the value of the sum variable to ensure that the correct calculation occurs.

This basic debugging workflow helps to identify logic errors and ensure that the code functions as expected.

2. Quarkus Dev Mode

Quarkus provides a development mode called Dev Mode, which allows developers to experience hot-reloading, faster iterations, and efficient debugging. With Dev Mode enabled, changes to your source code are automatically detected, and the application is recompiled and restarted in the background, without needing a full restart.

2.1 How to Use Quarkus Dev Mode?

To enable Dev Mode in Quarkus, run the following Maven command in your project’s root directory:

mvn quarkus:dev

This starts the Quarkus application in development mode. By default, Dev Mode listens on http://localhost:8080 for HTTP requests and allows you to interact with the application in your browser.

2.2 Code Example – Debugging a REST Endpoint in Quarkus

Quarkus allows you to expose RESTful APIs with ease using the JAX-RS API. Below is an example of a simple REST endpoint that can be debugged using Dev Mode.

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/hello")
public class HelloWorldResource {
  @GET
  public String hello() {
    System.out.println("Inside hello method");
    return "Hello, Quarkus!";
  }
}

In the above example, a simple REST endpoint is defined at /hello. When you navigate to http://localhost:8080/hello, the endpoint is triggered, and the message “Inside hello method” will be printed to the console.

2.2.1 Using Breakpoints in Dev Mode

In Quarkus Dev Mode, you can use your IDE’s debugging tools to set breakpoints. For example, you can set a breakpoint inside the hello() method, and the application will pause at the breakpoint when you access the endpoint.

To start debugging, run the Quarkus application in Dev Mode and connect your IDE’s debugger to the application. You can use IntelliJ IDEA or Visual Studio Code with the necessary debugging extensions.

2.3 Remote Debugging in Quarkus

For more advanced debugging scenarios, Quarkus supports remote debugging. Remote debugging allows you to connect your IDE to a Quarkus application running on a remote server or container. You can enable remote debugging by adding the following JVM option to the application.properties file:

quarkus.debug=true

Additionally, you need to set the JVM debug port in your IDE or container configuration (default is 5005). When the application starts, it listens for remote debugger connections.

Once the debugger is connected, you can inspect variables, step through the code, and identify issues without stopping the application.

2.4 Logging in Quarkus

Logging is an essential part of debugging. In Quarkus, you can configure the logging level for different classes or packages. You can enable detailed logging by modifying the application.properties file:

quarkus.log.level=DEBUG

You can also use the following logging configurations to capture various log levels:

  • quarkus.log.level=INFO: Standard log output for general operations.
  • quarkus.log.level=ERROR: Captures only error messages.
  • quarkus.log.category.”com.example”=DEBUG: Configure specific categories for detailed logging.

3. Conclusion

Debugging is a fundamental part of the development lifecycle, and Quarkus provides several features that make it easy to debug applications effectively. By using Dev Mode, setting breakpoints, enabling logging, and utilizing remote debugging, developers can troubleshoot issues quickly and efficiently.

With these powerful tools, you can ensure that your Quarkus applications are running optimally and are easy to maintain. Debugging should no longer be a time-consuming or frustrating process, thanks to the robust support provided by Quarkus for modern Java development.

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David M
David M
1 month ago

Exceptionally unhelpful article.
1) Debugging Quarkus requires some careful setup: in intellij only one of the plugins works, in vscode it’s actually impossible to do live reload, and in both cases, care must be taken to kill spawned processes. (you need two process to debug Quarkus).
2) your article doesn’t mention Gradle.
3) livereload only works for container managed entities

Last edited 1 month ago by David M
Back to top button