0

I have a java spring boot application project. I want to implement thread parallelism there when multiple users login into that application. the response time is currently increasing. for a single user the response time is 200 millisecond. for multiple users it goes to 9 to 10 seconds. we need to reduce it. currently we have enable @EnableAsync and a TaskExecutor is also used in the application. How should i start. can anyone please help me.

currently we have tried some concurrency enable @EnableAsync and a TaskExecutor is also used in the application.

a sample controller.

@RestController
@RequestMapping("left/transfer/api26")
Class Transfer
{
  private static Logger logger = LogManager.getLogger(TransferController.class);


  @GetMapping("/transfer/getById/{id}")
  public Response getTransferByID(HttpServletRequest request, @PathVariable("id") String id) {
    auditLogger.audit("GET_ONE_TRANSFER", request.getRemoteAddr());
    logger.info("Entered into getTRANSFERByID() => TRANSFER");
    Response response = new Response();
    Map<String, Object> transferResponse=new HashMap<String, Object>(); 
    try {
      transferResponse = transferservice.getTransferDataById(id);
      if (!transferResponse.isEmpty()) {
        response.setData(transferResponse);
        response.setMessage("Transfer Data Fetched");
        response.setStatus("Success");
      } else if (transferResponse.isEmpty()) {
        logger.error(transferResponse);
        response.setMessage("No transfer Object Found");
        response.setStatus("Failed");
      }
    } catch (Exception e) {
      logger.error("gettransferByID() controller error ==> Issue Found " + e.getMessage() + e);
      response.setData(null);
      response.setMessage(e.getMessage());
      response.setStatus("Failed");
    }
    return response;
  }

  }

main class

@SpringBootApplication()
@EnableAsync
@ComponentScan("com.ser.left.sectioncore398")
@Configuration 
@EnableAutoConfiguration 
public class SerApplicationMainclassLoader {
  
  @Value("${CorePoolSize}")
  int CorePoolSize;
  @Value("${MaxPoolSize}")
  int MaxPoolSize;
  @Value("${QueueCapacity}")
  int QueueCapacity;
  
  
  private static Logger logger = LogManager.getLogger(SerApplicationMainclassLoader.class);
  public static void main(String[] args) {
    logger.info("Main class is getting Invoked");

    SpringApplication.run(SerApplicationMainclassLoader.class, args);
  }

    @Bean(name = "processExecutor")
    TaskExecutor workExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setThreadNamePrefix("Mains-");
        threadPoolTaskExecutor.setCorePoolSize(CorePoolSize);
        threadPoolTaskExecutor.setMaxPoolSize(MaxPoolSize);
        threadPoolTaskExecutor.setQueueCapacity(QueueCapacity);
        threadPoolTaskExecutor.afterPropertiesSet();
        logger.info("ThreadPoolTaskExecutor set");
        return threadPoolTaskExecutor;
    }

}
cokachi
  • 27
  • 7
  • What is the symptom you are trying to solve? The application server which Spring Boot uses already takes care that multiple requests can be handled in parallel. Depending on the app server, there is a configuration property, how many parallel requests can be handled but the default value should be more than enough 99.9% of time. So you don't have to do anything. You don't have to do manual multithreading unless you have some long-running tasks in the context of a single login that can be parallelised. – Tarmo Mar 13 '23 at 06:52
  • the applications has multiple apis so if multiple users login into the application the response time increases. we need to reduce that thing – cokachi Mar 13 '23 at 06:55
  • can you tell me which configuration property i need to look – cokachi Mar 13 '23 at 06:56
  • This still doesn't explain the situation. Why can't multiple users use those APIs concurrently? – Tarmo Mar 13 '23 at 06:57
  • Max threads property depends on your chosen app server. Here's a bit on that https://stackoverflow.com/questions/39002090/spring-boot-limit-on-number-of-connections-created – Tarmo Mar 13 '23 at 06:59
  • A per my understand The default value should be 200. will it imporove if we increase the value to 400. I mean for a single user the response time will be 200 milliseconds but if more than 2 users login into the application the final response time in the ui will be 10s. can we use join-fork framework there? – cokachi Mar 13 '23 at 07:07
  • Theres too little information in this question to get a hang of whats really the problem here. Currently it feels like if you have only one user then logging in takes X milliseconds, if you have 2 users logging in at the same time then it takes X*2 milliseconds and so on. Or it's all OK with small number of users, but problems start occurring when number of parallel logins goes to hundreds? Whatever the case, have you debugged and profiled where exactly in your code the bottleneck is? Is it in app server? in rest client? in some database call? It's really hard to help you currently. – Tarmo Mar 13 '23 at 07:15
  • for 1 user it is like 200 milliseond for 2 to 3 user it is like 3 to 5 s for every api call .for getAll opertion it takes 5 to 10 seconds. we need to reduce that. the code is already optimized. it is rest application – cokachi Mar 13 '23 at 07:20
  • Please add a code example to your question about what the implementation of `getOne` and/or `getAll` requests currently look like. – Tarmo Mar 13 '23 at 07:22
  • We can't help you without this: https://stackoverflow.com/help/minimal-reproducible-example – Tarmo Mar 13 '23 at 07:42
  • sample is attached. please have a look at it @Tarmo – cokachi Mar 13 '23 at 12:12
  • It's still next to impossible for me to know where the bottleneck is here. Is it the client that calls this API? Perhaps it uses the `RestTemplate` or some library? Is it the service that services the request? Where does the time go? To `transferservice.getTransferDataById(id);` or somewhere else? – Tarmo Mar 13 '23 at 12:34
  • the service calls to other server and fetches data from the database – cokachi Mar 13 '23 at 12:46
  • We are going in circles here. You have a problem with scalability and you assume that the solution is multithreading. But you are not specifying what exactly is the bottleneck that you assume needs multithreading so therefor it's not possible to give you a good example on how to accomplish this. As for the original question on how to do multithreading in Spring Boot. Sprint Boot is just java, so the same way you would do it in any Java application. ThreadPools are good. @Async annotation is also good. Voted to close the question – Tarmo Mar 13 '23 at 12:57

0 Answers0