Posts

Showing posts from 2024

Java's ScheduledExecutorService example

Example of Java's ScheduledExecutorService to run a block of code once (with a delay) or repeatedly at fixed rate or fixed delay. ScheduledExecutorService can be a single threaded executor (used to execute in a coordinated way) or can be multi threaded executor (used to execute in parallel) import java.util.concurrent.Executors ; import java.util.concurrent.ScheduledExecutorService ; import java.util.concurrent.TimeUnit ; public class ExampleScheduledExecutor implements AutoCloseable { private final ScheduledExecutorService executorService = Executors . newSingleThreadScheduledExecutor (); // use newScheduledThreadPool() for running with multiple threads in parallel // private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); public ExampleScheduledExecutor ( long initialDelay , long fixedDelay ) { executorService .schedule(() -> executeOnlyOnce(), initialDelay , TimeUnit . SECONDS ); executorService .scheduleAtFixedR...

less - Unix / Linux command line reader cheat sheet

Useful gist that contains the common commands used in less command line reader  https://gist.github.com/harishkannarao/2bbda02dcca774c93eb3c2a0bce9de7f

vi - Unix / Linux command line editor cheat sheat

Useful gist file that contains the common commands used in vi command line editor https://gist.github.com/harishkannarao/f897f1dd60d5f3e894f93a640c0ca9c5

Jetbrains IDE - IntelliJ Customisation for Developer Productivity

Exporting current settings of any Jetbrains IDE (IntelliJ / PyCharm): File -> Manage IDE Settings -> Export Settings -> Choose location and file name as a zip file Importing settings from zip file into any Jetbrains IDE (IntelliJ / PyCharm): File -> Manage IDE Settings -> Import Settings -> Choose location and zip file The following changes to any Jetbrains IDE (IntelliJ / PyCharm) makes developers more productive. Appearance Changes: View Menu -> Appearance -> Status Bar -> Tick View Menu -> Appearance -> Status Bar Widgets -> Git Branch -> Tick View Menu -> Appearance -> Status Bar Widgets -> Line:Column Number -> Tick Settings Changes: Settings -> Appearance & Behaviour -> Appearance -> Zoom -> 90 or 100 or 110 based on Screen Resolution Settings -> Appearance & Behaviour -> Appearance -> Use custom font -> Size 16 Settings -> Appearance & Behaviour -> Appearance -> UI Options -> Show ...

sdkman - Multiple versions of Java JDK on Mac

Install sdkman curl -s "https://get.sdkman.io" | bash Verify Installation Open a new terminal and execute sdk version List candidates sdk list List available version for a candidate sdk list java Install Java LTS versions sdk install java 21.0.4-jbr sdk install java 17.0.12-jbr sdk install java 11.0.14.1-jbr Set default Java version sdk default java 21.0.4-jbr Create alias in $HOME/.zprofile to switch jdk echo "alias jdk_21_use='sdk default java 21.0.4-jbr'" >> $HOME/.zprofile echo "alias jdk_17_use='sdk default java 17.0.12-jbr'" >> $HOME/.zprofile echo "alias jdk_11_use='sdk default java 11.0.14.1-jbr'" >> $HOME/.zprofile List current default version of a candidate sdk current java List currently installed versions of a java sdk list java | grep 'installed' Install Maven sdk install maven List currently installed version of maven or other candidates sdk list maven | grep '*'

Spring Boot - Debug Webflux reactive application in IDE

With Spring Boot Webflux reactive application, to debug and identify the unexpected exception thrown from a handler while handling http request, put a debug / break point in the following method of the class. This will quickly reveal the exception along with stacktrace with your favourite IDE Class: org.springframework.web.reactive.DispatcherHandler Method: handleRequestWith Have a breakpoint in the line after Mono<HandlerResult> resultMono = adapter.handle(exchange, handler)  and evaluate as  resultMono.block() Happy Debugging !!!

Spring Boot - Debug Web MVC application in IDE

With Spring Boot Web MVC application, to debug and identify the unexpected exception thrown from a controller while handling http request, put a debug / break point in the following method of the class. This will quickly reveal the exception along with stacktrace with your favourite IDE Class: org.springframework.web.servlet.DispatcherServlet Method: doService Have a breakpoint in the finally block and evaluate as request.getAttribute("org.springframework.boot.web.servlet.error.DefaultErrorAttributes.ERROR") Happy Debugging !!!

Spring Boot - Log generated SQL statement and query

To log and see the generated SQL statement and queries in Spring Boot application use the following properties in properties or yaml file. The properties allows to see the SQL from JPA (hibernate), JdbcTemplate (Core JDBC) and Liquibase layers of Spring Boot application. These settings adjust the logging level in logback for various layers in spring boot application. Using application.properties file logging.level.org.hibernate=TRACE logging.level.org.springframework.jdbc=TRACE logging.level.liquibase=TRACE Or using application.yml file logging : level : org : hibernate : TRACE springframework : jdbc : TRACE liquibase : TRACE

Process Java Collection in batches

The following snippets shows how to process a given Java Collection (List / Set) in batches and process each batch in sequence or parallel. Split a given collection into batches int batchSize = 2 ; List < String > input = List .of( "apple" , "banana" , "orange" , "mango" , "peach" ); List < List < String >> batches = IntStream .range( 0 , input . size ()) . boxed () . collect ( Collectors .groupingBy( index -> index / batchSize )) . values () . stream () . map ( indices -> indices . stream (). map ( input :: get ). toList ()) . toList (); batches . forEach ( values -> System . out . println ( "values = " + values )); Parallel process collection in batches using Java's ForkJoin.commonPool() int batchSize = 2 ; List < String > input = List .of( "apple" , "banana" , "orange" , "mango" , "peach" ); List < List ...

Java's CompletableFuture reference

 Since the addition of CompletableFuture in Java 8, running background task or parallel processing or multi-threading got really simplified. These are some of the examples on how to use CompletableFuture in standalone java or with Spring Boot to perform background tasks. Complete list of methods available in CompletableFuture can be found here at https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/CompletableFuture.html Basic example to call a method in background and return result The task gets executed in Java's default ForkJoin.commonPool() CompletableFuture < String > completableFuture = CompletableFuture .supplyAsync(() -> "Hello" ); String result = completableFuture . join (); System . out . println ( "result = " + result ); Basic example to call a method in background and without returning a result The task gets executed in Java's default ForkJoin.commonPool() CompletableFuture < Void > completableFuture ...

Verify CORS settings using curl

The  https://www.example.com  has to be the actual host name of the website The  http://localhost:8080/my-api  should be the API URL that serves the data The value of  Access-Control-Request-Method  can be GET or PUT or POST or DELETE based on the http methods supported by the API URL curl -I -X OPTIONS -H "Origin: https://www.example.com" -H 'Access-Control-Request-Method: GET' "http://localhost:8080/my-api" 2>&1 | grep -i 'Access-Control-Allow-Origin'