<?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>Forem: Nilanchal</title>
    <description>The latest articles on Forem by Nilanchal (@nilan).</description>
    <link>https://forem.com/nilan</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%2F221779%2F011341f0-4a9a-4fd5-bd08-8def144a4159.png</url>
      <title>Forem: Nilanchal</title>
      <link>https://forem.com/nilan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nilan"/>
    <language>en</language>
    <item>
      <title>Working with XML-Based REST API with Spring Boot</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sat, 07 Sep 2024 22:04:45 +0000</pubDate>
      <link>https://forem.com/nilan/working-with-xml-based-rest-api-with-spring-boot-2g0h</link>
      <guid>https://forem.com/nilan/working-with-xml-based-rest-api-with-spring-boot-2g0h</guid>
      <description>&lt;p&gt;The &lt;code&gt;@RestController&lt;/code&gt; annotation in Spring boot is designed to automatically serializes Java objects into JSON or XML, based on the content negotiation strategy defined in your controller.&lt;/p&gt;

&lt;p&gt;It combines the &lt;code&gt;@Controller&lt;/code&gt; and &lt;code&gt;@ResponseBody&lt;/code&gt; annotations. It tells Spring that this class is a controller where every method returns a domain object instead of a view. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The payload is automatically converted to JSON or XML based on the value defined in the  &lt;code&gt;Accept&lt;/code&gt; header.&lt;/li&gt;
&lt;li&gt;Similarly, the response object is automatically converted to JSON or XML-based &lt;code&gt;Content-type&lt;/code&gt; header defined in your controller configuration. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring uses the Jackson library internally so we don't need to serialise or deserialise to convert Java objects manually. &lt;/p&gt;

&lt;p&gt;Watch the full video walkthrough.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/O0W3X9hX5bU"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For example in the following controller class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It has 3 controller methods; two GET endpoints that return the movie data&lt;/li&gt;
&lt;li&gt;A POST endpoint that adds a new movie to the MongoDB&lt;/li&gt;
&lt;li&gt;By default, the controller class will accept the JSON request and produce the JSON response.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/api/1.0/movies"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoviesController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;MovieService&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MoviesController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MovieService&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;movieService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Movie&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt; &lt;span class="nf"&gt;createMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;MovieDto&lt;/span&gt; &lt;span class="n"&gt;movieDto&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieDto&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/{movieId}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt; &lt;span class="nf"&gt;getMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;movieId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let us change the &lt;code&gt;MovieController&lt;/code&gt; to accept XML content type in the request body and produce the XML response. &lt;/p&gt;

&lt;h3&gt;
  
  
  Jackson XML Dependency
&lt;/h3&gt;

&lt;p&gt;First, We need to add &lt;a href="https://github.com/FasterXML/jackson-dataformat-xml/wiki/Jackson-XML-annotations#jacksonxmlelementwrapper" rel="noopener noreferrer"&gt;Jackson XML&lt;/a&gt; dependency for reading and writing XML data. &lt;/p&gt;

&lt;p&gt;For the Gradle project, add the following dependency to your &lt;code&gt;build.gradle&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="s1"&gt;'com.fasterxml.jackson.dataformat:jackson-dataformat-xml'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a maven-based project, you can add the following to your &lt;code&gt;pom.xml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;com.fasterxml.jackson.dataformat&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;jackson-dataformat-xml&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to annotate our controller mapping to match the &lt;code&gt;application/xml&lt;/code&gt; media type. This is done using the &lt;code&gt;Content-Type&lt;/code&gt; and &lt;code&gt;Accept&lt;/code&gt; media type to our controller mapping.&lt;/p&gt;

&lt;p&gt;This can be done by defining the appropriate MediaType using the &lt;code&gt;produces&lt;/code&gt; and &lt;code&gt;consumes&lt;/code&gt; property of &lt;code&gt;RequestMapping&lt;/code&gt; annotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@RestController&lt;/span&gt;  
&lt;span class="nd"&gt;@RequestMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/api/1.0/movies"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  
        &lt;span class="n"&gt;consumes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_XML_VALUE&lt;/span&gt;&lt;span class="o"&gt;},&lt;/span&gt; &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Content&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Type&lt;/span&gt;
        &lt;span class="n"&gt;produces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_XML_VALUE&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;  &lt;span class="err"&gt;#&lt;/span&gt; &lt;span class="nc"&gt;Accept&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MoviesController&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;MovieService&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;MoviesController&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MovieService&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;movieService&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Movie&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="nd"&gt;@PostMapping&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt; &lt;span class="nf"&gt;createMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@RequestBody&lt;/span&gt; &lt;span class="nc"&gt;MovieDto&lt;/span&gt; &lt;span class="n"&gt;movieDto&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieDto&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

    &lt;span class="nd"&gt;@GetMapping&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/{movieId}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt; &lt;span class="nf"&gt;getMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@PathVariable&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;movieId&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMovie&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieId&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, we have set the &lt;code&gt;MediaType&lt;/code&gt; configuration to the controller level, which means all controller methods will now consume and produce XML output. We can also do the same to the individual methods by using produces and consumes property on HTTP method mapping annotation.&lt;/p&gt;

&lt;p&gt;That is all, now our controller will handle the XML request and produce the &lt;code&gt;application/xml&lt;/code&gt; media type. Let us test our &lt;code&gt;/movies&lt;/code&gt; endpoint&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl &lt;span class="nt"&gt;--location&lt;/span&gt; &lt;span class="s1"&gt;'http://localhost:8080/api/1.0/movies'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s1"&gt;'Content-type: application/xml'&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--header&lt;/span&gt; &lt;span class="s1"&gt;'Accept: application/xml'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now it will produce XML output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;List&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;item&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;65c4092af4ba290f3c55cd06&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Iron Man &lt;span class="ni"&gt;&amp;amp;amp;&lt;/span&gt; Captain America: Heroes United&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;headline&amp;gt;&lt;/span&gt;Iron Man (Adrian Pasdar) and Captain America (Roger Craig Smith) must prevent Red Skull (Liam O'Brien) and Taskmaster (Clancy Brown) from destroying the world.&lt;span class="nt"&gt;&amp;lt;/headline&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;EN&lt;span class="nt"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;region&amp;gt;&lt;/span&gt;USA&lt;span class="nt"&gt;&amp;lt;/region&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;David Kaye&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Ian McKellen&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Adrian Pasdar&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Action&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Adventure&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Sci-fi&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;item&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;65c4092af4ba290f3c55cd07&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Iron Man &lt;span class="ni"&gt;&amp;amp;amp;&lt;/span&gt; Captain America: Heroes United&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;headline&amp;gt;&lt;/span&gt;Iron Man (Adrian Pasdar) and Captain America (Roger Craig Smith) must prevent Red Skull (Liam O'Brien) and Taskmaster (Clancy Brown) from destroying the world.&lt;span class="nt"&gt;&amp;lt;/headline&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;EN&lt;span class="nt"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;region&amp;gt;&lt;/span&gt;USA&lt;span class="nt"&gt;&amp;lt;/region&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;David Kaye&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Ian McKellen&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Adrian Pasdar&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Action&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Adventure&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Sci-fi&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/item&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/List&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure Default Content Negotiation
&lt;/h3&gt;

&lt;p&gt;The above method works fine but the configuration is now at controller level. The default media type remains JSON for all other controllers. &lt;/p&gt;

&lt;p&gt;We can override this by setting the default content negotiation for all controllers thought the project by implementing the &lt;code&gt;WebMvcConfigurer&lt;/code&gt; configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;WebMvcConfigurer&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  

    &lt;span class="nd"&gt;@Override&lt;/span&gt;  
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;configureContentNegotiation&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContentNegotiationConfigurer&lt;/span&gt; &lt;span class="n"&gt;configurer&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
        &lt;span class="n"&gt;configurer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;defaultContentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_XML&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Wrapping XML Response
&lt;/h3&gt;

&lt;p&gt;Notice the above XML response, the result is wrapped inside &lt;code&gt;&amp;lt;List&amp;gt;&amp;lt;item&amp;gt;&amp;lt;item&amp;gt;&amp;lt;/List&amp;gt;&lt;/code&gt; tag. This is not very pretty. &lt;/p&gt;

&lt;p&gt;We can make the following changes to wrap the response &lt;code&gt;&amp;lt;movies&amp;gt;&amp;lt;movie&amp;gt;&amp;lt;/movie&amp;gt;&amp;lt;/movies&amp;gt;&lt;/code&gt; tag. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a wrapper class for Movies.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@JacksonXmlRootElement&lt;/code&gt; annotation can be used to define the name of the root element used for the root-level object when serialized, which normally uses the name of the type (class). &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;@JacksonXmlElementWrapper&lt;/code&gt; annotation is used to specify XML elements to use for wrapping &lt;code&gt;List&lt;/code&gt; and &lt;code&gt;Map&lt;/code&gt; properties.&lt;/li&gt;
&lt;li&gt;From the controller, instead of returning &lt;code&gt;ResponseEntity&amp;lt;List&amp;lt;Movie&amp;gt;&amp;gt;&lt;/code&gt;, we will return the &lt;code&gt;ResponseEntity&amp;lt;Movies&amp;gt;&amp;gt;&lt;/code&gt; type.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt;
&lt;span class="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="nd"&gt;@RequiredArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@JacksonXmlRootElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;localName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"movies"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Movies&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@JacksonXmlProperty&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;localName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"movie"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@JacksonXmlElementWrapper&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;useWrapping&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Movie&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;moviesList&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;@Getter&lt;/span&gt;
&lt;span class="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="nd"&gt;@JacksonXmlRootElement&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;localName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Movie"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Movie&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;headline&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;language&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;actors&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;genres&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, update the controller to return the &lt;code&gt;ResponseEntity&amp;lt;Movies&amp;gt;&amp;gt;&lt;/code&gt; type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@GetMapping&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Movies&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;Movies&lt;/span&gt; &lt;span class="n"&gt;movies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Movies&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movieService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMovies&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;  
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;ok&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;movies&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this will produce,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;movies&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;movie&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;65c4092af4ba290f3c55cd06&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Iron Man &lt;span class="ni"&gt;&amp;amp;amp;&lt;/span&gt; Captain America: Heroes United&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;headline&amp;gt;&lt;/span&gt;Iron Man (Adrian Pasdar) and Captain America (Roger Craig Smith) must prevent Red Skull (Liam O'Brien) and Taskmaster (Clancy Brown) from destroying the world.&lt;span class="nt"&gt;&amp;lt;/headline&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;EN&lt;span class="nt"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;region&amp;gt;&lt;/span&gt;USA&lt;span class="nt"&gt;&amp;lt;/region&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;David Kaye&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Ian McKellen&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Adrian Pasdar&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Action&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Adventure&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Sci-fi&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;movie&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;65c4092af4ba290f3c55cd07&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Iron Man &lt;span class="ni"&gt;&amp;amp;amp;&lt;/span&gt; Captain America: Heroes United&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;headline&amp;gt;&lt;/span&gt;Iron Man (Adrian Pasdar) and Captain America (Roger Craig Smith) must prevent Red Skull (Liam O'Brien) and Taskmaster (Clancy Brown) from destroying the world.&lt;span class="nt"&gt;&amp;lt;/headline&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;language&amp;gt;&lt;/span&gt;EN&lt;span class="nt"&gt;&amp;lt;/language&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;region&amp;gt;&lt;/span&gt;USA&lt;span class="nt"&gt;&amp;lt;/region&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;David Kaye&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Ian McKellen&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;actors&amp;gt;&lt;/span&gt;Adrian Pasdar&lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/actors&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Action&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Adventure&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;genres&amp;gt;&lt;/span&gt;Sci-fi&lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/genres&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/movie&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/movies&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>springboot</category>
      <category>spring</category>
    </item>
    <item>
      <title>Importing CSV Data into PostgreSQL using Spring Batch</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Tue, 21 May 2024 10:25:29 +0000</pubDate>
      <link>https://forem.com/nilan/importing-csv-data-into-postgresql-using-spring-batch-2206</link>
      <guid>https://forem.com/nilan/importing-csv-data-into-postgresql-using-spring-batch-2206</guid>
      <description>&lt;p&gt;Spring Batch is a powerful module of the Spring framework that provides out-of-the-box implementation for batch processing tasks.&lt;/p&gt;

