0

I'm connected with MySQL database trying to fetch the data with JPA in findBy function but I stuck with Async function im just trying to set 10 executor i set task executor with all config but its doesn't work.

Class TestRepository

public interface TestRepository extends CrudRepository<Test, Long> {

    List<Test> findByLastName(String lastName);

    Test findById(long id);
}

Class AsyncConfiguration

@Configuration
@EnableAsync
public class AsyncConfiguration {
    private static final Logger LOGGER = LoggerFactory.getLogger(AsyncConfiguration.class);
    @Bean (name = "taskExecutor")
    public Executor taskExecutor() {
        LOGGER.debug("Creating Async Task Executor");
        final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(2);
        executor.setMaxPoolSize(2);
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("CarThread-");
        executor.initialize();
        return executor;
    }
}

Class AccessingDataJpaApplication

@SpringBootApplication
public class AccessingDataJpaApplication {

    private static final Logger log = LoggerFactory.getLogger(AccessingDataJpaApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(AccessingDataJpaApplication.class);
    }

    @Bean
    @Async
    public CommandLineRunner demo(TestRepository repository) {
        return (args) -> {

            log.info("--------------------------------------------");
            repository.findByLastName("FL").forEach(on -> {
                log.info(on.toString());
            });
        };

        }
}
Vy Do
  • 46,709
  • 59
  • 215
  • 313
mashro3ak
  • 93
  • 6
  • What do you expect it to do? This will use 1 thread and run (and I doubt it will use a thread from your pool as you annotated the bean method not the actual `run` method from the `CommandLineRunner`. – M. Deinum Oct 28 '19 at 18:41
  • @M.Deinum im still beginner with java and spring can you show me how to fix it, i just want to run 10 threads in parallel – mashro3ak Oct 28 '19 at 19:11
  • To do what? Currently there is only 1 thread being used. So not sure what you want to accomplish here. – M. Deinum Oct 29 '19 at 08:06
  • im trying to run 10 threads with findby function, sorry im still a beginner with java and spring – mashro3ak Oct 29 '19 at 08:11
  • 1
    Then start 10 threads to do a findBy. I still don't get what or why you want to do this. Why do you want 10 threads executing the same query? – M. Deinum Oct 29 '19 at 08:19
  • i have asked this question 2 days ago about how can i run 10 threads on JPA and http post to API , but no one helps me about this point thats why im trying to practice : https://stackoverflow.com/questions/58582177/jpa-with-https-request-multithreading-spring – mashro3ak Oct 29 '19 at 08:22

0 Answers0