<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oum</title>
    <description>The latest articles on DEV Community by Oum (@oumgit).</description>
    <link>https://dev.to/oumgit</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%2F911315%2Fc37135b2-8a8b-4bc2-b80a-b727dbb0bdf4.png</url>
      <title>DEV Community: Oum</title>
      <link>https://dev.to/oumgit</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oumgit"/>
    <language>en</language>
    <item>
      <title>Auditing in Spring Boot</title>
      <dc:creator>Oum</dc:creator>
      <pubDate>Fri, 19 Aug 2022 17:03:02 +0000</pubDate>
      <link>https://dev.to/oumgit/auditing-in-spring-boot-55hk</link>
      <guid>https://dev.to/oumgit/auditing-in-spring-boot-55hk</guid>
      <description>&lt;h2&gt;
  
  
  What's auditing?
&lt;/h2&gt;

&lt;p&gt;Auditing means tracking and logging transactions related to data, which simply means logging insert, update and delete operations (user and/or date of action).&lt;/p&gt;

&lt;h2&gt;
  
  
  How to add creation/update date of an @Entity in Spring?
&lt;/h2&gt;

&lt;p&gt;This can be achieved using different approaches:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database built-in solutions: Oracle Database 12c, Db2, MySQL Enterprise Audit...&lt;/li&gt;
&lt;li&gt;Creating database triggers,&lt;/li&gt;
&lt;li&gt;Use a third party tool,&lt;/li&gt;
&lt;li&gt;Create columns of update and create for each entity, and log every change manually to the database (write it completely on your own),&lt;/li&gt;
&lt;li&gt;Using Spring Auditing 💡&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This last choice doesn't require touching our entities business logic to add tracking logic, and doesn't require adding additional columns to the entity or additional tables to log changes. &lt;/p&gt;

&lt;p&gt;A headless solution to track your entity, if you are using Spring. We configure it once and use it everywhere:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Spring Data provides sophisticated support to transparently keep track of who created or changed an entity and the point in time this happened. (spring)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Implementation
&lt;/h2&gt;

&lt;p&gt;The possible approaches:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Implementation using standard JPA:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Uses the entity's table itself to log changes, in this case we can't audit delete operations.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;2. Implementation using auditing functionality provided by Hibernate:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;It logs into tables other than the entity's table, which allows to log delete operations.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;3. Implementation using auditing functionality provided by Spring Data JPA💡:&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;Provides handy annotations for auditing properties, ready for integration with Spring Security, and also cannot be used to log delete operations since it inherits the same flaws of the JPA approach.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next we will cover the implementation of auditing with Spring Data JPA.&lt;/p&gt;

