Author: Vijay SRJ
-
Java 21 – Scoped Values
How do you pass a value from one part of your code to another. Let’s say from one class to another. By using method arguments. Is there any other way that you can access a shared variable? For example, Let’s say you need to pass the user name who logged into your web app from…
-
How to connect to database using ConnectionDetails interface in Spring Boot?
Let’s say you want to connect to a database in your Spring Boot application. You first add Spring Data dependency. Then to configure the database details , you had two options: Starting Spring Boot 3.1.1 you have one more option. Using ConnectionDetails interface. Let’s see how to do that with an example. STEP 1: Add…
-
Java 21 – Structured Concurrency
Let’s say you want to perform a group of tasks in Java. And all these tasks are part of a single unit of work. And all these tasks can be executed independently. How can this be done in Java? You can execute each task sequentially. So you execute task 1 , then task 2 and…
-
Java 21 – Virtual Threads
Java supports concurrency. You can perform multiple tasks at the same time in a program. And how does Java implement this? Using Threads! A thread is the basic unit of concurrency in Java. If you want to perform multiple tasks in parallel you create one thread for each task and perform them in parallel. And…
-
Java 21 – Sequenced Collections
How do you get the last element of a list in Java? Using a code similar to below: That doesn’t look elegant! And how do you get the last element for a Sorted Set? That looks much more elegant but then different collections are having different ways to get the last element. There is no…
-
Java 21 – String templates
Contents: The Problem “String” is a basic and widely used data type in any programming language. Most of the data we deal with are texts which are represented by String datatype. One of the operations we often do on text is replacing part of the text with dynamic values . This is called interpolation. Languages…
-
Java 21 Features – Unnamed classes and instance main methods
Java 21 allows you to print Hello World message without creating a class and using public static keyword for main class.
-
Java 21 – Unnamed Patterns and Variables
Let’s say you have declared a variable in your Java program. But you are not going to use it for some reason. May be you declared an exception variable in a catch block but not going to use the variable at all: Or in a for loop statement: Or in Pattern Matching of Records (introduced…
-
How to implement Circuit Breaker Design Pattern in Spring Boot?
Table of Contents: Problem: Fault tolerance is a major requirement for enterprise applications. If your application gets huge load , how does it behave? If something wrong happens within your application and you still want to handle it gracefully , what do you do? Fault tolerance is especially important for microservices since many microservices communicate…
-
How to implement SAGA design pattern in Spring Boot?
Contents: Problem: Microservices come with their own advantages and disadvantages. One such disadvantage is managing distributed transactions. Let’s say your transaction spans 4 different microservices. How do you ensure that your transaction either commits successfully with all the 4 tasks succeeding or fails successfully if any of the tasks is not completed ( the completed…