&lt;p&gt;It is used in scenarios where data needs to be processed in multiple batches, for example, generating daily reports, periodic import of data into a database, or for any complex calculations and transformations of your data.&lt;/p&gt;

&lt;p&gt;A typical batch-processing application involves the following steps:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyprbuumw9k0scavzjqt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyprbuumw9k0scavzjqt6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Spring Batch?
&lt;/h2&gt;

&lt;p&gt;All the above steps can be achieved without using the spring batch. However, the spring batch provides the following benefits which makes a strong case for the framework.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You do not need to reinvent the wheel&lt;/li&gt;
&lt;li&gt;Seamless integration with Spring ecosystem&lt;/li&gt;
&lt;li&gt;Chunk-based processing&lt;/li&gt;
&lt;li&gt;Includes I/O capabilities such as support for a wide range of data sources and targets, including databases, XML, JSON, and flat files.&lt;/li&gt;
&lt;li&gt;Detailed monitoring and logging: Allows tracking of job and step execution, making it easier to understand the status and performance of batch jobs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring Batch provides two different ways to implement a job. We can process data using Tasklets and Chunks.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Tasklet&lt;/strong&gt; is a single task within a step. The Tasklet interface defines a single method execute(), called once during the step execution.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;Chunk-based&lt;/strong&gt; approach is more suitable for processing large datasets where data can be read, processed, and written in smaller, manageable chunks. This is typically used for reading data from a database or a file and processing it record by record. The chunk model is built around three main components; Reader, Writer and Processor&lt;/p&gt;

&lt;h2&gt;
  
  
  Spring Boot Batch Chunk Processing
&lt;/h2&gt;

&lt;p&gt;The key components of a Spring boot batch application include: - Job &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step &lt;/li&gt;
&lt;li&gt;Readers, Writers, Processors &lt;/li&gt;
&lt;li&gt;Job Repository &lt;/li&gt;
&lt;li&gt;Job Launcher&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The primary components of the Spring Batch and overall process flow are shown in the figure below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JobLauncher:&lt;/strong&gt; &lt;br&gt;
The JobLauncher is an interface that represents the component responsible for running the jobs. It takes care of receiving job parameters and launching a job with those parameters. It's typically used to start a job from different triggers such as an application event, a REST API call, or from scheduler.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Job:&lt;/strong&gt;&lt;br&gt;
A Job in a Spring Batch is an entity that encapsulates an entire batch process and is defined by a series of steps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step:&lt;/strong&gt;&lt;br&gt;
A single job may have one or more steps, where each step typically involves reading data, processing it, and writing the processed data to the output source.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ItemReader:&lt;/strong&gt;&lt;br&gt;
The ItemReader is responsible for reading data from different sources such as databases or files. The ItemReader has a &lt;code&gt;read()&lt;/code&gt; method, every time this method is invoked, it will return one item. If there are no more items to read, it returns null to indicate the end of the data input.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;FlatFileItemReader&lt;/code&gt; can be used for reading data from flat files (like CSV), &lt;code&gt;JdbcCursorItemReader&lt;/code&gt; for reading from databases using a JDBC cursor, and &lt;code&gt;JpaPagingItemReader&lt;/code&gt; can be used for reading database records using JPA pagination.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ItemProcessor:&lt;/strong&gt;&lt;br&gt;
The ItemProcessor is completely optional. It is used to validate, transform, or filter the items before passing them to the ItemWriter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ItemWriter:&lt;/strong&gt;&lt;br&gt;
The ItemWriter takes processed items and writes them to a database or a file. The &lt;code&gt;FlatFileItemWriter&lt;/code&gt; can be used for writing data to flat files, &lt;code&gt;JdbcBatchItemWriter&lt;/code&gt; for batching database operations through JDBC, and &lt;code&gt;JpaItemWriter&lt;/code&gt; for handling database operations using JPA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Job Repository:&lt;/strong&gt;&lt;br&gt;
The JobRepository does all the hard work such as recording the status of jobs in a database. It keeps track of which jobs are running, which have been completed, and if a job fails, what step it failed at. This is critical for jobs that need to be restarted after a failure, ensuring that the job can pick up where it left off.&lt;/p&gt;
&lt;h2&gt;
  
  
  Load CSV Data to Postgres SQL using Spring Batch
&lt;/h2&gt;

&lt;p&gt;This example uses chunk-based processing for reading CSV files, processing it and then storing it in Postgres SQL. For managing the database migrations we will use Flyway.&lt;/p&gt;

&lt;p&gt;Use Spring Initializr to bootstrap a spring boot project by selecting the required dependencies. This example uses Java 17 and Spring Boot 3.2.4 and has the following dependencies.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Batch&lt;/li&gt;
&lt;li&gt;Spring Data JPA&lt;/li&gt;
&lt;li&gt;PostgreSQL Driver&lt;/li&gt;
&lt;li&gt;Lombok - completely optional for reducing boilerplate code.&lt;/li&gt;
&lt;li&gt;Flyway - Database migration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Watch the step-by-step guide here &lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/XBW3D1f6CAA"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Find the full blog post and source code here at &lt;a href="https://stacktips.com/articles/importing-csv-data-into-postgresql-using-spring-boot-batch" rel="noopener noreferrer"&gt;stacktips.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>spring</category>
      <category>springboot</category>
      <category>springbatch</category>
      <category>youtube</category>
    </item>
    <item>
      <title>Validating Configuration Properties in Spring Boot Application Startup</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Tue, 07 May 2024 18:20:35 +0000</pubDate>
      <link>https://forem.com/nilan/validating-configuration-properties-in-spring-boot-application-startup-21na</link>
      <guid>https://forem.com/nilan/validating-configuration-properties-in-spring-boot-application-startup-21na</guid>
      <description>&lt;p&gt;The &lt;code&gt;@ConfigurationProperties&lt;/code&gt; annotation in Spring Boot is used to bind configuration parameters typically from properties or YAML files to a Java class.&lt;/p&gt;

&lt;p&gt;But, did you know you also can validate configuration properties at spring application startup with the &lt;code&gt;@ConfigurationProperties&lt;/code&gt; annotation? Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/xMORu5AP67c"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Let's say we have a Spring boot importer service that imports customer data from a CSV file to the database periodically. For the importer to work we have the following configurations:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;importer&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  
  &lt;span class="na"&gt;service&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;  
    &lt;span class="na"&gt;filePath&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;/nfs/files'&lt;/span&gt;
    &lt;span class="na"&gt;fileType&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;.xslx'&lt;/span&gt;
    &lt;span class="na"&gt;threadPoolSize&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These properties can be mapped to a Java class automatically using &lt;code&gt;@ConfigurationProperties&lt;/code&gt; annotation as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt; 
&lt;span class="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"importer.service"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImporterConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fileType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;threadPoolSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's say, we want to ensure the importer configuration is provided and meets the validation criteria when the application starts. If any of the validation fails, then the application should fail during startup. &lt;/p&gt;

&lt;p&gt;For this validation to work, we need to have a &lt;strong&gt;JSR-380&lt;/strong&gt; implementation like &lt;strong&gt;&lt;em&gt;Hibernate Validator&lt;/em&gt;&lt;/strong&gt; on your classpath.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="s1"&gt;'org.springframework.boot:spring-boot-starter-validation'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once we have a validator in the classpath, we need to explicitly enable the configuration properties validation using &lt;code&gt;@Validated&lt;/code&gt; annotation. &lt;/p&gt;

&lt;p&gt;For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt;  
&lt;span class="nd"&gt;@Setter&lt;/span&gt;  
&lt;span class="nd"&gt;@Component&lt;/span&gt;  
&lt;span class="nd"&gt;@Validated&lt;/span&gt;  
&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"importer.service"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ImporterConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

    &lt;span class="nd"&gt;@NotNull&lt;/span&gt;  
    &lt;span class="nd"&gt;@Pattern&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regexp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"\\.csv$|\\.txt$"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fileType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  

    &lt;span class="nd"&gt;@Positive&lt;/span&gt;  
    &lt;span class="nd"&gt;@Max&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;threadPoolSize&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can validate properties on application startup when used in conjunction with JSR-380 bean validation annotations such as &lt;code&gt;@Max&lt;/code&gt;, and &lt;code&gt;@NotNull&lt;/code&gt;. Here are some of the common annotations used for bean validation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@NotNull&lt;/code&gt;: specifies that a property must be not null&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@NotEmpty&lt;/code&gt;: specifies that a property must be not null or not empty&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Size&lt;/code&gt;: ensure that a property size is between attributes min and max&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Email&lt;/code&gt;: specifies that a property must be a valid email address&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@AssertTrue&lt;/code&gt; / &lt;code&gt;@AssertFalse&lt;/code&gt;: ensure that a property value is true/false&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Positive&lt;/code&gt; / &lt;code&gt;@Negative&lt;/code&gt;: ensure that a property is positive/negative&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Past&lt;/code&gt; / &lt;code&gt;@Future&lt;/code&gt;: specifies that the date must be in the past/ future&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Max&lt;/code&gt; / &lt;code&gt;@Min&lt;/code&gt;: specifies that a property has a value not greater/not smaller than the value attribute&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A full list of built-in &lt;a href="https://beanvalidation.org/2.0-jsr380/spec/#builtinconstraints" rel="noopener noreferrer"&gt;validation constraints&lt;/a&gt; in JSR 380 can be found here.&lt;/p&gt;

&lt;p&gt;Now, Run the application and notice that it will bind these properties from &lt;code&gt;application.yaml&lt;/code&gt; to the &lt;code&gt;ImporterConfig&lt;/code&gt; object and validate them at application startup, thus ensuring the application doesn't run with invalid configurations. &lt;/p&gt;

&lt;p&gt;If the validation fails it will result in &lt;code&gt;BindException&lt;/code&gt; as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target com.stacktips.app.config.ImporterConfig failed:

    Property: importer.service.fileType
    Value: ".xlsx"
    Origin: class path resource [application.yaml] - 8:15
    Reason: must match "\.csv$|\.txt$"


Action:

Update your application's configuration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you update the &lt;code&gt;fileType: .csv&lt;/code&gt; and the application will start as expected.&lt;/p&gt;

&lt;h3&gt;
  
  
  Considerations for Using Records
&lt;/h3&gt;

&lt;p&gt;Starting with Spring Boot 2.6 and Java 16 you can use record classes with &lt;code&gt;@ConfigurationProperties&lt;/code&gt;. But there are a few things you need to consider when using records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Validated&lt;/span&gt;  
&lt;span class="nd"&gt;@ConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"importer.service"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="nf"&gt;ImporterProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;  
        &lt;span class="nd"&gt;@NotNull&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;  
        &lt;span class="nd"&gt;@Min&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;threadPoolSize&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;@Componenet&lt;/code&gt; annotation cannot be used with records. Your IDE will show an &lt;code&gt;annotated with @ConstructorBinding but defined as a Spring component&lt;/code&gt; error. &lt;/p&gt;

