Skip to main content

Posts

Singleton class in java

What is Singleton class Singleton is a design technique which gaurantees that there will be only instance of a class globally. Such classes are required when we need to create some objects which are memory/ resource extensive and we can't afford many such objects. Using singleton We can maintain single object per JVM per classloader. Classloader could be different in different hierarchy and in such case we may have more than one instances of singleton but we can avoid it by loading it at appropriate classloader. For example in an ear application there could be multiple web modules and each one of them will have their own singleton instance. Sometimes it may be a need but sometimes it could be a flaw which can be resolved by loading it either at ear level or web module level as per requirement. Implementing singleton class There are two different ways to implement singleton in java. Using singleton design pattern In this pattern we can create singleton either using lazy
Recent posts

Print English alphabets using for loop in Java

 In this post we will see how we can print the english alphabets [a-z] using for loop. One way is to have a character array of a-z and print it using loop. But there is another way to just print them using ascii code. What is ascii code There are total 256 numbers in ascii and each of which represents a specific character including alphabets. Numbers from 65-90 represents the alphabets in capital case [A-Z] and numbers from 97-122 represents alphabets in small case [a-z]. Printing alphabets using for loop with ascii numbers Below code prints alphabets in both capital and small cases using for loop. Here you can see we are using ascii numbers in for loop which are printed after type casted to character type where it automatically translate the number to equivalent character. public class PrintAlphabetsUsingLoop { public static void main(String[] args) { int alphabetsCount = 26; int capitalLetterStart = 65; int smallLetterStart = 97; System.out.println("Printing lette

Weblogic - JavaEE 6 migration to JavaEE 7

Weblogic migration to 12.2.x  There are significant changes done between JEE 6 and JEE 7. Weblogic 12.2.x uses JEE 7 and applications need to migrate the weblogic related configurations along with JEE API version. In JEE7 many ejb and weblogic related schemas has been changed which I have listed down here. Required schema declaration for all related configurations are as given below. weblogic-ejb-jar.xml <weblogic-ejb-jar xmlns="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.7/weblogic-ejb-jar.xsd"> ejb-jar.xml <ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd&quo

Adding/ Editing Health checks to deployments in OpenShift

What are health checks in OpenShift Health checks are required to ensure that application is resilient so application can be restarted without manual action. Such health checks can be configured using probes in openshift. Kubernetes provides two type of probes, readiness probe and liveness probe. Liveness probe Liveness probe checks that application is running and handles those situations when there is any issue with application. Readiness probe Readiness probe check whether application is ready to handle the traffic. Another benefit of having these health checks is that you can restart or deploy application with zero downtime when you are having minimum 2 pods running for your application. In this case it will not bring all the pods down. Supported health check types You can configure the health check of below type for both of the above probes. HTTP Health Check It will call a web URL and if it returns the HTTP status code between 200 and 399 then it will consider as

Allowing Cross domain POST request with JSON or XML content type in Java

Cross domain POST Request Cross domain POST requests supports only below content types and other content type are not allowed by default. application/x-www-form-urlencoded multipart/form-data text/plain So, if we try to make an ajax request to such POST URLs with XML or JSON content type, it will not be able to make request and give CORS error. Allowing Cross domain POST request with JSON or XML Content-Type We have our service implemented in Java and we will use Servlet Filter & request wrapper to build our solution. Please see my below POST to see how to implement CORS using java servlets which works well with GET methods or allowed content types with POST method. https://www.thetechnojournals.com/2020/03/cors-implementation-using-java-filter.html In this POST I will explain only additional things required for our problem with POST request, so I request you to go through the above link first. Request wrapper to change/ override the content-type to JSON or XML

Microservices with Spring Boot - complete tutorial

In this tutorial we are going to learn how to develop microservices using spring boot with examples. Main focus of this tutorial is on learning by doing hands-on. Before hands-on we will first understand what is microservices and related terminologies like DDD, 12-Factors App, Dev Ops. What is a Microservice In simple terms microservice is a piece of software which has a single responsibility and can be developed, tested & deployed independently. In microservices we focus on developing independent and single fully functioning modules. Opposite to microservice, with monolithic application it focuses on all the functionality or modules in a single application. So when any changes required to monolithic application it has to deploy and test the complete application while with microservice it has to develop and deploy only affected component which is a small service. It saves lot of development and deployment time in a large application. It's basically an architectural style

Microservices - Spring cloud API gateway along with registry server

Here we will learn, how to create API gateway using Spring cloud API. We will also register it with discovery/ registry server. Also will see how to access microservice using registry server through API gateway. What is API gateway As its name implies, API gateway is the single entry-point to an application or system. It can handle a request and invoke the appropriate service registered with it. It provides many features like protocol translation, filtering, security, interceptors etc. Using API gateway we can invoke any microservice whether its direct invocation or using some registry/ discovery server to invoke registered services using their ID. Creating API gateway In our example we will create an API gateway which will register itself with Eureka discovery server and will invoke the microservice available in discovery server using its registered service ID. API gateway can be implemented two ways, one is using Zuul proxy and second is using spring cloud API gateway. Differ