<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rajubora</title>
    <description>The latest articles on DEV Community by rajubora (@rajubora).</description>
    <link>https://dev.to/rajubora</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F647695%2F195775d1-f680-4f59-b79f-1de9aaff2ce7.png</url>
      <title>DEV Community: rajubora</title>
      <link>https://dev.to/rajubora</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajubora"/>
    <language>en</language>
    <item>
      <title>Exception In java and hierarchy</title>
      <dc:creator>rajubora</dc:creator>
      <pubDate>Tue, 04 Jun 2024 18:37:57 +0000</pubDate>
      <link>https://dev.to/rajubora/exception-in-java-and-hierarchy-53f5</link>
      <guid>https://dev.to/rajubora/exception-in-java-and-hierarchy-53f5</guid>
      <description>&lt;p&gt;While trying to understand the concept of exception in java , it becomes necessary to understand the hierarchy of exceptions becauce &lt;br&gt;
we can see a famous interview question from exception hierarchy in java developer interview.&lt;/p&gt;

&lt;p&gt;suppose we have a parent class with a method that throws &lt;strong&gt;IO exception&lt;/strong&gt; , also there is a sub class that overrides the method of parent class throws &lt;strong&gt;FileNotFoundException&lt;/strong&gt; , will this code works fine or not?&lt;/p&gt;

&lt;p&gt;The answer is no because in Java the overridden method in the subclass cannot throw a broader or more general exception than the method in the parent class. &lt;/p&gt;

&lt;p&gt;In our scenario &lt;strong&gt;IOException&lt;/strong&gt; is a broader exception than &lt;strong&gt;FileNotFoundException&lt;/strong&gt;, so the overriding method in the subclass is not allowed to throw &lt;strong&gt;FileNotFoundException&lt;/strong&gt; (which is a subclass of IOException) .&lt;/p&gt;