&lt;p&gt;This is because the &lt;code&gt;@Component&lt;/code&gt; annotation is generally used to define a bean that the Spring container will automatically detect and manage. As Java records are immutable, it conflicts with the way Spring manages beans, which involves proxying, intercepting, and potentially altering object states or behaviour.&lt;/p&gt;

&lt;p&gt;To fix this, we need to remove the &lt;code&gt;@Component&lt;/code&gt; annotation and explicitly define the bean registration. We can either use &lt;code&gt;@EnableConfigurationProperties&lt;/code&gt; or define the &lt;code&gt;@Bean&lt;/code&gt; annotation in one of the &lt;code&gt;@Configuration&lt;/code&gt; classes. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@EnableConfigurationProperties&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ImporterProperties&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;  
&lt;span class="nd"&gt;@SpringBootApplication&lt;/span&gt;  
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyApplication&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
       &lt;span class="nc"&gt;SpringApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MyApplication&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;  
    &lt;span class="o"&gt;}&lt;/span&gt;  

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>springboot</category>
      <category>validation</category>
      <category>spring</category>
    </item>
    <item>
      <title>Task Execution and Scheduling in Spring Boot</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sat, 20 Apr 2024 12:35:56 +0000</pubDate>
      <link>https://forem.com/nilan/task-execution-and-scheduling-in-spring-boot-55e4</link>
      <guid>https://forem.com/nilan/task-execution-and-scheduling-in-spring-boot-55e4</guid>
      <description>&lt;p&gt;Spring Scheduler is used for running repetitive tasks or to automate tasks that need to run at specific times or at specific intervals. For example such as sending our email newsletters to your customers, generating daily reports, or updating a database periodically.&lt;/p&gt;

&lt;p&gt;In this crash course, we will cover everything you need to know about Spring Scheduler – including the annotations, examples, and other things to consider.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enable Scheduling
&lt;/h2&gt;

&lt;p&gt;To enable Spring's scheduled task execution capability, just annotate any of your &lt;code&gt;@Configuration&lt;/code&gt; classes with &lt;code&gt;@EnableScheduling&lt;/code&gt; annotation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableScheduling&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SchedulerConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//TODO Here it goes your configuration&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Cron expression
&lt;/h3&gt;

&lt;p&gt;A Cron expression consists of six sequential fields and is declared as &lt;code&gt;second, minute, hour, day of the month, month, day(s) of the week&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │              (0 or 7 is Sunday or MON-SUN)
│ │ │ │ │ │
* * * * * *
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ref: &lt;a href="https://crontab.guru/"&gt;https://crontab.guru/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And is declared as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Slf4j&lt;/span&gt;
&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;SimpleDateFormat&lt;/span&gt; &lt;span class="n"&gt;dateFormat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;SimpleDateFormat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"HH:mm:ss"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"*/5 * * * * *"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;currentTime&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Current Time = {}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dateFormat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also set the timezone as: &lt;a href="https://docs.oracle.com/cd/B13866_04/webconf.904/b10877/timezone.htm"&gt;https://docs.oracle.com/cd/B13866_04/webconf.904/b10877/timezone.htm&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cron&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"* * * * * *"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;zone&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Europe/London"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fixed delay
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fixedDelay&lt;/code&gt; property makes sure that there is a delay of n milliseconds between the finish time of an execution of a task and the start time of the next execution of the task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedDelay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//This will execute periodically, after the one before finishes&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; 

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, milliseconds will be used as the time unit for fixed delay, fixed rate, and initial delay values. If you would like to use a different time unit such as seconds or minutes, you can configure this via the &lt;code&gt;timeUnit&lt;/code&gt; attribute in &lt;code&gt;@Scheduled&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, the previous example can also be written as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedDelay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;timeUnit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;SECONDS&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; 
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//This will execute periodically, after the one before finishes&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; 

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Fixed Rate
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;fixedRate&lt;/code&gt; is used when we want to execute a task periodically at every n millisecond without checking for any previous executions of the task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedRate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
       &lt;span class="c1"&gt;//This will execute periodically&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For both &lt;code&gt;fixedDelay&lt;/code&gt; and &lt;code&gt;fixedRate&lt;/code&gt; tasks, you can specify an &lt;code&gt;intialDelay&lt;/code&gt; by indicating the amount of time to wait before the first execution of the method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Component&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;initailDelay&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fixedRate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//This will execute periodically&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt; 
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Schedule Tasks and Concurrency
&lt;/h3&gt;

&lt;p&gt;Spring Boot uses a &lt;code&gt;ThreadPoolTaskScheduler&lt;/code&gt; to execute scheduled tasks. By default, this thread pool has a single thread. This means that only one scheduled task can be executed at a time.&lt;/p&gt;

&lt;p&gt;If you need to execute multiple tasks concurrently, then you need to configure the &lt;code&gt;ThreadPoolTaskScheduler&lt;/code&gt; to have the required thread pool size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Bean&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;ThreadPoolTaskScheduler&lt;/span&gt; &lt;span class="nf"&gt;threadPoolTaskScheduler&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ThreadPoolTaskScheduler&lt;/span&gt; &lt;span class="n"&gt;threadPoolTaskScheduler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ThreadPoolTaskScheduler&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;threadPoolTaskScheduler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setPoolSize&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;threadPoolTaskScheduler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, we need to use the &lt;code&gt;@Async&lt;/code&gt; annotation to mark the scheduled task as asynchronous. This will make Spring Boot execute the tasks in separate threads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Async&lt;/span&gt;
    &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedDelay&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;startUsingFixedDelay&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;log&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;info&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"startUsingFixedDelay:: Task started at {}"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="no"&gt;DATE_FORMAT&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="o"&gt;()));&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Aggregate Scheduled annotations
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;@Schedules&lt;/code&gt; annotation is a container annotation that aggregates several Scheduled annotations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Schedules&lt;/span&gt;&lt;span class="o"&gt;({&lt;/span&gt;
   &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
   &lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0 * * * * MON-FRI"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;})&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomething&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//This will execute periodically&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before Java 8, a wrapper/container annotation was required to use multiple instances of the same annotation. But Java 8 supports repeatable annotations so wrapper annotation is no longer necessary. Multiple annotations can be used without a wrapper.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fixedRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@Scheduled&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0 * * * * MON-FRI"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doSomethingElse4&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//This will execute periodically&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This rule is automatically disabled when the project’s sonar.java.source is lower than 8 as repeating annotations were introduced in Java 8.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing the Scheduler using Awaitility
&lt;/h2&gt;

&lt;p&gt;Testing the scheduler can be a little tricky. We can test this by manually waiting for &lt;code&gt;x&lt;/code&gt; number of minutes using &lt;code&gt;Thread.sleep()&lt;/code&gt; , but for complex scenarios, this will be hard to scale.&lt;/p&gt;

&lt;p&gt;This is where we can use a framework like &lt;strong&gt;Awaitility&lt;/strong&gt; to wait for a certain condition to be met while running your test. In this case, we want to wait for &lt;code&gt;1000&lt;/code&gt; milliseconds before we assess the test results.&lt;/p&gt;

&lt;p&gt;Let's first add the &lt;code&gt;awaitility&lt;/code&gt; test dependency on your &lt;code&gt;build.gradle&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;testImplementation&lt;/span&gt; &lt;span class="s1"&gt;'org.awaitility:awaitility:3.1.2'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@SpringJUnitConfig&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SchedulerConfig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MySchedulerTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@SpyBean&lt;/span&gt;
    &lt;span class="nc"&gt;MyScheduler&lt;/span&gt; &lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;givenSleepBy1000ms_whenStartTask1&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="nc"&gt;Awaitility&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;await&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;atMost&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;MILLISECONDS&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;untilAsserted&lt;/span&gt;&lt;span class="o"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scheduler&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;atLeast&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;startUsingFixedDelay&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="o"&gt;});&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Task Monitoring:
&lt;/h2&gt;

&lt;p&gt;Spring boot &lt;a href="https://stacktips.com/courses/spring-boot-for-beginners/working-with-spring-boot-actuators"&gt;actuators&lt;/a&gt; provides the &lt;code&gt;/scheduledtasks&lt;/code&gt; endpoint to monitor the list of tasks scheduled and their configurations. To enable this we need to add the actuator starter dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;implementation&lt;/span&gt; &lt;span class="s1"&gt;'org.springframework.boot:spring-boot-starter-actuator'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once Actuator dependencies is added, we need to explicitly include the &lt;code&gt;scheduledtasks&lt;/code&gt; endpoint by using the following property&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight groovy"&gt;&lt;code&gt;&lt;span class="n"&gt;management&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endpoints&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;web&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exposure&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;scheduledtasks&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Did you know?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What happens if a scheduled task throws an exception?&lt;/strong&gt;&lt;br&gt;
If any exception is encountered during the scheduled task execution and if it is not handled gracefully using a try-catch block then the Spring Logging Error Handler will handle the exception and log the error details. &lt;/p&gt;

&lt;p&gt;The next instances of that task will continue to execute as per the schedule. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What happens if a scheduled task takes longer than its scheduled interval?&lt;/strong&gt;&lt;br&gt;
In the case of a fixed rate, if a scheduled task takes longer than its scheduled interval, the Spring Scheduler will start the next execution of the task immediately after the previous one is completed. &lt;/p&gt;

&lt;p&gt;This can cause tasks to overlap, which may impact performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations of Spring Scheduler
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No support for dynamic Scheduling&lt;/strong&gt;: The &lt;code&gt;@Scheduled&lt;/code&gt; annotations are typically configured at application startup and do not support &lt;a href="https://stacktips.com/articles/dynamic-scheduling-in-quartz-with-spring-boot-actuators"&gt;dynamic scheduling&lt;/a&gt; without redeploying the application.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No support for Job Persistence&lt;/strong&gt;: It does not offer built-in support for Job persistence, as a result, job recovery in the event of an application restart is not possible.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No clustering, load balancing&lt;/strong&gt;: It does not support clustering and load balancing, The tasks are typically run on a single node.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited control&lt;/strong&gt;: It does not allow fine-grained control over managing the tasks. For example, we cannot pause, resume, and unscheduled jobs individually.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring Scheduler is sufficient for many simple scheduling tasks, but if you have complex scheduling requirements, job management, and monitoring, then you would need another framework like &lt;a href="https://stacktips.com/articles/working-with-quartz-scheduler-in-spring-boot"&gt;Quartz&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>spring</category>
      <category>scheduler</category>
    </item>
    <item>
      <title>What is Project Lombok? Is it still relevant in 2023?</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sat, 30 Mar 2024 07:16:17 +0000</pubDate>
      <link>https://forem.com/nilan/what-is-project-lombok-is-it-still-relevant-in-2023-477h</link>
      <guid>https://forem.com/nilan/what-is-project-lombok-is-it-still-relevant-in-2023-477h</guid>
      <description>&lt;p&gt;&lt;em&gt;What is Project Lombok? Have you used this magical library?&lt;/em&gt; With the new Java Records feature, you might wonder if Lombok is still relevant. &lt;em&gt;Let's weigh the pros and cons and see if this is suitable for you.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Project Lombok?
&lt;/h2&gt;

&lt;p&gt;Project Lombok is a popular Java library, used to remove the boilerplate code with the intent to save development time.&lt;/p&gt;

&lt;p&gt;It replaces the boilerplate code such as getters, setters, constructors, toString, equals, and builders with a set of annotations. This means less code, saves time, and reduces verbosity, making your codebase cleaner, readable, and more maintainable.&lt;/p&gt;

&lt;p&gt;It also includes utilities such as &lt;strong&gt;delombok&lt;/strong&gt; checking null values and &lt;code&gt;@Slf4j&lt;/code&gt; for logging support.&lt;/p&gt;

