content: package com.citylots.forkjoin;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.annotation.Scope;
  import org.springframework.stereotype.Component;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.concurrent.ForkJoinTask;
  import java.util.concurrent.RecursiveAction;
  
  @Component
  @Scope("prototype")
  public class ForkingComponent extends RecursiveAction {
      @Value("${jdbc.batch.size}")
      private int batchSize;
  
      @Autowired
      private JoiningComponent joiningComponent;
  
      @Autowired
      private ApplicationContext applicationContext;
  
      private final List<String> jsonList;
  
      public ForkingComponent(List<String> jsonList) {
          this.jsonList = jsonList;
      }
  
      @Override
      public void compute() {
          if (jsonList.size() > batchSize) {
              ForkJoinTask.invokeAll(createSubtasks());
          } else {
              joiningComponent.executeBatch(jsonList);
          }
      }
  
      private List<ForkingComponent> createSubtasks() {
          List<ForkingComponent> subtasks = new ArrayList<>();
  
          int size = jsonList.size();
  
          List<String> jsonListOne = jsonList.subList(0, (size + 1) / 2);
          List<String> jsonListTwo = jsonList.subList((size + 1) / 2, size);
  
          subtasks.add(applicationContext.getBean(ForkingComponent.class, new ArrayList<>(jsonListOne)));
          subtasks.add(applicationContext.getBean(ForkingComponent.class, new ArrayList<>(jsonListTwo)));
  
          return subtasks;
      }
  }