title: Asynchronous Controller in Spring Boot
body: <p>I have created Webservice using Spring boot and in this there is a rest controller which hits the database via vendor based JDBC driver and fetch the records. In this process number of records retrieved are more than 80K records. Due to this when ever we are hitting the the rest endpoint as client , we are getting time out errors. </p>
  
  <p>I have tried setting up the asynchronous calls using the below tutorial.But unfortunately , rest calls are still timing out.</p>
  
  <p><a href="https://howtodoinjava.com/spring-boot2/enableasync-async-controller/" rel="nofollow noreferrer">https://howtodoinjava.com/spring-boot2/enableasync-async-controller/</a></p>
  
  <p>Controller </p>
  
  <pre><code>    @RequestMapping(value = "/v1/lr/fullpositionasync", produces = {APPLICATION_JSON_UTF8_VALUE},   method = RequestMethod.GET)
      @ResponseBody
      public CompletableFuture&lt;List&lt;Position&gt;&gt; retrieveTradePositionsFullAsync(HttpServletRequest request, HttpServletResponse response) throws ExecutionException, InterruptedException {
  
          CompletableFuture&lt;List&lt;Position&gt;&gt; positionList =null;
          try {
              positionList = positionService.getFullPosition();
          }
          catch(Exception e){
              log.info("Error Occurred in Controller is:"+e.getMessage());
          }
          CompletableFuture.allOf(positionList).join();
          log.info(String.valueOf(positionList.get()));
          return positionList;
      }
  
  </code></pre>
  
  <p>Service</p>
  
  <pre><code>@Service
  @Slf4j
  public class PositionServiceImpl implements PositionService {
  
      @Autowired
      private PositionDao positionDao;
  
      @Async("asyncExecutor")
      @Override
      public CompletableFuture&lt;List&lt;Position&gt;&gt; getFullPosition()  {
  
          List&lt;Position&gt; fullpositionList = null;
          log.info("Getting the full Position process started");
          fullpositionList = positionDao.retrieveData();
          log.info("Total Positions retrieved:"+fullpositionList.size());
          try {
              log.info("Thread is about to sleep 1000 milliseconds");
              Thread.sleep(1000);
          }catch(InterruptedException e){
              log.info(e.getMessage());
          }
          log.info("Full Positions retrieval completed");
          return CompletableFuture.completedFuture(fullpositionList);
      }
  
  
  }
  
  </code></pre>
  
  <p>Configuration</p>
  
  <pre><code>@Configuration
  @EnableAsync
  @Slf4j
  public class AsyncConfiguration
  {
      @Bean(name = "asyncExecutor")
      public Executor asyncExecutor()
      {
          ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
          executor.setCorePoolSize(20);
          executor.setMaxPoolSize(1000);
          executor.setWaitForTasksToCompleteOnShutdown(true);
          executor.setThreadNamePrefix("AsynchThreadForEndPoint-");
          executor.initialize();
          log.info("Executor is :"+executor.toString());
          return executor;
      }
  }
  </code></pre>
  
  <p>DAO</p>
  
  <pre><code>@Repository
  public class PositionDaoImpl implements PositionDao {
  
      @Autowired
      private JdbcTemplate jdbcTemplate;
  
      private static final String ALL_POSITION_QUERY = "call AllPositionProcedure()";
  
  
      public List&lt;Position&gt; retrieveData() {
          return jdbcTemplate.query(ALL_POSITION_QUERY, new BeanPropertyRowMapper(Position.class));
          //  List&lt;Map&lt;String, Object&gt;&gt; mapList = jdbcTemplate.queryForList(sql);
  
      }
  </code></pre>