Introduction 📝
This week’s collection includes approaches for leveraging Java interfaces creatively, multi-tenancy strategies in Spring Boot, and real-time communication with Quarkus WebSockets. Other topics include using lambda expressions in SQL, mocking JDBC for unit testing in Spring Boot, and building custom Spring Boot starters.
The articles also discuss monitoring JVM memory usage, thread synchronization in Java, optimizing garbage collection, and integrating WireMock for testing.
Top Picks ⭐
Monitor Non-Heap Memory Usage of a JVM
The Baeldung article delves into monitoring non-heap memory usage in Java applications, emphasizing the importance of understanding the JVM's memory structure, which includes both heap and non-heap areas.
Non-heap memory encompasses regions like Metaspace, Code Cache, Thread stacks, and Symbol tables, each serving specific purposes such as storing class metadata, JIT-compiled code, thread-specific data, and constant pool information, respectively.
To effectively monitor these areas, tools like
jcmd
can be utilized to gather insights into memory utilization, helping developers identify potential memory leaks or inefficiencies.
When SQL Meets Lambda Expressions
This article discusses how modern SQL dialects like ClickHouse, Databricks, and Snowflake have started integrating lambda expressions, especially for working with
ARRAY
types.It explains how jOOQ supports this feature by mapping Java (or Kotlin, Scala) lambdas to SQL lambda expressions, making it easier to apply functions like
ARRAY_FILTER
in SQL queries. It also covers workarounds for databases that do not support lambda expressions directly, such as using subqueries.
Multi-Tenancy in Spring Boot: Sharding vs. Schema Isolation
The article discusses multi-tenancy strategies in Spring Boot, comparing two models: Schema-Based Isolation and Database Sharding.
Schema-Based Isolation creates separate schemas for each tenant, offering strong data isolation. On the other hand, Database Sharding uses a single database with tenant-specific partitioning.
It provides implementation examples for both models, emphasizing the use of Hibernate Multi-Tenancy to switch schemas in Spring Boot applications dynamically.
Latest 🔥
Exploring Quarkus WebSockets
This article introduces Quarkus WebSockets, which enable real-time, full-duplex communication between clients and servers using a single TCP connection.
It explains the advantages of WebSockets for applications like chat, gaming, live notifications, and IoT.
Built on the Quarkus framework, Quarkus WebSockets offer efficient bidirectional communication and integrate well with reactive architectures, reducing latency and enhancing responsiveness.
The article also highlights Quarkus's benefits such as fast startup, low memory usage, and compatibility with cloud-native environments.
Integrating WireMock with Spring Boot
The Baeldung article explains how to integrate WireMock into a Spring Boot project to mock external APIs for testing purposes.
It covers setting up WireMock as a standalone server or as part of test cases, including defining stubs to simulate specific HTTP responses.
This approach allows developers to test application interactions with external services without relying on actual service availability, making tests more reliable and faster.
Unlocking the Power of Interfaces: Creative Approaches in Java
The article explores creative ways to leverage interfaces in Java to create modular, reusable, and maintainable code.
Key approaches include the Strategy Pattern for dynamic behavior changes at runtime, using callback interfaces for asynchronous tasks, and applying functional interfaces to streamline operations.
These techniques enhance system flexibility, allowing for better abstraction, decoupling, and scalability in Java applications.
Mocking JDBC for Unit Testing
The Baeldung article on mocking JDBC for unit testing explores strategies for testing Java applications that interact with databases.
It discusses two primary approaches: using an in-memory database like H2 to run tests without affecting the actual database, and mocking the
JdbcTemplate
using tools like Mockito to simulate database operations.The article also introduces Spring Boot’s
@JdbcTest
annotation, which sets up an embedded H2 database for testing purposes. These techniques allow developers to isolate database interactions during tests, ensuring the application logic is tested without needing a live database, leading to more reliable and maintainable code.
How Java Manages Thread Synchronization with Locks and Monitors
The article explains how Java manages thread synchronization using intrinsic locks and monitors. Every Java object has an inherent lock tied to its monitor, which controls access to synchronized code blocks.
When a thread enters a synchronized method, it acquires the object’s lock, blocking other threads until it is released. Monitors track thread ownership, waiting threads, and those paused by
wait()
.The article also covers tools like
ReentrantLock
and condition variables that provide more control over thread synchronization.
Building a Custom Spring Boot Starter for Shared Logic
The article explains how to create a custom Spring Boot starter to centralize shared logic, such as logging or database configurations, across multiple services.
It details how Spring Boot’s auto-configuration mechanism works, utilizing annotations like
@AutoConfiguration
and@ConditionalOnMissingBean
to automatically wire beans into the application.It also covers how to create customizable properties using
@ConfigurationProperties
and how to package the starter for reuse in other projects.The process includes structuring the project, using Maven for packaging, and publishing it for others to use.
Real-World Garbage Collection Scenarios and Solutions
The article discusses real-world scenarios where garbage collection (GC) tuning in Java significantly improved performance. It highlights key performance indicators like throughput, latency, and CPU time.
Case studies include an insurance application that improved response times by 15% through GC tuning, a robotics system that reduced pauses to seconds, and a cloud service provider that improved throughput from 96% to near 100%.
It emphasizes the importance of analyzing GC logs using tools like GCeasy to identify bottlenecks and optimize performance.