&lt;p&gt;For example, traditionally a class declaration looks something like this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fuelType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;boolean&lt;/span&gt; &lt;span class="nf"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;o&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Car&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;o&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;model&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;car&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Car{"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;"model='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", year="&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="s"&gt;", fuelType='"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="sc"&gt;'\''&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt;
                &lt;span class="sc"&gt;'}'&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;hashCode&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Objects&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getModel&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setModel&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;getYear&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setYear&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;getFuelType&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setFuelType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;fuelType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But with Lombok, we can write the same code using a bunch of annotations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Getter&lt;/span&gt;
&lt;span class="nd"&gt;@Setter&lt;/span&gt;
&lt;span class="nd"&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@ToString&lt;/span&gt;
&lt;span class="nd"&gt;@EqualsAndHashCode&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can also replace all the above annotations with just one &lt;code&gt;@Data&lt;/code&gt; annotation.&lt;/p&gt;

&lt;h3&gt;
  
  
  How it works?
&lt;/h3&gt;

&lt;p&gt;It works by using an annotation processor to modify the abstract syntax tree (AST) of your code during compilation. The annotation processor reads annotations defined in your class and adds required methods to the AST. The compiler then uses the modified AST to produce the bytecode.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lombok Annotations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;@Getter&lt;/code&gt;, &lt;code&gt;@Setter&lt;/code&gt; : Generates getters and setter methods for the field.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@NoArgsConstructor&lt;/code&gt;, &lt;code&gt;@AllArgsConstructor&lt;/code&gt;, &lt;code&gt;@RequiredArgsConstructor&lt;/code&gt; : Generates constructors for your class&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Builder&lt;/code&gt;: Provides a builder pattern interface for the annotated class.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Data&lt;/code&gt; : Equivalent to adding the annotations &lt;code&gt;@Getter&lt;/code&gt;,&lt;code&gt;@Setter&lt;/code&gt;, &lt;code&gt;@RequiredArgsConstructor&lt;/code&gt;, &lt;code&gt;@ToString&lt;/code&gt;, &lt;code&gt;@EqualsAndHashCode&lt;/code&gt; to your class &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@Value&lt;/code&gt;:  Makes the class instances immutable&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@NonNull&lt;/code&gt;:  Null check &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@SneakyThrows&lt;/code&gt; : Could be added to a method that throws a checked exception &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@ToString&lt;/code&gt; : Generates a toString() method &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@EqualsAndHashCode&lt;/code&gt; : Generates an &lt;code&gt;.equals(..)&lt;/code&gt; and a &lt;code&gt;hashCode()&lt;/code&gt; methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Check out the official documentation for all Lombok annotations and their usage. &lt;a href="https://t.co/HoiMNzPm4k"&gt;https://t.co/HoiMNzPm4k&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Things to consider
&lt;/h2&gt;

&lt;p&gt;While it promotes simplicity and clean code, it does too much magic in the background, and for newbie developers, it can be quite confusing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Find usage problem
&lt;/h3&gt;

&lt;p&gt;When using Lombok you don't see the generated getter, setter, constructor, builder methods, etc. So, if you want to want to find out where these methods are being used in your project, you can't do this without being dependent on IDE plugins.&lt;/p&gt;

&lt;h3&gt;
  
  
  Misuse of &lt;code&gt;@Builder&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;With Lombok, a builder is so easy to create that even when a class has only two parameters the developers prefer to use &lt;a class="mentioned-user" href="https://dev.to/builder"&gt;@builder&lt;/a&gt; instead of a constructor or a static constructor method. Doing so makes it more harm than good.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;code&gt;@Value&lt;/code&gt; annotation and final class
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;@Value&lt;/code&gt; annotations make the class Final. It is the immutable variant of &lt;code&gt;@Data&lt;/code&gt; annotation.&lt;/p&gt;

&lt;p&gt;All fields are made private and final by default and this does not generate any setter methods. If you want to make your class extendable, then you cannot use the &lt;code&gt;@Value&lt;/code&gt; annotation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Value&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Car&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;fuelType&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The same behaviour for a class can be achievable natively in Java using &lt;a href="https://stacktips.com/articles/java-17-interview-questions-and-answers#1-what-is-the-records-in-java"&gt;Java 16 records&lt;/a&gt;. With records, you can define the data fields in one line of code, instead of having to define a constructor and getter/setter methods for each field in a class.&lt;/p&gt;

&lt;p&gt;Records also have a built-in &lt;code&gt;equals()&lt;/code&gt; and &lt;code&gt;hashCode()&lt;/code&gt; method and they are _ &lt;strong&gt;immutable&lt;/strong&gt; _ by default. It makes your code shorter, easier to read, and less prone to errors.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public record Car(String model, int year, String fuelType) {

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

&lt;/div&gt;



&lt;p&gt;With the inclusion of records in Java, one would wonder if you need Lombok at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using &lt;code&gt;@Data&lt;/code&gt; on Entities
&lt;/h3&gt;

&lt;p&gt;You need to use the &lt;code&gt;@Data&lt;/code&gt; annotation very cautiously. It can cause serious serialization issues with ORMs like Hibernate and serializer engines like Gson.&lt;/p&gt;

&lt;p&gt;Do yourself a favour and read this article by &lt;a href="https://thorben-janssen.com/lombok-hibernate-how-to-avoid-common-pitfalls/"&gt;Thorben Janssen&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Verdict
&lt;/h2&gt;

&lt;p&gt;Lombok is just one tool in the toolbox. You need to understand the trade-offs and consider team expertise before considering this. Lombok makes a lot of sense in Greenfield and small projects to get off the ground. Introducing larger projects has mixed benefits at bestIf you using an IDE such as IntelliJ, it is pretty easy to generate these boilerplate codes, and enabling the code folding can help to make your code more readable.&lt;/p&gt;

&lt;p&gt;Unless you know what you're doing, I suggest holding back on your temptation to use Lombok.&lt;/p&gt;

&lt;p&gt;What is your experience? Do you use Lombok for your projects? Would love you hear your experience.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Originally published at &lt;a href="http://stacktips.com/articles/project-lombok-is-it-still-relevant-in-2023"&gt;&lt;/a&gt;&lt;a href="http://stacktips.com"&gt;http://stacktips.com&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
      <category>java</category>
    </item>
    <item>
      <title>Introducing Bloggy: The Open-Source Blogging Platform Built with Python and Django</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sun, 22 Oct 2023 09:55:16 +0000</pubDate>
      <link>https://forem.com/nilan/introducing-bloggy-the-open-source-blogging-platform-built-with-python-and-django-293e</link>
      <guid>https://forem.com/nilan/introducing-bloggy-the-open-source-blogging-platform-built-with-python-and-django-293e</guid>
      <description>&lt;p&gt;Today, I am excited to take a giant leap forward in my journey by open-source the codebase of my blog &lt;a href="https://stacktips.com"&gt;stacktips&lt;/a&gt; is now available on GitHub. &lt;/p&gt;

&lt;p&gt;Check out the codebase here.&lt;br&gt;
&lt;a href="https://github.com/StackTipsLab/bloggy"&gt;https://github.com/StackTipsLab/bloggy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is built using Python and the latest version of the Django framework. I am no expert in Python, but this is my attempt to master the language and framework. &lt;/p&gt;

&lt;h2&gt;
  
  
  Key Features
&lt;/h2&gt;

&lt;p&gt;Along with tons of features aimed at enhancing the development and blogging experience. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Signup and Login&lt;/strong&gt;: Seamlessly create your account and log in to access exclusive content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Magic Link Sign-In&lt;/strong&gt;: Forget passwords! We've streamlined the login process with magic link sign-in.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Create and Publish&lt;/strong&gt;: Share your knowledge with the world by creating and publishing articles, courses, and quizzes effortlessly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Customized Admin Dashboard&lt;/strong&gt;: Manage your content efficiently with a user-friendly admin dashboard designed with you in mind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Sitemaps&lt;/strong&gt;: Enhance discoverability with built-in sitemaps that improve search engine ranking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Webmaster Notifications&lt;/strong&gt;: Get noticed! StackTips automates Google and Bing webmaster notifications to ensure your content reaches a wider audience.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Open-Source StackTips?
&lt;/h2&gt;

&lt;p&gt;I believe in the power of collaboration. By open-sourcing my blog codebase, I invite the developer community to contribute, improve, and build upon our platform. &lt;/p&gt;

&lt;p&gt;This isn't just a blog; it's a collective effort to create a resource that truly serves developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Get Involved!
&lt;/h2&gt;

&lt;p&gt;Are you a developer looking to enhance your skills, share your knowledge, or simply be curious about the inner workings of a developer-centric blog platform? Now's your chance! &lt;/p&gt;

&lt;p&gt;Want to contribute right away? Check out the issues section.&lt;br&gt;
&lt;a href="https://github.com/StackTipsLab/bloggy/issues"&gt;https://github.com/StackTipsLab/bloggy/issues&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Join us on GitHub and contribute to the Bloggy project: &lt;a href="https://github.com/StackTipsLab/bloggy"&gt;https://github.com/StackTipsLab/bloggy&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's shape the future of developer blogging together. &lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>python</category>
      <category>opensource</category>
      <category>django</category>
      <category>github</category>
    </item>
    <item>
      <title>Replace Embedded Tomcat Server with Jetty or Undertow in Spring Boot3</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sat, 07 Oct 2023 01:06:27 +0000</pubDate>
      <link>https://forem.com/nilan/replace-embedded-tomcat-server-with-jetty-or-undertow-in-spring-boot3-2506</link>
      <guid>https://forem.com/nilan/replace-embedded-tomcat-server-with-jetty-or-undertow-in-spring-boot3-2506</guid>
      <description>&lt;p&gt;One of the key features of the Spring Boot framework is the built-in support for embedded servers. It includes support for embedded Tomcat, Jetty, and Undertow servers. This means you don’t need any external web servers and no need to deploy WAR files anymore.&lt;/p&gt;

&lt;p&gt;The embedded Tomcat server is available through the &lt;code&gt;spring-boot-starter-web&lt;/code&gt; dependency.&lt;/p&gt;

&lt;p&gt;However, if you want Jetty or Undertow servers then you can include &lt;code&gt;spring-boot-starter-jetty&lt;/code&gt; or &lt;code&gt;spring-boot-starter-undertow&lt;/code&gt; dependencies.&lt;/p&gt;

&lt;p&gt;This video tutorial explains how to replace the default embedded Tomcat with Jetty or Undertow servers in your Maven or Gradle project.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1gEoiMVULt4"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>jetty</category>
      <category>undertow</category>
      <category>springboot3</category>
    </item>
    <item>
      <title>Popular Java Interview Questions and Answers</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Mon, 02 Oct 2023 00:02:35 +0000</pubDate>
      <link>https://forem.com/nilan/popular-java-interview-questions-and-answers-3nn3</link>
      <guid>https://forem.com/nilan/popular-java-interview-questions-and-answers-3nn3</guid>
      <description>&lt;h3&gt;
  
  
  What is the difference between comparison done by equals method and == operator?
&lt;/h3&gt;

&lt;p&gt;In Java, equals() method is used to compare the contents of two string objects and returns true if the two have the same value while == operator compares the references of two string objects.&lt;/p&gt;

&lt;p&gt;In the following example, equals() returns true as the two string objects have the same values. However == operator returns false as both string objects are referencing to different objects:&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 EqualsTest {

    public static void main(String args[]) {
        String str1 = new String("Hello World");
        String str2 = new String("Hello World");

        //Reference comparison
        System.out.println(s1 == s2);

        //Content comparison
        System.out.println(s1.equals(s2));

        // integer-type
        System.out.println(10 == 10);

        // char-type
        System.out.println('a' == 'a');
    }

}

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

&lt;/div&gt;



&lt;p&gt;Output&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;false
true
true
true

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  How is an infinite loop declared in Java?
&lt;/h3&gt;

&lt;p&gt;Infinite loops are those loops that run infinitely without any breaking conditions. Some examples of consciously declaring an infinite loop are:&lt;/p&gt;

&lt;p&gt;Using For Loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (;;)
{
   // Business logic
   // Any break logic
}

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

&lt;/div&gt;



&lt;p&gt;Using while loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;while(true){
   // Business logic
   // Any break logic
}

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

&lt;/div&gt;



&lt;p&gt;Using do-while loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;do{
   // Business logic
   // Any break logic
} while(true);

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why is the main method static in Java?
&lt;/h3&gt;

&lt;p&gt;The main method is always static because static members are those methods that belong to the classes, not to an individual object. So if the main method will not be static then for every object, It is available. And that is not acceptable by JVM. JVM calls the main method based on the class name itself. Not by creating the object.&lt;/p&gt;

&lt;p&gt;Because there must be only 1 main method in the java program as the execution starts from the main method. So for this reason the main method is static.&lt;/p&gt;

&lt;h3&gt;
  
  
  How would you differentiate between a String, StringBuffer, and a StringBuilder?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Storage area:&lt;/strong&gt; In string, the String pool serves as the storage area. For StringBuilder and StringBuffer, heap memory is the storage area.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mutability:&lt;/strong&gt; A String is immutable, whereas both the StringBuilder and StringBuffer are mutable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Efficiency:&lt;/strong&gt; It is quite slow to work with a String. However, StringBuilder is the fastest in performing operations. The speed of a StringBuffer is more than a String and less than a StringBuilder. (For example appending a character is fastest in StringBuilder and very slow in String because a new memory is required for the new String with appended character.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Thread-safty:&lt;/strong&gt; In the case of a threaded environment, StringBuilder and StringBuffer are used whereas a String is not used. However, StringBuilder is suitable for an environment with a single thread, and a StringBuffer is suitable for multiple threads.&lt;/p&gt;

&lt;p&gt;Syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// String
String first = "InterviewBit";
String second = new String("InterviewBit");

// StringBuffer
StringBuffer third = new StringBuffer("InterviewBit");

// StringBuilder
StringBuilder fourth = new StringBuilder("InterviewBit");

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is a Comparator in java?
&lt;/h3&gt;

&lt;p&gt;Consider the example where we have an ArrayList of employees like( EId, Ename, Salary), etc. Now if we want to sort this list of employees based on the names of employees. Then that is not possible to sort using the Collections.sort() method. We need to provide something to the sort() function depending on what values we have to perform sorting. Then in that case a comparator is used.&lt;/p&gt;

&lt;p&gt;Comparator is the interface in java that contains the compare method. And by overloading the compare method, we can define that on what basis we need to compare the values.&lt;/p&gt;

&lt;h3&gt;
  
  
  What makes a HashSet different from a TreeSet?
&lt;/h3&gt;

&lt;p&gt;Although both HashSet and TreeSet are not synchronized and ensure that duplicates are not present, there are certain properties that distinguish a HashSet from a TreeSet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation:&lt;/strong&gt; For a HashSet, the hash table is utilized for storing the elements in an unordered manner. However, TreeSet makes use of the red-black tree to store the elements in a sorted manner.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Complexity/ Performance:&lt;/strong&gt; For adding, retrieving, and deleting elements, the time amortized complexity is O(1) for a HashSet. The time complexity for performing the same operations is a bit higher for TreeSet and is equal to O(log n). Overall, the performance of HashSet is faster in comparison to TreeSet.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Methods:&lt;/strong&gt; hashCode() and equals() are the methods utilized by HashSet for making comparisons between the objects. Conversely, compareTo() and compare() methods are utilized by TreeSet to facilitate object comparisons.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Objects type:&lt;/strong&gt; Heterogeneous and null objects can be stored with the help of HashSet. In the case of a TreeSet, runtime exception occurs while inserting heterogeneous objects or null objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the differences between HashMap and HashTable in Java?
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;HashMap&lt;/th&gt;
&lt;th&gt;HashTable&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;HashMap is not synchronized thereby making it better for non-threaded applications.&lt;/td&gt;
&lt;td&gt;HashTable is synchronized and hence it is suitable for threaded applications.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Allows only one null key but any number of null in the values.&lt;/td&gt;
&lt;td&gt;This does not allow null in both keys or values.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Supports order of insertion by making use of its subclass LinkedHashMap.&lt;/td&gt;
&lt;td&gt;Order of insertion is not guaranteed in HashTable.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  What are the different ways to create threads in Java?
&lt;/h3&gt;

&lt;p&gt;We can define and implement a thread in java using two ways: Extending the Thread class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ThreadExample extends Thread {  
   public void run(){  
       System.out.println("Thread runs...");  
   }  
   public static void main(String args[]){  
       ThreadExample ib = new ThreadExample();  
       ib.start();  
   }  
}

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

&lt;/div&gt;



&lt;p&gt;Implementing the Runnable interface&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ThreadExample implements Runnable {  
   public void run(){  
       System.out.println("Thread runs...");  
   }  
   public static void main(String args[]){  
       Thread ib = new Thread(new ThreadExample()); 
       ib.start();  
   }  
}

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

&lt;/div&gt;



&lt;p&gt;Implementing a thread using the method of Runnable interface is more preferred and advantageous as Java does not have support for multiple inheritances of classes. start() method is used for creating a separate call stack for the thread execution. Once the call stack is created, JVM calls the run() method for executing the thread in that call stack.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is difference between thread and process?
&lt;/h3&gt;

&lt;p&gt;A thread is a class in java that belongs to java.lang package. A thread is an lightweight process and has its own call stack. In Java, you can run multiple threads parallely. A thread is used to perform long running jobs dedicated without disturbing to the other part of program. Even if you don't create any new threads in your program, there is at least one thread i.e. main thread() which runs the application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Threads share the address space of the process that created it; processes have their own address.&lt;/li&gt;
&lt;li&gt;Threads have direct access to the data segment of its process; processes have their own copy of the data segment of the parent process.&lt;/li&gt;
&lt;li&gt;Threads can directly communicate with other threads of its process; processes must use interprocess communication to communicate with sibling processes.&lt;/li&gt;
&lt;li&gt;Threads have almost no overhead; processes have considerable overhead.&lt;/li&gt;
&lt;li&gt;New threads are easily created; new processes require duplication of the parent process.&lt;/li&gt;
&lt;li&gt;Threads can exercise considerable control over threads of the same process; processes can only exercise control over child processes.&lt;/li&gt;
&lt;li&gt;Changes to the main thread (cancellation, priority change, etc.) may affect the behavior of the other threads of the process; changes to the parent process do not affect child processes.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What are the different states of a thread's life cycle?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;New&lt;/strong&gt; - When a thread is instantiated it is in New state until the start() method is called on the thread instance. In this state the thread is not considered to be alive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Runnable&lt;/strong&gt; - The thread enters into this state after the start method is called in the thread instance. The thread may enter into the Runnable state from Running state. In this state the thread is considered to be alive.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Running&lt;/strong&gt; - When the thread scheduler picks up the thread from the Runnable thread's pool, the thread starts running and the thread is said to be in Running state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Waiting/Blocked/Sleeping&lt;/strong&gt; - In these states the thread is said to be alive but not runnable. The thread switches to this state because of reasons like wait method called or sleep method has been called on the running thread or thread might be waiting for some i/o resource so blocked. Dead - When the thread finishes its execution i.e. the run() method execution completes, it is said to be in dead state. A dead state can not be started again. If a start() method is invoked on a dead thread a runtime exception will occur.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What are different ways in which a thread can enter the waiting state?
&lt;/h3&gt;

&lt;p&gt;A thread can enter the waiting state by the following ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Invoking its sleep() method,&lt;/li&gt;
&lt;li&gt;By blocking on I/O&lt;/li&gt;
&lt;li&gt;By unsuccessfully attempting to acquire an object's lock&lt;/li&gt;
&lt;li&gt;By invoking an object's wait() method.&lt;/li&gt;
&lt;li&gt;It can also enter the waiting state by invoking its (deprecated) suspend() method.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is the difference between yield and sleep?
&lt;/h3&gt;

&lt;p&gt;When a task invokes its yield() method, it returns to the ready state, either from waiting, running or after its creation. When a task invokes its sleep() method, it returns to the waiting state from a running state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extending Thread class or implementing Runnable Interface. Which is better?
&lt;/h3&gt;

&lt;p&gt;You have two ways to create a thread in Java. First, making your class "extends" Thread class. The other way is making your class implement "Runnable" interface.&lt;/p&gt;

&lt;p&gt;The latter is more advantageous, cause when you are going for multiple inheritance, then only interface can help. If you are already inheriting a different class, then you have to go for Runnable Interface. Also, if you are implementing interface, it means you have to implement all methods in the interface.&lt;/p&gt;

&lt;p&gt;Both Thread class and Runnable interface are provided for convenience and use them as per the requirement. But if you are not extending any class, better extend Thread class as it will save few lines of coding. Otherwise performance wise, there is no distinguishable difference. A thread is in the ready state after it has been created and started.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is mutual exclusion? How can you take care of mutual exclusion using Java threads?
&lt;/h3&gt;

&lt;p&gt;Mutual exclusion in Java is a mechanism that allows only one thread to access a shared resource or critical section of code at a time, preventing race conditions where multiple threads can access and modify the same resource simultaneously.&lt;/p&gt;

&lt;p&gt;For mutual exclusion, you can simply use the &lt;code&gt;synchronized&lt;/code&gt; keyword, which can be applied to a method or a block of code to ensure that only one thread can access the synchronized code at a time.&lt;/p&gt;

&lt;p&gt;Java offers different options to achieve mutual exclusion, such as the &lt;code&gt;Semaphore&lt;/code&gt; class, the &lt;code&gt;ReadWriteLock&lt;/code&gt; interface, the &lt;code&gt;AtomicInteger&lt;/code&gt; class, and the &lt;code&gt;StampedLock&lt;/code&gt; class. The choice of implementation depends on the specific requirements and constraints of the application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Semaphore:&lt;/strong&gt; The Semaphore class can be used to implement mutual exclusion. It allows a limited number of threads to access a critical section at the same time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ReadWriteLock:&lt;/strong&gt; The ReadWriteLock interface can be used to implement mutual exclusion for read and write operations. It allows multiple threads to read a shared resource simultaneously, but only one thread can write to the resource at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AtomicInteger:&lt;/strong&gt; The AtomicInteger class can be used to implement mutual exclusion for integer values. It provides atomic operations such as incrementAndGet() and decrementAndGet(), which ensures that only one thread can modify the value at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;StampedLock:&lt;/strong&gt; The StampedLock class can be used to implement mutual exclusion for read and write operations. It provides optimistic read locks, which allows multiple threads to read a shared resource simultaneously, and a write lock, which allows only one thread to write to the resource at a time.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is the difference between preemptive scheduling and time slicing?
&lt;/h3&gt;

&lt;p&gt;Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then re-enters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is deadlock?
&lt;/h3&gt;

&lt;p&gt;When two threads are waiting for each other and can't proceed until the first thread obtains a lock on the other thread or vice versa, the program is said to be in a deadlock.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are synchronized methods and synchronized statements?
&lt;/h3&gt;

&lt;p&gt;Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a volatile keyword?
&lt;/h3&gt;

&lt;p&gt;In general, each thread has its own copy of the variable, such that one thread is not concerned with the value of the same variable in the other thread. But sometime this may not be the case. Consider a scenario in which the count variable is holding the number of times a method is called for a given class irrespective of any thread calling, in this case, irrespective of thread access the count has to be increased so the count variable is declared as volatile.&lt;/p&gt;

&lt;p&gt;The copy of the volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also has performance issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Serializable?
&lt;/h3&gt;

&lt;p&gt;Serializable is a marker interface. When an object has to be transferred over a network ( typically through RMI or EJB) or persists the state of an object to a file, the object Class needs to implement a Serializable interface. Implementing this interface will allow the object converted into a byte stream and transfer over a network.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is use of serialVersionUID?
&lt;/h3&gt;

&lt;p&gt;During object serialization, the default Java serialization mechanism writes the metadata about the object, which includes the class name, field names, types, and superclass. This class definition is stored as a part of the serialized object. This stored metadata enables the deserialization process to reconstitute the objects and map the stream data into the class attributes with the appropriate type every time an object is serialized the java serialization mechanism automatically computes a hash value.&lt;/p&gt;

&lt;p&gt;ObjectStreamClass’s computeSerialVersionUID() method passes the class name, sorted member names, modifiers, and interfaces to the secure hash algorithm (SHA), which returns a hash value. The serialVersionUID is also called suid.&lt;/p&gt;

&lt;p&gt;So when the serialized object is retrieved, the JVM first evaluates the suid of the serialized class and compares the suid value with the one of the objects. If the suid values match then the object is said to be compatible with the class and hence it is de-serialized. If not InvalidClassException exception is thrown.&lt;/p&gt;

&lt;p&gt;Changes to a serializable class can be compatible or incompatible.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is difference between ArrayList and Vector?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Synchronization&lt;/strong&gt;  – ArrayList is not thread-safe whereas Vector is thread-safe. In Vector class each method like &lt;code&gt;add()&lt;/code&gt;, &lt;code&gt;get(int i)&lt;/code&gt; is surrounded with a synchronized block and thus making Vector class thread-safe.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data growth&lt;/strong&gt;  – Internally, both the ArrayList and Vector hold onto their contents using an Array. When an element is inserted into an ArrayList or a Vector, the object will need to expand its internal array if it runs out of room. A Vector defaults to doubling the size of its array, while the ArrayList increases its array size by 50 percent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is difference between HashMap and HashTable?
&lt;/h3&gt;

&lt;p&gt;Both collections implement Map. Both collections store value as key-value pairs. The key differences between the two are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;

&lt;ol&gt;
&lt;li&gt;Hashmap is not synchronized in nature but HashTable is.&lt;/li&gt;
&lt;li&gt;Another difference is that the iterator in the HashMap is fail-safe while the enumerator for the HashTable isn’t. Fail-safe – if the HashTable is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove method, the iterator will throw a &lt;code&gt;ConcurrentModificationException&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;HashMap permits null values and only one null key, while Hashtable doesn’t allow key or value as null.&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  When to use ArrayList or LinkedList?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;For the ArrayList, doing random lookup using &lt;code&gt;get()&lt;/code&gt; is fast, but for LinkedList, it’s slow. It’s slow because there’s no efficient way to index into the middle of a linked list.&lt;/li&gt;
&lt;li&gt;When removing elements, using ArrayList is slow. This is because all remaining elements in the underlying array of Object instances must be shifted down for each remove operation. But here LinkedList is fast because deletion can be done simply by changing a couple of links.&lt;/li&gt;
&lt;li&gt;So an ArrayList works best for cases where you’re doing random access on the list, and a LinkedList works better if you’re doing a lot of editing in the middle of the list.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is final, finalize() and finally? What does it mean that a class or member is final?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;final&lt;/strong&gt; – Variables defined in an interface are implicitly final. A final class can’t be extended i.e., the final class may not be subclassed. This is done for security reasons with basic classes like String and Integer. It also allows the compiler to make some optimizations and makes thread safety a little easier to achieve. A final method can’t be overridden when its class is inherited. You can’t change the value of a final variable (which is a constant)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;finally&lt;/strong&gt; – a keyword used in exception handling will be executed whether or not an exception is thrown. For example, the closing of open connections is done in the final method.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;finalize&lt;/strong&gt; – helps in garbage collection. finalize() method is used just before an object is destroyed and garbage collected.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Method Overriding? What restrictions are placed on method overriding?
&lt;/h3&gt;

&lt;p&gt;When a class defines a method using the same name, return type, and argument list as that of a method in its superclass, the method in the subclass is said to override the method present in the Superclass. When the method is invoked for an object of the class, it is the new definition of the method that is called and not the method definition from the superclass. Restrictions placed on method overriding&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Overridden methods must have the same name, argument list, and return type.&lt;/li&gt;
&lt;li&gt;The overriding method may not limit the access of the method it overrides. Methods may be overridden to be more public, not more private.&lt;/li&gt;
&lt;li&gt;The overriding method may not throw any exceptions that may not be thrown by the overridden method.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is the difference between an Inner Class and a Sub-Class?
&lt;/h3&gt;

&lt;p&gt;An Inner class is a class that is nested within another class. An Inner class has access rights to the class which is nesting it and it can access all variables and methods defined in the outer class.&lt;/p&gt;

&lt;p&gt;A sub-class is a class that inherits from another class called a super class. Sub-class can access all public and protected methods and fields of its super class.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are the various access specifiers for Java classes?
&lt;/h3&gt;

&lt;p&gt;In Java, access specifiers are the keywords used before a class name that defines the access scope. The types of access specifiers for classes are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Public:&lt;/strong&gt; Class, Method, Field is accessible from anywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Protected:&lt;/strong&gt; Method, Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of the same package, but not from outside.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Default:&lt;/strong&gt; Method, Field, and class can be accessed only from the same package and not from outside of its native package.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Private:&lt;/strong&gt; Method, Field can be accessed from the same class to which they belong.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What?s the purpose of Static methods and static variables?
&lt;/h3&gt;

&lt;p&gt;When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keywords to make a method or variable shared for all objects.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is data encapsulation and what?s its significance?
&lt;/h3&gt;

&lt;p&gt;Encapsulation is a concept in Object Oriented Programming for combining properties and methods in a single unit.&lt;/p&gt;

&lt;p&gt;Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves the data hiding purpose.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a singleton class? Give a practical example of its usage.
&lt;/h3&gt;

&lt;p&gt;Singleton design pattern belongs to the creational family of patterns that governs the instantiation process. This pattern ensures at most one instance of a particular class is ever created in your application. Examples such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Project Configuration&lt;/strong&gt;: A class that reads your project configuration can be made Singleton. By making this singleton, you are allowing global access for all classes in your application. If the project configs are stored in a file, it just reads once and holds on the application cache. You don’t have to read the file multiple times.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Application Log:&lt;/strong&gt;  Logger will be used everywhere in your application. It must be initialized once and used everywhere.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Analytics and Reporting:&lt;/strong&gt; If you are using some kind of data analysis tool like Google Analytics, you will notice that they are designed to be a singleton. It initializes once and is used everywhere for each user action.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Read more about singleton implementation &lt;a href="https://stacktips.com/articles/singleton-design-pattern-in-java" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the difference between a continue and break statement?
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;break&lt;/code&gt; and &lt;code&gt;continue&lt;/code&gt; are two important keywords used in the loops. When a break keyword is used in a loop, the loop is broken instantly while when the continue keyword is used, the current iteration is broken and the loop continues with the next iteration.&lt;/p&gt;

&lt;p&gt;In the below example, the Loop is broken when the counter reaches 4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (counter = 0; counter&amp;lt; 10; counter++)
    system.out.println(counter);

    if (counter == 4) {
        break;
    }
}

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

&lt;/div&gt;



&lt;p&gt;In the below example when the counter reaches 4, the loop jumps to the next iteration and any statements after the continue keyword are skipped for the current iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (counter = 0; counter &amp;lt; 10; counter++)
    system.out.println(counter);
    if (counter == 4) {
        continue;
    }
}

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is the difference between double and float variables in Java?
&lt;/h3&gt;

&lt;p&gt;In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float is single-precision floating-point decimal number while Double is a double-precision decimal number.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the Final Keyword in Java?
&lt;/h3&gt;

&lt;p&gt;A variable declared with the final keyword is a constant in Java. Value can be assigned only once and after the assignment, the value of a constant can’t be changed.&lt;/p&gt;

&lt;p&gt;For example, a constant with the name MAX_LIMIT is declared and assigned value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;private final int MAX_LIMIT=100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When a method is declared as final, it can NOT be overridden by the subclasses. This method is faster than any other method because they are resolved at the complie time.&lt;/p&gt;

&lt;p&gt;When a class is declared as final, it cannot be inherited. Example String, Integer, and other wrapper classes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can we declare a class as Abstract without having any abstract method?
&lt;/h3&gt;

&lt;p&gt;Yes, we can create an abstract class by using the abstract keywords before the class names even if it doesn’t have any abstract method. However, if a class has even one abstract method, it must be declared as abstract otherwise it will give an error.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the difference between an Abstract Class and an Interface in Java?
&lt;/h3&gt;

&lt;p&gt;The primary difference between an abstract class and an interface is that an interface can only possess a declaration of public static methods with no concrete implementation while an abstract class can have members with any access specifiers (public, private, etc) with or without a concrete implementation.&lt;/p&gt;

&lt;p&gt;Another key difference in the use of abstract classes and interfaces is that a class that implements an interface must implement all the methods of the interface while a class that inherits from an abstract class doesn’t require the implementation of all the methods of its super class.&lt;/p&gt;

&lt;p&gt;A class can implement multiple interfaces but it can extend only one abstract class.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can we declare the main method of our class as private?
&lt;/h3&gt;

&lt;p&gt;In Java, the main method must be public static in order to run any application correctly. If the main method is declared as private, the developer won’t get any compilation error however, it will not get executed and will give a runtime error.&lt;/p&gt;

&lt;h3&gt;
  
  
  How we can execute any code even before main method?
&lt;/h3&gt;

&lt;p&gt;If we want to execute any statements before even the creation of objects at the load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before the creation of objects in the main method.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Originally published at &lt;a href="https://stacktips.com/articles/most-popular-java-interview-questions-and-answers" rel="noopener noreferrer"&gt;stacktips.com&lt;/a&gt;&lt;/small&gt; &lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.stacktips.com%2Fmedia%2Fuploads%2Fstacktips-banner.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.stacktips.com%2Fmedia%2Fuploads%2Fstacktips-banner.png" alt="Most Popular Java Interview Questions and Answers"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>interview</category>
    </item>
    <item>
      <title>7 Blogging Mistakes I Wish I Had Known Before I Started</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Sat, 30 Sep 2023 17:12:12 +0000</pubDate>
      <link>https://forem.com/nilan/7-blogging-mistakes-i-wish-i-had-known-before-i-started-o54</link>
      <guid>https://forem.com/nilan/7-blogging-mistakes-i-wish-i-had-known-before-i-started-o54</guid>
      <description>&lt;p&gt;My personal journey in tech blogging has been a roller coaster ride. I have been blogging ever since I entered the IT industry as a new entrant in 2008. It was driven by the passion for learning new technology and exploring stuff that is contrary to my regular day-to-day job.&lt;/p&gt;

&lt;p&gt;So when I was working as an Android application developer, in my personal time I was trying to learn J2EE and Apache Struts. And, I love taking notes along the way while doing small and simple POCs.&lt;/p&gt;

&lt;p&gt;At that time there was no application like Notion, so I decided to create a website using Google Blogspot and started capturing all my notes there. Those notes mainly served as a bookmark, so I could refresh my memory whenever I wanted.&lt;/p&gt;

&lt;p&gt;Slowly, I started to like writing detailed tutorials and sharing my knowledge which some of my friends, and colleagues loved to read.&lt;/p&gt;

&lt;p&gt;And that is how I was introduced to blogging.&lt;/p&gt;

&lt;p&gt;But, I never wanted to be a full-time blogger. I never believed I had that spark, which would make my livelihood out of it. That made me a passive blogger.&lt;/p&gt;

&lt;p&gt;Now I am running this blog &lt;a href="https://stacktips.com/"&gt;stacktips.com&lt;/a&gt;. It is a custom-built site, using Python, Django, and VueJS. &lt;/p&gt;

&lt;h2&gt;
  
  
  7 Blogging Mistakes I wish I had Known Before
&lt;/h2&gt;

&lt;p&gt;Over the years, I've had my fair share of experiences as a passive blogger, and through trial and error, I've learned valuable lessons that I wish I had known earlier.&lt;/p&gt;

&lt;p&gt;In this blog post, I'll share these insights with you, in the hope that they'll help you on your own blogging journey.&lt;/p&gt;

&lt;p&gt;This post is probably too long for you. You don't need to spend all your time reading this unless you really want to hear my story.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let’s cut to the chase:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Content is the king, but consistency is the key to success!&lt;/li&gt;
&lt;li&gt;Building a distribution channel is more important than building a great site.&lt;/li&gt;
&lt;li&gt;You don't need a perfect domain or perfect site design. No one gives a shit about your domain name or site design as long as you have the X factor in your content.&lt;/li&gt;
&lt;li&gt;Understand your niche. Don't try to be a jack of all, you may appear as a jackass.&lt;/li&gt;
&lt;li&gt;SEO is the secret weapon. The earlier you realize the better for your site growth.&lt;/li&gt;
&lt;li&gt;Engagements are more important than having a larger number of subscribers/followers.&lt;/li&gt;
&lt;li&gt;Social media can help you to create the hype but a simple mistake can fuck up your whole strategy. So use it cautiously.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Only, for not-so-busy people:
&lt;/h3&gt;

&lt;p&gt;Nothing new here, but I expand on these above topics.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Content is the King, but Consistency is the Key to Success!
&lt;/h3&gt;

&lt;p&gt;Great content is essential for any successful blog, this is a must-have. However, consistency is equally important. You need to strike the right balance between quality and quantity.&lt;/p&gt;

&lt;p&gt;As a lone worrier, I used to dedicate a few hours over a weekend to produce 2/3 articles for my blog. But I never realized that we need to be consistent in publishing stories. I used to publish 3/4 articles in a day and then nothing for the rest of the month.&lt;/p&gt;

&lt;p&gt;Publishing many articles a day, makes your readers overwhelmed with content so it is very unlikely that all your stories will be read by everyone. But publishing a story on a regular interval, let’s say every Sunday, will definitely drive more engagement, and your readers will love to spend a few minutes reading a story per week.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Building a Distribution Channel is More Important than Building a Great Site
&lt;/h3&gt;

&lt;p&gt;Great content is like a hidden treasure, it is worth only when discovered by your readers. Without having an effective distribution strategy no one will ever find what you write.&lt;/p&gt;

&lt;p&gt;You need to plan your distribution strategy including content syndication, building email lists, social media groups, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. You Don't Need a Perfect Domain or Perfect Site Design
&lt;/h3&gt;

&lt;p&gt;Many aspiring bloggers get caught up in the quest for the perfect domain name or website design. While these factors are important, they are not as critical as the quality and uniqueness of your content.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Understand Your Niche
&lt;/h3&gt;

&lt;p&gt;Trying to cover too many topics can dilute your blog's identity and make you appear unfocused. Find out why it's crucial to understand your niche and become an authority in it.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. SEO is the Secret Weapon
&lt;/h3&gt;

&lt;p&gt;Search Engine Optimization (SEO) can be a game-changer for your blog's growth and to drive organic traffic to your blog. While SEO strategy might sound easy when you Google these topics, there is a vast amount of science behind it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some of these include:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Having a catchy page title (not clickbait) and page description.&lt;/li&gt;
&lt;li&gt;Maintain a structural hierarchy of your content on the page&lt;/li&gt;
&lt;li&gt;Canonical and SEO-friendly page links&lt;/li&gt;
&lt;li&gt;Backlinks and building trustworthiness&lt;/li&gt;
&lt;li&gt;Page loading speed&lt;/li&gt;
&lt;li&gt;Image and video annotations and structured content markup&lt;/li&gt;
&lt;li&gt;Submit the site to search engines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are just a few of these SEO strategies to start with.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Engagements are More Important than a Large Number of Subscribers/Followers
&lt;/h3&gt;

&lt;p&gt;It's not just about the numbers. Don't always throw your stuff, expand your social posts to other stuff including a photo from your pub night or day out with your kids. People love to connect with humans more than a BOT🤖.&lt;/p&gt;

&lt;p&gt;Engaging with your audience, building a community, and nurturing relationships can be more valuable than having a massive but passive following.&lt;/p&gt;

&lt;h3&gt;
  
  
  7. Social Media: Hype and Caution
&lt;/h3&gt;

&lt;p&gt;Social media can be a double-edged sword. Learn how to leverage its potential to create excitement around your blog while avoiding common pitfalls that can harm your strategy.&lt;br&gt;
In Summary,&lt;/p&gt;

&lt;p&gt;I am not a very successful blogger. I never made direct money out of my blogging. However, blogging enabled me to expand my horizons and build connections. Using my blogging connections I have published books, built projects, and earned sufficient money to call out “My Blogging Journey has been Worthwhile!”.&lt;/p&gt;

&lt;p&gt;Happy blogging! &lt;/p&gt;

</description>
      <category>blogging</category>
      <category>writing</category>
    </item>
    <item>
      <title>Getting Started with Spring Boot: A Beginner’s Guide</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Mon, 18 Sep 2023 14:06:16 +0000</pubDate>
      <link>https://forem.com/nilan/getting-started-with-spring-boot-a-beginners-guide-3e5h</link>
      <guid>https://forem.com/nilan/getting-started-with-spring-boot-a-beginners-guide-3e5h</guid>
      <description>&lt;h2&gt;
  
  
  What is Spring Boot?
&lt;/h2&gt;

&lt;p&gt;Spring Boot is a popular, open-source, enterprise-level framework for creating standalone, production-grade applications that run on the Java Virtual Machine (JVM).&lt;/p&gt;

&lt;p&gt;It requires minimal or zero configuration and is easy to get started. It is a widely popular choice among Java developers for developing MicroServices and web applications.&lt;/p&gt;

&lt;p&gt;Spring boot is built on top of the popular Spring Framework and inherits features like dependency injection(DI) or Inversion of Control (IoC) from the Spring.&lt;/p&gt;

&lt;p&gt;It offers built-in support for typical tasks that an application needs to perform, such as data binding, type conversion, validation, exception handling, resource and event management, internationalization, and more.&lt;/p&gt;

&lt;p&gt;👇&lt;strong&gt;Chapter-1 video course(more chapters dropping)&lt;/strong&gt;👇&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/GX--xvYOlQ0"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  What we can do with Spring Boot?
&lt;/h2&gt;

&lt;p&gt;Spring Boot framework provides several features that make it ideal for building a variety of applications, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web applications:&lt;/strong&gt; You can create simple to complex multi-tier web applications&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RESTful APIs:&lt;/strong&gt; Spring Boot makes it easy to create RESTful APIs to expose your data and functionality to other applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microservices:&lt;/strong&gt; Widely used choice for building small, independent services that can be easily scaled and deployed.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Batch processing:&lt;/strong&gt; You can build a Spring batch application that can process large volumes of data in a scheduled or event-driven manner. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Command line applications:&lt;/strong&gt; Spring Boot also can be used to build applications that run on the command line.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Key Features of Spring Boot
&lt;/h2&gt;

&lt;p&gt;Some of the key features of the Spring Boot framework are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auto-configuration&lt;/li&gt;
&lt;li&gt;Dependency Injection (DI)&lt;/li&gt;
&lt;li&gt;Spring Boot Starter Dependencies&lt;/li&gt;
&lt;li&gt;Embedded Server&lt;/li&gt;
&lt;li&gt;Production-ready features&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Auto Configuration
&lt;/h3&gt;

&lt;p&gt;Spring Boot applications are initialized with pre-set dependencies so you don’t have to configure the underlying Spring Framework and third-party packages manually for your application.&lt;/p&gt;

&lt;p&gt;This helps developers to reduce the number of errors caused due to misconfiguration and makes them more productive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency Injection
&lt;/h3&gt;

&lt;p&gt;Dependency Injection is one of the core features of the Spring framework. This allows developers to write loosely coupled code that is easy to maintain and test.&lt;/p&gt;

&lt;p&gt;Dependency Injection (DI) is a software design pattern used in object-oriented programming, where the dependencies of a class are provided by an external entity rather than being created within the class itself.&lt;/p&gt;

&lt;p&gt;In other words, instead of a class creating its dependencies, those dependencies are "injected" into the class from outside.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spring Boot Starter Dependencies
&lt;/h3&gt;

&lt;p&gt;Dependency management is one of the critical parts of any project development. Managing this manually is very cumbersome and error-prone. Spring Boot provides us with several Spring Boot starter packages to address this problem.&lt;/p&gt;

&lt;p&gt;There are two types of dependencies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Direct:&lt;/strong&gt;  These are dependencies defined in your &lt;code&gt;pom.xml&lt;/code&gt; or &lt;code&gt;build.gradle&lt;/code&gt; file under the &lt;code&gt;dependencies&lt;/code&gt; section.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Transitive:&lt;/strong&gt;  These are dependencies that are dependencies of your direct dependencies. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, if we add the &lt;code&gt;spring-boot-starter-web&lt;/code&gt; as a dependency to your &lt;code&gt;pom.xml&lt;/code&gt; file then will download &lt;code&gt;spring-boot-starter-tomcat&lt;/code&gt; as the direct dependency and with that, it will also download the other transitive dependencies like &lt;code&gt;tomcat-embedded-core&lt;/code&gt; and &lt;code&gt;tomcat-embedded-el&lt;/code&gt; and &lt;code&gt;tomcat-embedded-websocket&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.stacktips.com%2Fmedia%2Fuploads%2Fsummernote%2FSpring_Boot_Starter_Dependencies.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.stacktips.com%2Fmedia%2Fuploads%2Fsummernote%2FSpring_Boot_Starter_Dependencies.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you’re managing the dependencies manually, then you must be very careful about the specific version of the dependency you are using and also need to worry about their compatibility with each other.&lt;/p&gt;

&lt;p&gt;The built-in Spring Boot starters help you to simplify the build configuration and make the development very rapid and easy.&lt;/p&gt;

&lt;p&gt;Starter dependencies are a set of convenient dependency descriptors that we can use to bootstrap our Spring boot applications. The Spring Boot starters contain a lot of pre-defined dependencies with a supported set of transitive dependencies.&lt;/p&gt;

&lt;p&gt;Spring Boot starters follow a similar naming pattern, e.g. &lt;code&gt;spring-boot-starter-XYZ&lt;/code&gt;, where XYZ denotes a particular type of application.&lt;/p&gt;

&lt;p&gt;Here are some of the popular Spring Boot starter packages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-web&lt;/strong&gt;: It is used for building web applications, including RESTful applications using Spring MVC. It uses Tomcat as the default embedded container. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-activemq&lt;/strong&gt;: It is used in JMS messaging using Apache ActiveMQ. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-actuator&lt;/strong&gt;: Provides production-ready features such as health checks, monitoring, and other application management features. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-batch&lt;/strong&gt;: It is used for the Spring Batch. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-cache&lt;/strong&gt;: It is used for Spring Framework's caching support. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-data-jpa&lt;/strong&gt;: It is used for Spring Data JPA with Hibernate. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-data-mongodb&lt;/strong&gt;: It is used for MongoDB document-oriented database and Spring Data MongoDB. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;spring-boot-starter-mail&lt;/strong&gt;: It is used to support Java Mail and Spring Framework's email sending. &lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Embedded Servers
&lt;/h3&gt;

&lt;p&gt;Spring Boot includes support for embedded Tomcat, Jetty and Undertow servers. This means you don’t need any external web servers and no need to deploy WAR files anymore.&lt;/p&gt;

&lt;p&gt;The embedded Tomcat server is available through the &lt;code&gt;spring-boot-starter-web&lt;/code&gt; dependency.&lt;/p&gt;

&lt;p&gt;However, if you want Jetty or Undertow servers then you can include &lt;code&gt;spring-boot-starter-jetty&lt;/code&gt; or &lt;code&gt;spring-boot-starter-undertow&lt;/code&gt; dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Production Ready Features
&lt;/h3&gt;

&lt;p&gt;Spring Boot Framework includes production-ready features such as metrics, health checks, and external configuration management. Spring Boot also provides integration with various enterprise technologies such as RMI, JPA and JMS, AMQP, etc.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;RMI&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Hibernate&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WebSocket API (&lt;a href="https://www.jcp.org/en/jsr/detail?id=356" rel="noopener noreferrer"&gt;JSR 356&lt;/a&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;AMQP - Advanced Message Queuing Protocol&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Java Web Services&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JPA () - Java Persistence API&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JMS&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spring also provides a Model-View-Controller (MVC) framework that simplifies the development of web applications by separating the presentation layer from the business logic.&lt;/p&gt;

&lt;p&gt;It also provides a number of tools and frameworks for testing applications, including the Spring Test Framework and the Spring MVC Test Framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  Spring Boot vs. Spring: What Works for You?
&lt;/h2&gt;

&lt;p&gt;Ultimately, the best framework for you will depend on your specific requirements. If you are not sure which framework to choose, I recommend starting with Spring Boot.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Features&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;Spring&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Spring Boot&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Source type&lt;/td&gt;
&lt;td&gt;It is an open-source framework.&lt;/td&gt;
&lt;td&gt;It is built on top of a spring.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Core features&lt;/td&gt;
&lt;td&gt;Dependency injection&lt;/td&gt;
&lt;td&gt;Auto-configuration is an essential feature.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deployment&lt;/td&gt;
&lt;td&gt;The server is set explicitly for spring. The deployment descriptor is required.&lt;/td&gt;
&lt;td&gt;Embedded servers are provided. The deployment descriptor is not required.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use cases&lt;/td&gt;
&lt;td&gt;Good for complex applications.&lt;/td&gt;
&lt;td&gt;Good for rapid application development&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Setup time&lt;/td&gt;
&lt;td&gt;It takes more time to set up.&lt;/td&gt;
&lt;td&gt;Faster to set up.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Spring Boot is a great choice to get started with Spring development without having to worry about the complexities of the Spring Framework. Once you understand Spring better, you can decide if you need the additional features and flexibility of the Spring Framework.&lt;/p&gt;

&lt;p&gt;&lt;small&gt;Originally published at &lt;a href="https://stacktips.com/courses/spring-boot-beginners-guide/introduction-to-spring-boot-framework" rel="noopener noreferrer"&gt;stacktips.com&lt;/a&gt;&lt;/small&gt;&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>spring</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Hit 1000 followers on DEV 🎉 🎉 🎉</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Wed, 23 Aug 2023 15:53:52 +0000</pubDate>
      <link>https://forem.com/nilan/hit-1000-followers-on-dev-p10</link>
      <guid>https://forem.com/nilan/hit-1000-followers-on-dev-p10</guid>
      <description>&lt;p&gt;Woohoo!&lt;/p&gt;

&lt;p&gt;I have hit 1000 followers in DEV.to today 🎉. Thank you for your interest in my articles!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LJTXUcHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxa0iwlh82u6dsteiti6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LJTXUcHJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxa0iwlh82u6dsteiti6.png" alt="Image description" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I will try to write more articles, tutorials in dev.to and add value to the community.&lt;/p&gt;

&lt;p&gt;Thank you all💖🙏&lt;/p&gt;

</description>
      <category>meta</category>
      <category>blog</category>
      <category>writing</category>
    </item>
    <item>
      <title>Getting Started with Maven: A Beginner's Guide to Java Build Automation</title>
      <dc:creator>Nilanchal</dc:creator>
      <pubDate>Mon, 21 Aug 2023 22:39:41 +0000</pubDate>
      <link>https://forem.com/nilan/getting-started-with-maven-a-beginners-guide-to-java-build-automation-41f2</link>
      <guid>https://forem.com/nilan/getting-started-with-maven-a-beginners-guide-to-java-build-automation-41f2</guid>
      <description>&lt;p&gt;Maven is a popular build automation tool used primarily for Java projects. It is designed to manage a project's build process, including the managing dependencies, packaging, deployment and generating reports.&lt;/p&gt;

&lt;p&gt;Maven uses an XML file called a Project Object Model &lt;code&gt;pom.xml&lt;/code&gt; to define the project's configuration, including its dependencies, plugins, and other settings. &lt;/p&gt;

&lt;p&gt;The POM file is typically stored in the project's root directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Maven?
&lt;/h2&gt;

&lt;p&gt;Maven is a popular build tool for Java projects that provides a number of benefits, including:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dependency management:&lt;/strong&gt; Maven makes it easy to manage dependencies for your project, including downloading and including libraries from remote repositories.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Build automation:&lt;/strong&gt; Maven automates the build process, including compiling the code, running tests, and packaging the application into a distributable format.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Consistent project structure:&lt;/strong&gt; Maven provides a standard project structure that makes it easy to organize your code and resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Plugin system:&lt;/strong&gt; Maven has a powerful plugin system that can be used to extend the functionality of the build process, such as generating documentation or deploying the application to a server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easy project setup:&lt;/strong&gt; Maven provides archetypes that can be used to quickly set up a new project with a predefined structure and dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reproducible builds:&lt;/strong&gt; Maven provides a way to create reproducible builds, which means that the build process is consistent across different machines and environments.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integration with IDEs:&lt;/strong&gt; Maven integrates with popular Java IDEs, such as Eclipse and IntelliJ, making it easy to import and work with Maven projects.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Overall, Maven can help improve the productivity of developers and make it easier to manage complex Java projects by automating common tasks and providing a standardized way to structure and build projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Identifiers in Maven?
&lt;/h2&gt;

&lt;p&gt;Maven projects are identified by 3 main identifiers/coordinates:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Group Id:&lt;/strong&gt; Unique identifier for the organization. Typically, this is a reversed domain name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Artifact Id:&lt;/strong&gt; Used to generate the build (JAR /WAR) files.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version:&lt;/strong&gt; A version number e.g 1.0.0&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Maven Repositories
&lt;/h2&gt;

&lt;p&gt;Maven uses a centralised repository to manage project dependencies. The Maven Repository is divided into three types: Local, Central and Remote.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Maven Local Repository&lt;/strong&gt;&lt;br&gt;
The local repository is a directory on the developer's machine where all the dependencies for a specific project are stored. &lt;/p&gt;

&lt;p&gt;The local repository in Maven is usually located in the user's home directory under the .m2 folder.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Central Maven Repository&lt;/strong&gt;&lt;br&gt;
 The Central Repository is the default remote repository used by Maven.&lt;/p&gt;

&lt;p&gt;It contains a vast collection of open-source libraries and dependencies, making it a one-stop shop for most projects. When Maven needs to download a dependency, it first looks in the local repository and then in the Central Repository.&lt;/p&gt;

&lt;p&gt;The central maven repository is managed by Sonatype. There are almost all java libraries that exist in it.&lt;/p&gt;

&lt;p&gt;When building a project, if it can not find any dependent jars in the local maven repository it will search in the central maven repository and download those JARs if found. You can also search for an artifact in central.sonatype.com  if you want to download the artifact’s pom or jars.&lt;/p&gt;

&lt;p&gt;To search/browse in the central repository you can use: central.sonatype.com&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Remote Repository&lt;/strong&gt; is a repository of dependencies located on a remote server, typically managed  by third party organizations.&lt;/p&gt;

&lt;p&gt;Here's an example of how to use a remote Maven repository in a Java project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;repositories&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;repository&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;id&amp;gt;&lt;/span&gt;reporitory-id&lt;span class="nt"&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;name&amp;gt;&lt;/span&gt;Repository Name&lt;span class="nt"&gt;&amp;lt;/name&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;url&amp;gt;&lt;/span&gt;your remote repository url&lt;span class="nt"&gt;&amp;lt;/url&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/repository&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/repositories&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every dependency defined in &lt;code&gt;pom.xml&lt;/code&gt; file, it first search dependency in the local repository, followed by the remote repositories in the same order as defined in the &lt;code&gt;pom.xml&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;If the dependency is not found in any of the remote repositories, Maven reports a build error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating Maven Java Project using CommandLine
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a new project directory:
Create a new directory for your project. For example, you can create a directory called "my-maven-project".
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir my-maven-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Navigate to the project directory:
Navigate to the newly created directory.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd my-maven-project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Create a new Maven project:
Create a new Maven project using the &lt;code&gt;mvn archetype:generate&lt;/code&gt; command. This command generates a new Maven project based on a template or archetype.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mvn archetype:generate 
    -DgroupId=com.example.app 
    -DartifactId=my-app 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By the way, you don’t have to remember each of these parameters provided above. Just use the &lt;code&gt;interactiveMode=true&lt;/code&gt;, so that Maven asks for all the required parameters.&lt;/p&gt;

&lt;p&gt;In this command, the &lt;code&gt;groupId&lt;/code&gt; and &lt;code&gt;artifactId&lt;/code&gt; are the unique identifiers for your project. The &lt;code&gt;archetypeArtifactId&lt;/code&gt; specifies the archetype to use, which in this case is the &lt;code&gt;maven-archetype-quickstart&lt;/code&gt; archetype, which creates a simple Java project with a main class.&lt;/p&gt;

&lt;p&gt;Learn more about Maven build process, repository and build profiling here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/introduction-to-maven"&gt;c01-Introduction to Maven&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/introduction-to-maven#install-and-configure-maven-on-windows"&gt;c02-Install and configure Maven on Windows&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/introduction-to-maven#install-and-configure-maven-on-macos"&gt;c03-Install and configure Maven on macOS?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/introduction-to-maven-repository"&gt;c04-Maven Repository Introduction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/creating-maven-java-project-using-commandline"&gt;c05-Creating Maven Java Project using CommandLine&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/understanding-maven-project-dependencies"&gt;c06-Understanding Maven Project and Dependencies&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/maven-build-lifecycles"&gt;c07-Maven Build Lifecycles&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/maven-commands-cheat-sheet"&gt;c08-Popular Maven Commands&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stacktips.com/courses/maven-for-beginners/working-with-profiles-in-maven"&gt;c09-Working with Profiles in Maven&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion and Wrap Up
&lt;/h2&gt;

&lt;p&gt;Whether you're a beginner or an experienced developer, this articles will help you to up to speed with Maven.&lt;/p&gt;

</description>
      <category>maven</category>
      <category>java</category>
      <category>tooling</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
