Sunday, January 30, 2022

My Work Desk using CSS.

 Hello!, I just tried to create my work desk using CSS and HTML. Here all I used is<div>. 


CSS : 

body{
    background-color: white;
}

.mydesk{
    position: absolute;
    height: 10px;
    width: 500px;
    background-color: burlywood;
    top: 425px;
    left: 313px;
    box-shadow: 20px 20px 20px 20px;
}

.left-stand{
    position: absolute;
    height: 150px;
    width: 5px;
    left: 350px;
    top: 465px;
    background-color: black;

}
.right-stand{
    position: absolute;
    height: 150px;
    width: 5px;
    left: 750px;
    top: 466px;
    background-color: black;
}
.desktop {
    position: absolute;
    width: 200px;
    height: 150px;
    border-radius: 10px 10px 10px 10px;
    background-color: black;
    top: 44%;
    left: 72%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 20px -7px;
}

.monitor {
    border:double;
    background-color: darkgray;
    height: 130px;
    width: 180px;
    margin-left: 6px;
    margin-top: 5px;
}

.power{
    height: 5px;
    width: 5px;
    border-radius: 5px;
    background-color: white;
    margin-top: 1px;
    margin-left: 190px;
}

.power-on{
    height: 3px;
    width: 3px;
    background-color: white;
    margin-top: 0px;
    margin-left: 189px;
}

.stand{
    height: 25px;
    width: 30px;
    background-color: black;
    left: 89px;
    top: 152px;
    position: absolute;
    box-shadow: 0 0 5px 0px;
}

.flat{
    position: absolute;
    width: 80px;
    height: 12px;
    background-color: black;
    left: 65px;
    top: 207px;
    box-shadow: 0 0 5px 3px;
}
.monitor-power-supply{
    border-left: solid black;
    height: 65px;
    margin-left: 150px;
}

.laptop-outer{
    position: absolute;
    width: 150px;
    height: 100px;
    border-radius: 5px;
    background-color: black;
    top: 57%;
    left: 40%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 14px -2px;
}

.lap-monitor {
    border: double;
    background-color: darkgray;
    height: 65px;
    width: 135px;
    margin-left: 5px;
    margin-top: 10px;
}

.lenovo{
   
    background-color: grey;
    width: 36px;
    height: 6px;
    margin-top: 7px;
    margin-left: 5px;
}
.lenovo-font {
    font-family: pf lindemann sans;
    font-size: 15px;
}
.key-pad{
    width: 180px;
    height: 10px;
    box-shadow: 0px 0px 20px 0px;
    margin-top: 10px;
    margin-left: -12px;
    background-color: black;
}

.bose{
    width: 45px;
    padding: 8px;
    border: 5px solid gray;
    border-bottom-left-radius: 30%;
    border-bottom-right-radius: 30%;
    border-top-left-radius: 30%;
    border-top-right-radius: 30%;
    box-shadow: 0px 0px 40px -2px;
    background-color: gray;
    margin-left: 67px;
    margin-top: 31px;
}
.lamp{
    position: absolute;
    width: 100px;
    height: 20px;
    top: 394px;
    left: 515px;
    border-radius: 20px 20px 20px 20px;
    background-color: black;
}
.lamp-vetical-pole-1{
    top: -101px;
    left: 45px;
    position: absolute;
    height: 100px;
    width: 10px;
    background-color: black;
}
.lamp-vetical-pole-2{
    top: -148px;
    left: 47px;
    position: absolute;
    height: 46px;
    width: 5px;
    background-color: black;
}
.lamp-circle{
    position: absolute;
    width: 80px;
    height: 80px;
    border-radius: 50px;
    background-color: black;
    top: -229px;
    left: 10px;
}
.lamp-circle-inner{
    position: absolute;
    width: 40px;
    height: 40px;
    border-radius: 20px;
    background-color: white;
    left: 20px;
    top: 20px;
}

HTML: 

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="./mydesc.css">
</head>

<body>
    <div class="desktop">
        <div class="monitor"></div>
        <div class="power">
            <div class="power-on"></div>
        </div>
        <div class="stand"></div>
        <div class="flat"></div>
        <!-- <div class="monitor-power-supply">
           
        </div> -->
        <div class="bose">
           
        </div>
    </div>
    <div class="laptop-outer">
        <div class="lap-monitor"></div>
        <div class="lenovo">
           
        </div>
        <div class="key-pad"></div>
    </div>
    <div class="curves-width"></div>
    <div class="lamp">
        <div class="lamp-vetical-pole-1"></div>
        <div class="lamp-vetical-pole-2"></div>
        <div class="lamp-circle">
            <div class="lamp-circle-inner"></div>
        </div>
    </div>
    <div class="mydesk"></div>
    <div class="left-stand"></div>
    <div class="right-stand"></div>