&lt;p&gt;📝 Checkpoints/Todos:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Auditing Author Using AuditorAware and Spring Security (allows adding &lt;code&gt;@CreatedBy&lt;/code&gt;, and &lt;code&gt;@LastModifiedBy&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Enable JPA Auditing by Using &lt;code&gt;@EnableJpaAuditing&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create Generic Auditable Class with Spring Data Annotations &lt;code&gt;@CreatedDate&lt;/code&gt;, and &lt;code&gt;@LastModifiedDate&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Add Extends from Auditable Class to the entity we want to track.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1- Create &lt;code&gt;AuditorAware&lt;/code&gt; implementation that will be used to configure auditing:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.domain.AuditorAware&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Optional&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;AuditorAwareImpl&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;AuditorAware&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="o"&gt;{&lt;/span&gt;

        &lt;span class="c1"&gt;// Returning empty instead of user since we're tracking just dates not users&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;Optional&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="nf"&gt;getCurrentAuditor&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;Optional&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;empty&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Use the following to get the actual connected user&lt;/span&gt;
        &lt;span class="cm"&gt;/*
    public User getCurrentAuditor() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication == null || !authentication.isAuthenticated()) {
            return null;
        }

        return ((MyUserDetails) authentication.getPrincipal()).getUser();
    }
    */&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2- Enable auditing, by adding this configuration class to the spring boot project's configuration directory:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Bean&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Configuration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.domain.AuditorAware&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.jpa.repository.config.EnableJpaAuditing&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableJpaAuditing&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;auditorAwareRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"auditorProvider"&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;JpaAuditingConfig&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Bean&lt;/span&gt;
    &lt;span class="nc"&gt;AuditorAware&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="nf"&gt;auditorProvider&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="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;AuditorAwareImpl&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;&lt;strong&gt;3- Once auditing is enabled we create the Auditable class, that we will extend wherever we want (in our model classes):&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;lombok.AllArgsConstructor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;lombok.Data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;lombok.NoArgsConstructor&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.annotation.CreatedDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.annotation.LastModifiedDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.data.jpa.domain.support.AuditingEntityListener&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.persistence.Column&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.persistence.EntityListeners&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;javax.persistence.MappedSuperclass&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.util.Date&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Data&lt;/span&gt;
&lt;span class="nd"&gt;@AllArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@MappedSuperclass&lt;/span&gt;
&lt;span class="nd"&gt;@EntityListeners&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;AuditingEntityListener&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;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Auditable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;U&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@CreatedDate&lt;/span&gt;
    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"created_date"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;createdDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nd"&gt;@LastModifiedDate&lt;/span&gt;
    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"last_modified_date"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;protected&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="n"&gt;lastModifiedDate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// To track the user that modified or created use @LastModifiedBy and @CreatedBy&lt;/span&gt;

    &lt;span class="c1"&gt;//@LastModifiedBy&lt;/span&gt;
    &lt;span class="c1"&gt;//protected U lastModifiedBy;&lt;/span&gt;

    &lt;span class="c1"&gt;//@CreatedBy&lt;/span&gt;
    &lt;span class="c1"&gt;//protected U createdBy;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4- Last, extend from Auditable class to track changes, 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;@Data&lt;/span&gt;
&lt;span class="nd"&gt;@Entity&lt;/span&gt;
&lt;span class="nd"&gt;@NoArgsConstructor&lt;/span&gt;
&lt;span class="nd"&gt;@Table&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"products"&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;Product&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Auditable&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="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Id&lt;/span&gt;
    &lt;span class="nd"&gt;@Column&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nd"&gt;@GeneratedValue&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nc"&gt;GenerationType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;AUTO&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;Long&lt;/span&gt; &lt;span class="n"&gt;id&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 columns &lt;code&gt;created_date&lt;/code&gt; and &lt;code&gt;last_modified_date&lt;/code&gt; are automatically added to &lt;code&gt;products&lt;/code&gt; table in the database, and since &lt;code&gt;Product&lt;/code&gt; class extends &lt;code&gt;Auditable&lt;/code&gt; class the &lt;code&gt;createdDate&lt;/code&gt; and &lt;code&gt;lastModifiedDate&lt;/code&gt; attributes can be used in the same way &lt;code&gt;Product&lt;/code&gt;'s other attributes are used.&lt;/p&gt;

&lt;p&gt;More on:&lt;br&gt;
&lt;a href="https://www.baeldung.com/database-auditing-jpa"&gt;Official docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>auditing</category>
      <category>springboot</category>
      <category>spring</category>
      <category>jpa</category>
    </item>
    <item>
      <title>Create an asynchronous method in Spring Boot</title>
      <dc:creator>Oum</dc:creator>
      <pubDate>Thu, 18 Aug 2022 19:17:54 +0000</pubDate>
      <link>https://dev.to/oumgit/create-an-asynchronous-method-in-spring-boot-4fkn</link>
      <guid>https://dev.to/oumgit/create-an-asynchronous-method-in-spring-boot-4fkn</guid>
      <description>&lt;p&gt;Normally, a given program runs straight along, with only one thing happening at once. And a block of code that relies on the previous one, should wait for it to end executing, and until that happens everything is stopped from the perspective of the user.&lt;/p&gt;

&lt;p&gt;Let’s say we want to make a call to an API, knowing that the service we are calling takes too much time. So we prefer to return a response, telling that the call was made and the processing has started.&lt;/p&gt;

&lt;p&gt;You might be wondering either the process succeeded or not, so instead of waiting for a single call to end, you can log everything and check on it later or make another service that returns the result (if you are really interested).&lt;/p&gt;

&lt;h1&gt;
  
  
  How to create an asynchronous method in Spring Boot
&lt;/h1&gt;

&lt;h2&gt;
  
  
  1. Enabling Async Support:
&lt;/h2&gt;

&lt;p&gt;To enable asynchronous processing, with Java configuration, simply add the &lt;code&gt;@EnableAsync&lt;/code&gt; to a configuration class:&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="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.context.annotation.Configuration&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.scheduling.annotation.EnableAsync&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@Configuration&lt;/span&gt;
&lt;span class="nd"&gt;@EnableAsync&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;SpringAsyncConfig&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 enable annotation is enough, but there is more options you can add as well (like specifying the &lt;code&gt;ThreadPoolTaskExecutor&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Making your service asynchronous:
&lt;/h2&gt;

&lt;p&gt;We won't be touching the controller, so it will be something like:&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="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/do-heavy-work"&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;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MessageResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;getTheWorkDone&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 service is called, &lt;/span&gt;
                &lt;span class="c1"&gt;//but the process does not wait for its execution to end&lt;/span&gt;
        &lt;span class="n"&gt;someService&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;doWorkAsynchronously&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
                &lt;span class="c1"&gt;//we return immediately ok to the client&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MessageResponse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Executing..."&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 will simply add the @Async to the called method:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;However &lt;code&gt;@Async&lt;/code&gt; has limitations to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it must be applied to public methods only,&lt;/li&gt;
&lt;li&gt;calling the method decorated with &lt;code&gt;@Async&lt;/code&gt; from within the same class won't work,&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;The method will be something like:&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="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doWorkAsynchronously&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// the nessesary work&lt;/span&gt;
        &lt;span class="c1"&gt;// log the success&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// log the error&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 that's it 😎, you’ve got your asynchronous method readyyyyyy 😉.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmemegenerator.net%2Fimg%2Finstances%2F84951610.jpg" 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%2Fmemegenerator.net%2Fimg%2Finstances%2F84951610.jpg" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. More options:
&lt;/h2&gt;

&lt;p&gt;Other stuff not covered here, that Spring Boot offers:&lt;br&gt;
&lt;a href="https://spring.io/guides/gs/async-method/" rel="noopener noreferrer"&gt;https://spring.io/guides/gs/async-method/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>async</category>
      <category>spring</category>
      <category>asynchronous</category>
    </item>
  </channel>
</rss>