&lt;p&gt;let us understand by an example :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We have a parent class&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.io.FileNotFoundException;
import java.io.IOException;
class ParentClass {
    // Method that throws a narrower exception
    public void Method() throws FileNotFoundException {
        System.out.println("IncorrectParentClass: Method");
        if (true) {
            throw new FileNotFoundException("FileNotFoundException from ParentClass");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now we are inheriting all the features of super class in our base &lt;br&gt;
   class:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; class ChildClass extends ParentClass {
    // Overriding method that throws a broader exception
    @Override
    public void Method() throws IOException {
        System.out.println("IncorrectChildClass: Method");
        if (true) { 
            throw new IOException("IOException from ChildClass");
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now Declaring Main class:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main {
    public static void main(String[] args) {
             ParentClass parent =   new ParentClass();
             ChildClass  child   =  new ChildClass();

        try {
            parent.Method();    
        } catch (FileNotFoundException e) {
            System.out.println("Caught FileNotFoundException from parent: " + e.getMessage());
        }

        try {
            child.Method();
        } catch (IOException e) {
            System.out.println("Caught IOException from child: " + e.getMessage());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we try to compile Main.java along with &lt;strong&gt;ParentClass.java&lt;/strong&gt; and  &lt;strong&gt;ChildClass.java&lt;/strong&gt;, you will get a compilation error indicating that IOException is broader than FileNotFoundException.&lt;/p&gt;

&lt;p&gt;The ChildClass will not compile because it violates the rule that an overriding method cannot throw a broader exception than the method it overrides.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Comparable and Comparator in Java</title>
      <dc:creator>rajubora</dc:creator>
      <pubDate>Sun, 12 May 2024 08:25:35 +0000</pubDate>
      <link>https://dev.to/rajubora/comparable-and-comparator-in-java-inh</link>
      <guid>https://dev.to/rajubora/comparable-and-comparator-in-java-inh</guid>
      <description>&lt;p&gt;In java-8  comparable and comparator interface are used for sorting object using custom logic .&lt;br&gt;
However there are some diffrences between them listed as below : &lt;/p&gt;

&lt;h2&gt;
  
  
  Comparable Interface :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For implementing comparable interface we have to override the &lt;strong&gt;compareTo()&lt;/strong&gt; method.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;CompareTo method takes only one arguement, which is the object being compared.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This CompareTo method has to be implemented in the class to define the natural ordering of the objects.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It allow us to define single sorting criteria.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interface Signature :&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface Comparable&amp;lt;T&amp;gt; {
    public int compareTo(T o);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparator Interface :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For implementing Comparator interface we have to override the &lt;strong&gt;compare()&lt;/strong&gt; method of the interface.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This method takes two parameters and  compares them (o1 and o2) &lt;br&gt;
and returns:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;a negative integer if o1 is less than o2.
zero if o1 is equal to o2.
a positive integer if o1 is greater than o2.
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;It allow is to define multiple sorting criteria and can be used to sort object in diffrent ways. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Interface Signature :&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface Comparator&amp;lt;T&amp;gt; {
    public int compare(T o1, T o2);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Example :
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

// Example class representing a Student
class Student implements Comparable&amp;lt;Student&amp;gt; {
    private String name;
    private int id;
    private int age;

    // Constructor
    public Student(String name, int id, int age) {
        this.name = name;
        this.id = id;
        this.age = age;
    }

    // Getters
    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public int getAge() {
        return age;
    }

    // Implementing Comparable interface based on student id
    @Override
    public int compareTo(Student other) {
        return Integer.compare(this.id, other.id);
    }

    // toString method for displaying student details
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                '}';
    }
}

// Comparator to sort students by age
class AgeComparator implements Comparator&amp;lt;Student&amp;gt; {
    @Override
    public int compare(Student s1, Student s2) {
        return Integer.compare(s1.getAge(), s2.getAge());
    }
}

public class ComparableComparatorExample {
    public static void main(String[] args) {
        // Creating a list of students
        List&amp;lt;Student&amp;gt; students = new ArrayList&amp;lt;&amp;gt;();
        students.add(new Student("Ram", 101, 20));
        students.add(new Student("Shyam", 102, 22));
        students.add(new Student("Raj", 103, 21));
        // Sorting students using Comparable (by student id)
        Collections.sort(students);
        System.out.println("Students sorted by ID (Comparable):");
        for (Student student : students) {
            System.out.println(student);
        }

        // Sorting students using Comparator (by age)
        Collections.sort(students, new AgeComparator());
        System.out.println("\nStudents sorted by Age (Comparator):");
        for (Student student : students) {
            System.out.println(student);
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  In above Example :
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The Student class implements the Comparable interface based on the student ID. This allows sorting a list of students by their IDs.&lt;/li&gt;
&lt;li&gt;The AgeComparator class implements the Comparator interface to provide custom sorting logic based on student ages.&lt;/li&gt;
&lt;li&gt;In the main method, we create a list of Student objects and sort them first using the natural ordering defined by Comparable and then using the custom ordering defined by Comparator.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Apache Kafka :</title>
      <dc:creator>rajubora</dc:creator>
      <pubDate>Wed, 24 Jan 2024 19:33:01 +0000</pubDate>
      <link>https://dev.to/rajubora/apache-kafka--1khg</link>
      <guid>https://dev.to/rajubora/apache-kafka--1khg</guid>
      <description>&lt;p&gt;Apache Kafka is an open-source distributed system consisting of servers and clients, which is created by linkedin. Apache Kafka is used by thousands of the world's leading organizations for high-performance data pipelines, streaming analytics, data integration and many other vital applications. Apache Kafka act as an communicaiton/transportation tool between the source system to target system with the following use cases :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;  Messaging systems&lt;/li&gt;
&lt;li&gt;  Activity Tracking&lt;/li&gt;
&lt;li&gt;  Application logs analysis&lt;/li&gt;
&lt;li&gt;  Decoupling of system dependencies&lt;/li&gt;
&lt;li&gt;  Integration with Big Data technologies like Spark, Flink, 
 Storm,  Hdoop.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why apache kafka :&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Distributed , resilient architecture , fault tolerance .&lt;/li&gt;
&lt;li&gt; Horizontal scalability :

&lt;ul&gt;
&lt;li&gt; can scale to 100s of brokers .&lt;/li&gt;
&lt;li&gt; can scale to millions of messages per second .&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; High performance latency of less then 10ms in real time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;following are the companies name that uses apache kafka for real time solutions :&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;1) Netflix uses kafka to apply recommendations in real time while &lt;br&gt;
   you are watching TV shows .&lt;/p&gt;

&lt;p&gt;2) Uber uses kafka to gather user , taxi and trip data in real &lt;br&gt;
   time to compute and forcast demand and compute pricing in real&lt;br&gt;
   time .&lt;br&gt;
3) Linkedin uses kafka to prevent spam , collect user interactions &lt;br&gt;
   to make better connection recommendation in real time .&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Actuators in SpringBoot :</title>
      <dc:creator>rajubora</dc:creator>
      <pubDate>Thu, 11 Jan 2024 16:55:19 +0000</pubDate>
      <link>https://dev.to/rajubora/actuators-in-springboot--4cgc</link>
      <guid>https://dev.to/rajubora/actuators-in-springboot--4cgc</guid>
      <description>&lt;p&gt;Actuators are the list of endpoints which are provided by spring boot by default so that our applications has production ready features. the main benefit of actuators is that we can get production grade tool without having implement these features by ourselves .&lt;/p&gt;

&lt;p&gt;To enable spring boot actuator , we need to add spring boot actuator dependency in&lt;br&gt;
pom.xml :&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;&lt;br&gt;
    &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;&lt;br&gt;
    &amp;lt;artifactId&amp;gt;spring-boot-starter-actuator&amp;lt;/artifactId&amp;gt;&lt;br&gt;
&amp;lt;/dependency&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;also add configuration to application.property file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;management.endpoints.web.exposure.include=* 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;some of the endpoints :&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;/health - it summarizes the health status of application.&lt;br&gt;
/loggers- it enables us to query and modify the logging level of &lt;br&gt;
             our application.&lt;br&gt;
 /env    - returns the current enviroment property .&lt;br&gt;
 /info   - returns general information.&lt;br&gt;
 /beans  - returns all available beans in our BeanFactory. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many benefits of using actuators but main is that we can check the health of microservices .for example there is an order service and a discovery server. the server send message to the service in every T seconds to check wheather service is UP or Down . if it is UP then order service send status code 200 otherwise 500 or 503 . &lt;br&gt;
here T seconds should be chosen in a such  a manner so that the api calls should be minimum .&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Service Discovery :</title>
      <dc:creator>rajubora</dc:creator>
      <pubDate>Sat, 06 Jan 2024 08:58:46 +0000</pubDate>
      <link>https://dev.to/rajubora/service-discovery--2m14</link>
      <guid>https://dev.to/rajubora/service-discovery--2m14</guid>
      <description>&lt;p&gt;In a microservices architecture, a discovery Server is a component that helps microservices locate and communicate with each other within the distributed system. Since microservices contains &lt;br&gt;
number of services so ,for storing all the services together at a location we have to use service registry at that point . Commonly used service registries include tools like Eureka, Consul, and ZooKeeper. &lt;br&gt;
By using Service discovery  and service registry, microservices can achieve better scalability, flexibiliy.&lt;/p&gt;

&lt;p&gt;1) In Springboot based application we can use Service Discovery(Service Registry) as Server by adding a dependency in pom.xml file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
   &amp;lt;groupId&amp;gt;org.springframework.cloud&amp;lt;/groupId&amp;gt; 
   &amp;lt;artifactId&amp;gt;spring-cloud-starter-netflix-eureka- 
    server&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After adding dependency the next step is to add the scripts in application.yml or property file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                  eureka:
                     instance:
                           hostname: localhost
                      client:
                            register-with-eureka: false
                            fetch-registry: false
                  sever:
                      port: 8761
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by adding above scripts the service will acts as a Eureka server where we can store all the client services .&lt;/p&gt;

&lt;p&gt;2) For Using Service Discovery as client we have to add another dependency in pom.xml file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;dependency&amp;gt;
     &amp;lt;groupId&amp;gt;org.springframework.cloud&amp;lt;/groupId&amp;gt;
     &amp;lt;artifactId&amp;gt;spring-cloud-starter-netflix-eureka- 
       client&amp;lt;/artifactId&amp;gt;
  &amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to add scripts in Application.yml or property file :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      eureka:
            instance:
              prefer-ip-address: true
         client:
             register-with-eureka: true
             fetch-registry: true
             serviceUrl:
                defaultZone: http://localhost:8761/eureka/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by adding these scripts the service will act as a eureka client and automatically store into Service Registry&lt;/p&gt;

&lt;p&gt;Note that : netflix-eureka-server  are  the servers of Netflix itself which is configured by spring developers for using it in springboot application .&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
