Posts

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'

Colima - Drop In replacement for Docker Desktop for Mac and Linux

Colima ( Co ntainer on Li nux Ma chines) is an open source alternative and drop in replacement for Docker Desktop on Mac and Linux. https://github.com/abiosoft/colima It is simple to setup and run containers without the need for sudo or root access. Images can be pulled form both Docker hub ( https://hub.docker.com ) or Amazon's public registry ( https://gallery.ecr.aws ) Amazon's registry is more permissive or friendly with pull rates for both clients and non-clients of AWS. This article show the setup and basics of running containers on mac. Installation command for Linux can vary based on the distribution type, hence please refer to official documentation for up-to-date steps at  https://github.com/abiosoft/colima/blob/main/docs/INSTALL.md Installation on mac: brew install docker  brew install docker-buildx brew install docker-compose brew install jq brew install colima Setup docker compose as docker plugin mkdir -p $HOME/.docker/cli-plugins ln -sfn $(which docker-compose) $