title: Multithreaded method to call API in Springboot
body: <p>I need to implement multi-threading in Springboot while calling API with POST method. I am pulling records from oracle database based on a SELECT query, then using RowMapper go through each record one by one. In next step, I am just calling a method to send these records to API to send these records in the form of postmapping and get the records back.</p>
  <p>As the select query can return 10,20 or 100 records at a time. Calling each record one by one won't be ideal. I was thinking if I can send these multiple records at a time. I am not sure how to achieve it as I am new to Springboot and still learning it.</p>
  <pre><code>import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.List;
  
  import javax.sql.DataSource;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.jdbc.core.RowMapper;
  
  import com.demo.payengine.api.ProviderUpdateAPI;
  import com.demo.payengine.config.DataSourceDbConfig;
  import com.demo.payengine.pojo.ProvRecordMappingPOJO;
  
  public class ProvRecordProcessing {
      
      //declare variables
      private static String tn_id                     ;
      private static String pr_id                     ; 
      private static String pr_entity                 ;
      private static String pr_cl_eft_ind             ;    
      private static String pr_edi_dest_ind           ; 
      private static String pr_ra_dest_ind            ; 
      private static String br_id                     ; 
      private static String br_account_no             ; 
      private static String br_account_name           ; 
      private static String br_account_number_qual    ;
  
      @Autowired
      DataSourceDbConfig dbConfig;
      
      @Autowired
      ProviderUpdateAPI provUpdate;
      
      DataSource dataSource;
      
      
      public void dbProvRecordProcessing() {
          
          dataSource = dbConfig.dataSource();
          JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
          
          
          String sqlQuery =&quot;tn_id, pr_id, pr_entity, pr_cl_eft_ind, pr_edi_dest_ind, pr_ra_dest_ind, \r\n&quot; + 
                  &quot;br_id, br_account_no, br_account_name, br_account_number_qual\r\n&quot; + 
                  &quot;from mc_pr_prv pr \r\n&quot; + 
                  &quot;inner join sg_prb_enrll_fl enroll\r\n&quot; + 
                  &quot;on pr.tn_id = enroll.pr_payee_id\r\n&quot; + 
                  &quot;inner join mc_mbr_bank_rel br\r\n&quot; + 
                  &quot;on br.br_ck = pr.br_ck\r\n&quot; + 
                  &quot;where pr.pr_entity ='G'&quot;;
          
          
          RowMapper&lt;ProvRecordMappingPOJO&gt; rowMapper = new RowMapper&lt;ProvRecordMappingPOJO&gt;(){
  
              @Override
              public ProvRecordMappingPOJO mapRow(ResultSet rs, int rowNum) throws SQLException {
                  tn_id                   = rs.getString(&quot;tn_id&quot;)                     ;
                  pr_id                   = rs.getString(&quot;pr_id&quot;)                     ;
                  pr_entity               = rs.getString(&quot;pr_entity&quot;)                 ;
                  pr_cl_eft_ind           = rs.getString(&quot;pr_cl_eft_ind&quot;)             ;
                  pr_edi_dest_ind         = rs.getString(&quot;pr_edi_dest_ind &quot;)          ;
                  br_id                   = rs.getString(&quot;br_id&quot;)                     ;
                  br_account_no           = rs.getString(&quot;br_account_no&quot;)             ;
                  br_account_name         = rs.getString(&quot;br_account_name&quot;)           ;
                  br_account_number_qual  = rs.getString(&quot;br_account_number_qual&quot;)    ;
                  
                  
                  return new ProvRecordMappingPOJO(tn_id, pr_id, pr_entity, pr_cl_eft_ind, pr_edi_dest_ind, 
                          pr_ra_dest_ind, br_id, br_account_no, br_account_name, br_account_number_qual);
                  }
              };
          
          //intialize sql query
          List&lt;ProvRecordMappingPOJO&gt; provRecords = jdbcTemplate.query(sqlQuery, rowMapper);
          
          for(ProvRecordMappingPOJO record : provRecords) {
              
              System.out.println(&quot;Processing provider record..&quot; + record);
              
              //This method will call API &lt;--- how to make it multi threaded
              provUpdate.provUpdateAPI(tn_id, pr_id, pr_entity, pr_cl_eft_ind, pr_edi_dest_ind, pr_ra_dest_ind, 
                      br_id, br_account_no, br_account_name, br_account_number_qual); 
              
          }
          
      }
      
  }
  </code></pre>