Posts

Java | Apache Commons StringUtils

The Apache Commons library is utility library provides many features which lacks in existing Java Core Framework. It is widely used open sourced library. In this post, we are going to learn utility methods provided by StringUtils class. Unlike Java's String class, Apache StringUtils class methods are null-safe. This means no NullPointerExeception is thrown if null value is sent. Below are popular methods used - Complete list of methods can be found at Apache commons StringUtils methods StringUtils.isEmpty() / StringUtils.isBlank() This methods used to check if a String contains any text. Both of these return true if the String is empty. The method isBlank() will return true if a String contains only whitespaces. StringUtils.isNotBlank() Checks if a string is not empty (""), not null and not whitespace only. StringUtils.equals() This case sensitive method compares two strings, returning true if they represent equal sequences of characters. Two null references are considere

Java | Debugging

Image
In the day-to-day life of a programmer, most of the time is spent in debugging. Writing a code can be easy, but fixing a bug can be a nightmare. The java applications can be debugged to check why application is not behaving as expected.  To debug a Spring boot application we have to start application in debug mode. The Spring boot application we created in last post can be debugged as follows -  Intellij Idea -  Once the application is started you can add debug points in code by double-clicking on desired line. As soon as we hit a request, it can be seen that the code execution stopped where debug is added. We check the variables in the right hand side of debug mode. This is useful to check what value is being set. It also has options to mute debug points if we want to continue with normal execution etc.  Most of the IDEs support the Evaluate function. To check some logic, we can right click on code and select Evaluate Expression option where we can add custom code to check values logi

Spring Boot | Sample REST Application

Image
In this post we are going to write a Simple REST Application in Spring Boot. Spring Boot has inbuilt tomcat which makes an application start without the need to have any application/web server. Though we can always deploy applications in web servers too. For this tutorial we will be using Maven for dependency management. Step 1. Add all required Spring dependencies in your pom file We can use below sample pom file to start with or go to https://start.spring.io/ to kick start your spring boot application. Step 2. Spring starter class Spring Boot uses @SpringBootApplication to auto configure some of spring configuration. You can find more info on  SpringBoot details Step 3. Application Controller We will be using MVC pattern to separate our restful layer from the service layer.   In our REST service we use  @RestController  annotation. It is combination of @Controller and @ResponseBody annotations. We created CustomerController to return customer details. Step 4. Application Servi

JavaScript | Promises

 JavaScript promises are object which used for holding value of Async operation. The value can be either success and failure.  You can refer below links to learn more about promises. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise https://javascript.info/promise-basics In earlier post  I'd mentioned about callbacks. Adding multiple callbacks in code make code more complicated and not readable. We can solve this issue with callbacks. The $.get used already returns promise object so we can directly call then() method of promise object to do processing. We returned the call to second rest so that its result will be available in chain format. This is useful where we have to call one rest one after another. The fail() at last of chain can be added to catch any errors. See the Pen Promise by Rohan ( @rohanlopes20 ) on CodePen .

JavaScript | Callbacks

JavaScript c allbacks can be used to process the result of async operation. Lets take a example below. As shown in below example the callback function is used to process the response received from rest service. See the Pen by Rohan ( @rohanlopes20 ) on CodePen . Things gets complicated when we have to call more than one request on after another like second request is depending upon response of first request. This leads to callback hell. I recommend to read more on  https://javascript.info/callbacks As you chain more and more calls it becomes complicated to maintained code. This can be avoided with JavaScript Promises. See the Pen by Rohan ( @rohanlopes20 ) on CodePen .

Java | Shutdown Hooks for Java Applications

When java application is stopped, its used services/processes needs to be shutdown gracefully. Java shutdown hooks provides way to gracefully stop the services/processes. We can write our own logic to do things when shutdown hooks are called.  Shutdown hooks are called when -  1. When the main thread exits, the JVM starts its shutdown process 2. Sending an interrupt signal from the OS. Eg. Ctrl + C or OS Shutdown 3. Calling System.exit() from Java code Shutdown hooks are basically initialized but unstarted threads. Shutdown hooks are only called on above mentioned conditions, it is not called in case of jvm shut down abruptly eg. power failure etc.

Java 8 | Writing custom Stream API using builder pattern

In this post we are going to create simple Java 8   Stream API using builder pattern. Check builder pattern post here .  API will provide below four methods - sort - Sort the list limit - Fetch only N numbers from the list distinct - Remove duplicates from the list forEach - Print the list