</body>
</html>

OUTPUT:




Friday, January 28, 2022

Error creating bean with name 'conversionServicePostProcessor' defined in class path resource

Today I changed my .m2 repository from external repo to internal repo. After downloading all the necessary jars, I did mvn clean install and both got success. Then i tried to start my inbuild tomcat server for my spring boot application, after few seconds I got this error on console and server shutdown.


Error creating bean with name 'conversionServicePostProcessor' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.beans.factory.config.BeanFactoryPostProcessor]: Factory method 'conversionServicePostProcessor' threw exception; nested


The same code working other for my team, so I just googled the error and find some suggestion,   

spring.main.allow-bean-definition-overriding=true

But this same already available in my application.properties.


spring.main.allow-bean-definition-overriding=true 
So I have no clue to solve this issue, I tried many suggestion nothing works for me. Then I remember one, what if I try to  
Reimport All Maven Projects in my IDE, I am using IntelliJ. If you are using Eclipse just refresh the dependencies.


After all the jar loading done, I retried to start the server. Finally it works for me.
If you have some problem initializing spring boot bean after successful mvn install, just try reimport your jars on your IDE.
Because sometime your IDE need manual call to get refresh the dependencies.


Thursday, January 27, 2022

Spring Boot Actuator

 In Spring Boot, Actuator allows you to monitor your application, by just adding a dependency in you pom.xml. By adding this dependency we can able to track health check of the application. This will help on production environment, to check the application is running or not?. 

Let see how to implement this,

Create a spring boot application using Spring initializr . To test actuator we need to select spring-boot-starter-actuator along with spring-boot-starter-web(without this we cannot start the server).

Here my project specification, Using java 8, Maven Project .



Finally we can download the project. And the downloaded project in your IDE.

To Start application we need to do mvn clean install. Once that done we are good to start the server.

While starting the server we will see following log that confirm that we are enabling the actuator.




Here are three default actuator to check and monitor the application.(health, info and actuator). Here my server started on port 8081. Let me hit my server using http://localhost:8081/actuator. See the below JSON format available monitoring methods. Remember we need to call get method not post.


Is there only 3method to check the monitor, No. Then where are others??. Do we need to add any code to get all of them?? . Yes we need to mention in application.properties to say expose all the monitoring methods.

Add below code in your application.properties to expose all the monitoring method.

management.endpoints.web.exposure.include=*

Here let check server log after exposing all the monitoring methods,



We can see 14 endpoints exposed. Here output is like below,
I captured part of the output.


This will enable expose all the method available in spring boot monitoring.
Or we can add specific property by using below,

management.endpoint.health.enabled=true
management.endpoint.loggers.enabled=true

And let see what we can see by calling endpoint actuator/health



by calling http://localhost:8081/actuator/beans we can see what are the bean
initialized on server start of the this application.


Adding Logo on Coffee Cup

 Here I created a coffee cup using css Coffee cup . Added new style to display logo in the cup. 

I am using my previous employer logo. The same cup the given me on my first day of joining. 


Here  css changes

body{
    background-color: tomato;
}

.cup {
    position: fixed;
    height: 110px;
    width: 90px;
    background-color: whitesmoke;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 0 0 20px 20px;
  }

  .cup-bottom {
    width: 90px;
    border-radius: 0 0 45px 45px;
  }

  .cup-hand {
    position: absolute;
    height: 58px;
    width: 32px;
    margin-left: 89px;
    margin-top: -40px;
    background-color: whitesmoke;
    border-top-left-radius: 2px 20px;
    border-top-right-radius: 24px;
    border-bottom-left-radius: 2px 20px;
    border-bottom-right-radius: 24px;
  }
 
  .cup-hand-inside {
    position: absolute;
    height: 35px;
    width: 22px;
    margin-left: 1px;
    margin-top: 11px;
    background-color: tomato;
    border-radius: 0px 25px 25px 0px;
}

.cnsi{
    color: blue;
    font-style: normal;
    font-size: 20px;
    margin-left: 35px;
    margin-top: 36px;
}

.cnsi-logo{
    position: absolute;
    height: 25px;
    width: 25px;
    background-color: blue;
    border-radius: 50%;
    top: 35px;
    left: 4px;
}
.cnsi-logo-ship{
    position: absolute;
    left: 6px;
    top: 5px;
    height: 21px;
    width: 2px;
    background-color: white;
    transform: rotate(
330deg
);
}
.cnsi-logo-horns1{
    position: absolute;
    height: 0px;
    width: 0px;
    left: 2px;
    top: -2px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
}
.cnsi-logo-horns2{
    position: absolute;
    height: 0px;
    width: 0px;
    left: 2px;
    top: 4px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
}
.cnsi-logo-horns3{
    position: absolute;
    height: 0px;
    width: 0px;
    left: 2px;
    top: 9px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
}
.cnsi-logo-horns4{
    position: absolute;
    height: 0px;
    width: 0px;
    left: 2px;
    top: 15px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
}

