Tag Archives: Java

Complete Guide to Enabling HTTPS on Apache Tomcat

When your web application moves beyond hobby status, the first hardening step is wrapping every byte in TLS. Tomcat makes the process painless once you understand where the moving parts live. In this guide you will create (or import) a certificate, wire it into Tomcat’s connector, and verify that the padlock appears in every browser.


Why HTTPS on Tomcat Matters

Plain HTTP exposes cookies, credentials, and payloads to anyone on the wire. Search engines penalize insecure sites, browsers now flag non-TLS pages as “Not Secure”, and compliance frameworks such as PCI-DSS simply forbid clear-text traffic. Turning on HTTPS:

  • Encrypts data in transit
  • Proves server identity to clients
  • Unlocks HTTP/2 and modern protocols
  • Keeps Google and your security team happy

Prerequisites and Environment

Before touching configuration files ensure:

  • Tomcat 9.x or 10.x is installed and starts cleanly on port 8080
  • JAVA_HOME points to JDK 8+ (keytool comes with the JDK)
  • OpenSSL 1.1+ if you prefer generating private keys externally
  • Server DNS name (e.g. app.ankurm.com) resolves to the VM or container
Continue reading Complete Guide to Enabling HTTPS on Apache Tomcat

Log4j2 Logging Levels – Complete Guide

Imagine your application’s log file as a constant stream of information. In a production crisis, this stream becomes a firehose. How do you find the single critical error message in a flood of routine status updates? The answer lies in logging levels.

These aren’t just labels; they are the fundamental control mechanism in Log4j2. They allow developers to filter noise, pinpoint failures, and monitor application health effectively. Mastering this hierarchy is the key to creating logs that are helpful, not overwhelming. This guide explores Log4j2’s levels, from configuration to real-world best practices.


The Logging Threshold: How Levels Work

Think of logging levels as a gatekeeper’s volume knob. Each log message you write (an “event”) is assigned a level of importance, or severity. The logger itself is then configured with a threshold level.

When a message arrives, the framework compares its severity to the logger’s threshold. Only messages at or above the configured threshold are processed and sent to their destination (like a file or the console).

This simple mechanism provides fine-grained control over your application’s verbosity. You can run the exact same code in different environments and get drastically different log outputs—all without changing a single line of Java. In development, you might set the threshold low to see everything. In production, you set it high to capture only significant errors.

Continue reading Log4j2 Logging Levels – Complete Guide

Understanding JUnit 5 @AfterEach Annotation with Practical Examples

In the world of Java development, writing a test is only half the battle. The real challenge lies in ensuring those tests are isolated, repeatable, and clean. If your first test leaves a database connection open, a locked file in the filesystem, or a messy entry in a shared cache, your second test might fail—not because of a bug in your production code, but because of a “dirty” environment. These “flaky tests” are the bane of modern CI/CD pipelines, leading to wasted developer time and decreased confidence in the build.

This is where the JUnit 5 @AfterEach annotation becomes your best friend. In this comprehensive guide, we will dive deep into how to use this lifecycle callback to manage resource cleanup, maintain test integrity, and ensure that every test runs in a pristine environment.


What is @AfterEach?

The @AfterEach annotation marks a method that should be executed after every individual @Test method in the current class. Think of it as the “janitor” of your testing suite; no matter if your test passes, fails, or throws an unexpected exception, the @AfterEach method steps in to sweep the floor and reset the stage for the next performer.

Unlike its counterpart @AfterAll (which runs once after the entire class is finished), @AfterEach is granular. It ensures that the state is reset immediately after each logic check, preventing “leakage” where the side effects of Test A interfere with the assertions of Test B.

Continue reading Understanding JUnit 5 @AfterEach Annotation with Practical Examples

Compact Strings in Java 9: How They Save Memory and Boost Performance

Starting with Java 9, the JVM quietly adopted a new internal representation for the java.lang.String class. The feature is called Compact Strings and, unless you went looking, you probably never noticed the change—yet it can shrink your application’s heap by 10-30% and speed up operations on text-heavy workloads.

This post explains what compact strings are, how this subtle change delivers massive performance wins, and how you can measure the benefit on your own code. Get ready to reclaim your RAM!


The Memory Drain Java 9 Tried to Solve

Prior to JDK 9, every character inside a String was stored as a 16-bit char, using the UTF-16 encoding.

The problem? For common languages like English, or text data formats like XML, JSON, log messages, and HTTP headers, most characters fall within the Latin-1 (ISO-8859-1) set, which perfectly fits into a single 8-bit byte.

