Home > Software design >  Why Spring use ForkPoolJoin instead of ThreadPoolTaskExecutor with @Async?
Why Spring use ForkPoolJoin instead of ThreadPoolTaskExecutor with @Async?

Time:10-28

For my studies, I'm working on a Spring Boot REST API. I'm supposed to reduce the execution time of the code when it received a request. So, I thought that make the code asynchronous would be a good idea. But, unfortunately, I face some problems with Spring for this, and despite the few hours of research online to find a solution, I didn't find anything. Let me explain :

To optimize my code, I decided to use @Async Spring Annotation. For this, I created an AsyncConfiguration class who looks like this :

@Configuration
@EnableAsync
public class AsyncConfiguration {

    @Bean(name = "asyncExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(1);
        executor.setQueueCapacity(50);
        executor.setThreadNamePrefix("AsynchThread-");
        executor.initialize();
        return executor;
    }
    
}

Usually, the asyncExecutor() method returns the Executor class or sub-class that must be used by Spring for async calls. In this case, it's a ThreadPoolTaskExecutor. And the majority of my code is annotated with @Async to use my asyncExecutor, like this :

@Async("asyncExecutor")
    public CompletableFuture<User> calculateRewards(User user) {
        return CompletableFuture.supplyAsync(() -> {
            logger.info("Calculating Reward for user : "   user.getUserName());
            List<VisitedLocation> userLocations = user.getVisitedLocations();
            List<Attraction> attractions = gps.getAllAttraction();
        
            for(VisitedLocation visitedLocation : userLocations) {
                for(Attraction attraction : attractions) {
                    if(user.getUserRewards().stream().filter(r -> r.attraction.attractionName.equals(attraction.attractionName)).count() == 0) {
                        if(nearAttraction(visitedLocation, attraction)) {
                            user.addUserReward(new UserReward(visitedLocation, attraction, reward.getAttractionRewardPoints(attraction.attractionId, user.getUserId())));
                        }
                    }
                }
            }

            return user;
        });
    }

But, here's the point : when I run the code, Spring DON'T use my asyncExecutor() bean. How do I know that ? First, when I call an annotated method, in the terminal, here's what I see :

2022-10-27 00:26:24.688  INFO 41436 --- [onPool-worker-4] c.T.T.service.TourGuideMainService       : Calculating Reward for user : internalUser919

The "[onPool-worker-4]" is the Thread name, or at least the end of the Thread name. But it's not supposed to be named like that. If you look at my asyncExecutor() method above, you can see that there is a executor.setThreadNamePrefix("AsynchThread-");. If the code was working as intended, the Thread should be called "AsynchThread-4", but It's not.

Secondly, I decided to run my code in Debug mode, and I went into the Debug menu of VS Code, and I dicovered two things :

1 - When I run my stress test who make 1000 calls of calculateRewards() simultaneously, there is only 11 Threads created. Considering the execution time of the calculateRewards() method AND the fact that the Executor have its maxPoolSize by default (i.e. Integer.MAX_VALUE), there should be more than 11 Threads ;

2 - The entire name of the Threads when they are created is "[ForkJoinPool.commonPool-worker-4]" ;

It seems that Spring is using the ForkJoinPool class to create Threads, and it don't ever consider the configuration of my Executor. I have no idea of why it's doing that, I never used ForkJoinPool at all, and like I said, I didn't find anything when searching about that online.

So, why Spring is using ForkJoinPool instead of ThreadPoolTaskExecutor for async methods in my code ? And most important : how can I address that ?

(I hope that it's understandable...)

EDIT 1 : During some random testing, I found that, if Spring seems to use some ForkJoinPool Threads to execute my code, it create a "AsynchThread" anyway but don't use it. That's even more confusing... -_-"

CodePudding user response:

The problem here is that you are using CompletableFuture.supplyAsync to produce your CompletableFuture.

This method will fork off a task running in the ForkJoinPool.commonPool() and execute your Supplier in that task.

Since spring will supply the executor and run your entire method asynchronously, you shouldn't need to use the supplyAsync function with a lambda. Instead your async method should look something like this (inside of your service):

@Service
public class MyService {
  
  ...

  @Async("asyncExecutor")
  public CompletableFuture<User> calculateRewards(User user) {
    logger.info("Calculating Reward for user : "   user.getUserName());
    List<VisitedLocation> userLocations = user.getVisitedLocations();
    List<Attraction> attractions = gps.getAllAttraction();

    for(VisitedLocation visitedLocation : userLocations) {
      for(Attraction attraction : attractions) {
        if(user.getUserRewards().stream().filter(r -> r.attraction.attractionName.equals(attraction.attractionName)).count() == 0) {
          if(nearAttraction(visitedLocation, attraction)) {
            user.addUserReward(new UserReward(visitedLocation, attraction, reward.getAttractionRewardPoints(attraction.attractionId, user.getUserId())));
          }
        }
      }
    }

    return CompletableFuture.completedFuture(user);
  }

}

If you autowire your service into a CommandLineRunner that executes your method, you can use this runner to see that spring will handle executing your method asynchronously using the Executor you have defined in your configuration.

For example:

@Component
public class Runner implements CommandLineRunner {

  private final MyService service;

  public Runner(MyService service) {
    this.service = service;
  }

  @Override
  public void run(String... args) throws Exception {
    CompletableFuture<User> user1 = service.calculateRewards(new User("user1"));
    CompletableFuture<User> user2 = service.calculateRewards(new User("user2"));
    CompletableFuture<User> user3 = service.calculateRewards(new User("user3"));

    CompletableFuture.allOf(user1,user2,user3).join();
  }

}
  • Related