HTML changes:

<div class="cup">
    <div class="cnsi-logo">
        <div class="cnsi-logo-ship">
            <div class="cnsi-logo-horns1"></div>
            <div class="cnsi-logo-horns2"></div>
            <div class="cnsi-logo-horns3"></div>
            <div class="cnsi-logo-horns4"></div>
        </div>
    </div>
    <div class="cnsi">CNSI</div>
    <div class="cup-hand">
        <div class="cup-hand-inside"></div>
    </div>
   
</div>

Output: 

Need to add more style to look like exact coffee cup with their logo. Working on it....!

The above CSS works fine, but review it back it seems we are using bilateral code on cnsi-logo-horns1 to cnsi-logo-horns4. Lets fix the CSS code with good standard.

.cnsi-logo-horns1{
    position: absolute;
    height: 0px;
    width: 0px;
    left: 2px;
    top: -2px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
 
}

if u see above and rest of cnsi-logo-horns* class, rest of top and left element are same for all these 4 selectors. Let make some changes without effecting style. 

Here we split the differing element from classes and make them as different selector,

.cnsi-logo-horn{
    position: absolute;
    height: 0px;
    width: 0px;
    border-top: 1px solid transparent;
    border-left: 15px solid white;
    border-bottom: 5px solid transparent;
    transform: rotate(
350deg
 );
 
}
.horn1{
    left: 2px;
    top: -2px;
}
.horn2{
    left: 2px;
    top: 4px;
}
.horn3{
    left: 2px;
    top: 9px;
}
.horn4{
    left: 2px;
    top: 15px;
}

And HTML changes for that,
            <div class="cnsi-logo-horn horn1"></div>
            <div class="cnsi-logo-horn horn2"></div>
            <div class="cnsi-logo-horn horn3"></div>
            <div class="cnsi-logo-horn horn4"></div>
Output here as same as before and code also looks good.
 

Tuesday, January 25, 2022

My First CSS work!!

CSS:

body{
    background-color: tomato;
}

.cup {
    position: fixed;
    height: 90px;
    width: 70px;
    background-color: whitesmoke;
    border-radius: 0px 0px 24px 24px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

  }

  .cup-hand {
    position: absolute;
    height: 58px;
    width: 33px;
    margin-left: 68px;
    margin-top: 15px;
    background-color: whitesmoke;
    border-top-left-radius: 2px 20px;
    border-top-right-radius: 24px;
    border-bottom-left-radius: 2px 20px;
    border-bottom-right-radius: 24px;
  }
 
  .cup-hand-inside {
    position: absolute;
    height: 35px;
    width: 22px;
    margin-left: 1px;
    margin-top: 11px;
    background-color: tomato;
    border-radius: 0px 25px 25px 0px;
}

HTML:

<div class="cup">
    <div class="cup-hand">
        <div class="cup-hand-inside"></div>
    </div>
</div>

And My Coffee Cup :

Hope u can see a cup!!. Will be adding more style on the cup further. 

Here I added more style to display logo on coffee cup.

Java Long variable not equals on == check , why?

 Today we faced a issue in dev environment, the same code works perfectly in local. We have no clue what happens in prod, we thought of data is problem in dev environment. While comparing data between environment we find the empId (Long Type is problem), which mean the type is not a problem, the way compare the Long variable makes the problem in different environment.

Here code snippet of problematic code. 

Boolean isEmployee = employee.getEmpId() == emp.getEmpId();

Since in local we have limited data might be around 50 data max, so empId is might max 50.

Let see what happen in dev environment.

    Dev environment we have lot of data for this particular table. approximately 10000+ records.

Is count of employee table makes that problem??. Partially Yes. How??
Let me explain what is the problem with above code??
In java Long variable will have [-128 to 127] range. If the value is greater than -128 and
less than 127 above condition will return true. else false. why??


Let see how internally Long works, literal values auto boxing using Long.valueOf(String),
this will works on Long [-128 to 127] range and it will find the value from LongCache.
If number greater than 127, then new Long object will create, so while using "==" return false.


What is the fix for our problem??

    We need to use .equals instead of ==. Fix is employee.getEmpId().equals(emp.getEmpId()) or employee.getEmpId().longValue() == emp.getEmpId().longValue().


Here is two example of < 127 and > 127.