This meant that for a vast majority of applications, half of every character array was filled with redundant zeroes. That silent waste translated directly to:

  • Larger Heap Footprint: Your application demands more RAM.
  • More GC Pressure: The Garbage Collector has to work harder and longer.
  • Lower CPU-Cache Locality: Data is scattered, slowing down processing.

While developers could manually pack bytes, it came at the cost of code clarity and API compatibility. Clearly, a JVM-level fix was needed to make string handling efficient by default.

Continue reading Compact Strings in Java 9: How They Save Memory and Boost Performance

Java Stream API: How to Get the Last Element

Java Streams provide elegant ways to process collections, but retrieving the last element isn’t as straightforward as calling a getLast() method. Since streams are designed for sequential processing without inherent indexing, developers need specific techniques to fetch the final element efficiently. This guide explores practical approaches to solve this common programming challenge.

Whether you’re processing large datasets or building concise functional pipelines, understanding these methods will help you write cleaner, more maintainable code.

The Core Challenge

Unlike List or Deque collections, Java Streams don’t maintain bidirectional iteration or direct index access. Once elements pass through the pipeline, they’re consumed. This design makes operations like stream().last() impossible without workarounds. However, several clever techniques leverage stream reduction and size information to achieve the desired result.

Approach 1: Using reduce() Method

The reduce() operation processes each element while retaining only the last one seen. This approach works beautifully for both sequential and parallel streams.

import java.util.Optional;
import java.util.stream.Stream;

public class LastElementWithReduce {
    public static void main(String[] args) {
        Stream fruitStream = Stream.of("apple", "banana", "cherry", "date");
        
        Optional lastElement = fruitStream.reduce((first, second) -> second);
        
        lastElement.ifPresent(element -> 
            System.out.println("Last fruit: " + element)
        );
    }
}
Continue reading Java Stream API: How to Get the Last Element

Get Year, Month, Day from Date in Java

Extracting individual date components like year, month, and day is a common requirement in Java applications. Whether you’re working with legacy Date objects or modern date-time APIs, this guide will show you multiple approaches to accomplish this task efficiently.


1. Using Calendar Class (Legacy Approach)

Before Java 8, the Calendar class was the standard way to extract date fields from a Date object. While it’s still supported, this approach is considered outdated and less intuitive than modern alternatives.

import java.util.Calendar;
import java.util.Date;

public class DateExtractor {
    public static void main(String[] args) {
        Date currentDate = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1; // Months are 0-based
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        
        System.out.println("Year: " + year);
        System.out.println("Month: " + month);
        System.out.println("Day: " + day);
    }
}

Note: The Calendar.MONTH field returns values from 0 (January) to 11 (December), so you need to add 1 to get the standard 1-12 month numbering.

Continue reading Get Year, Month, Day from Date in Java

Building Native Images of Spring Boot Applications with GraalVM: A Step-by-Step Guide

In the ever-evolving landscape of cloud-native development and serverless architectures, the demand for applications with lightning-fast startup times and minimal memory footprints is greater than ever. Java, traditionally known for its “write once, run anywhere” philosophy, has sometimes faced criticism regarding these very aspects. However, with the advent of GraalVM and Spring Boot’s dedicated support, Java is now a formidable contender in this space.

This post will guide you through the process of building GraalVM native images of your Spring Boot native applications (specifically Spring Boot 3.x), demonstrating how to unlock significant optimizations in startup time and memory consumption, perfect for serverless Java and general cloud native Java deployments.

Why Native Images? The GraalVM Advantage

Traditional Java applications run on the Java Virtual Machine (JVM). While the JVM offers incredible runtime optimizations through its Just-In-Time (JIT) compiler, there’s an inherent overhead: the JVM itself needs to start, classes need to be loaded, and code needs to be JIT-compiled at runtime.

GraalVM Native Image technology compiles your Java application ahead-of-time (AOT) into a standalone executable. This executable includes the application code, required libraries, and a minimal runtime environment (the Substrate VM) – all compiled into a single binary.

The benefits are substantial:

  • Blazing Fast Startup: Native images typically start in milliseconds, making them ideal for serverless functions, microservices, and environments where rapid scaling is crucial.
  • Reduced Memory Footprint: By eliminating the JVM overhead and only including the necessary code paths, native images use significantly less memory.
  • Smaller Deployment Size: The resulting binary is often much smaller than a traditional JAR file bundled with a JRE.
  • Lower Resource Consumption: Less CPU and memory usage translates to lower operational costs in cloud environments.

Spring Boot 3.x has embraced GraalVM native image compilation with first-class support, making the process more seamless than ever.

Continue reading Building Native Images of Spring Boot Applications with GraalVM: A Step-by-Step Guide