public static void main(String[] args) {
Employee employee = new Employee("B", 1, 128L);
Employee emp = new Employee("B", 1, 128L);
Boolean isEmployee = employee.getEmpId() == emp.getEmpId();
if(isEmployee){
//Logic here.
System.out.println("Same Employee");
}else{
System.out.println("Different Employee");
}
System.out.println(employee.getEmpId() == emp.getEmpId());
System.out.println(employee.getEmpId().equals(emp.getEmpId()));
}

Output:

Same Employee
true// two variable pointing to same address
true


If we change empId to 128 and the output below is,
Different Employee
false //False because new object created and reference pointing two different object.
true

Integer also works same way. Here the link for Integer on == 

Tuesday, January 11, 2022

Print nth Fibonacci using java recursion.

 Hey there, 

    We know the fibonacci start with 0, 1, then addition of previous two number. Here next numbers are 1(0+1), 2(1+1), 3(1+2), 5(2+3), 8(3+5), 13(5+8) like wise. 

Here i will post the recursion program for find the Nth fibonacci number. Recursion is calling a method from that method, simple calling itself. 

For Example : 


private static void sayHello(){
System.out.println("Hello !");
return sayHello();
}

If you execute this method we will end up with stackoverflow exception. The exception will
be thrown after jvm stack overloaded,

......... here is lot of Hello ! printed.
Hello !
Hello !
Hello !
Hello !
Exception in thread "main" java.lang.StackOverflowError
	at sun.nio.cs.UTF_8$Encoder.encodeLoop(UTF_8.java:691)
	at java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579)

Because in this method we don't have any base condition to return this recursion stack trace.
This is very important when you go for recursion we need to have base condition at
first in you implementation.


private static void sayHello(int n){
if(n<1){
return ;
}
System.out.println("Hello !");
sayHello(n-1);
}

Here if condition check n is less than 1, this is our base condition. So now you will get
some idea about how recursion works in java. Let we get in our fibonacci program,


private static long printFibonacci(int n){
if(n<=1){
return n;
}
return printFibonacci(n-1) + printFibonacci(n-2);
}
If user input n is 5 then this program will print 5(0,1,1,2,3,5) the number is start with 0
so we will get 5.
Explanation for printFibonacci() is loading...

Monday, January 3, 2022

Multithreading in Java

Multithreading provider ability to execute multiple different path of code in same time

Normally java run into only one thread, but we have the ability to break off into multiple thread and do multiple things at once.


There are two main way to create thread.

1. Class extends Thread class, we need to override the run method from thread class, which is extends from Runnable interface.

 public class MyThread extends Thread {


@Override
public void run(){
for(int i = 0; i < 5; i++){
System.out.println("Count : " + i + " Thread name : " + Thread.currentThread());
}
}
}

public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}

Here the test file to run the thread and we are printing currently running thread. Below console log,




2. Implements Runnable interface, override run method from Runnable interface. While using implement we need to create Thread instance explicitly to call start() or run().

public class MyRunnable implements Runnable {
@Override
public void run() {
for(int i = 0; i < 5; i++){
System.out.println("Thread is running with: " + i + " " + Thread.currentThread());
}
}
}
public class ThreadTester {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
Above tester class we are instantiate thread object with name of MyRunnable class. 
And console log is below

If you notice we are override run() method from Runnable interface, but we called start()
from Thread class.
What will happen if we call run() instead of start(). Let me explain about start() and run().

Start():
    Calling start() will create a new thread from existing thread then run() will be execute, 
and its enables multithreading.
From above two coding example, we are seeing that Tread-0 is created from main thread.
[Thread-0,5,main]

run(): Calling run() will not create new thread, it execute as normal method with existing 
thread, no multithreading will be enabled.
let see example of calling run()
 
public class ThreadTester {
    public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.run();
}
}
Let see from output, 

From above console the run method is called as a method in main thread [main,5,main].

Now let see how the multiple thread running in java, here I will create two object of 
MyThread class and call start() of created instance.
public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyThread myThread1 = new MyThread();
myThread.start();
myThread1.start();
}
}
From below console, we are able to see the both thread executed 5times and creating new thread,
 but execution order is not maintained in jvm.
order of execution is changed from highlighted log line.

We can ensure that currently running thread to complete its action until we can hold the
execution of other thread by calling join()
public class ThreadTester {
public static void main(String[] args) {
MyThread myThread = new MyThread();
MyThread myThread1 = new MyThread();
myThread.start();
try {
myThread.join(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
myThread1.start();
}
}
Here I called myThread.join(mills), this will ensure that myThread is complete this action 
and allows myThread1.start() to run.
Below logs we can see Thread-0 is executed before Thread-1 start().