This is a POC of multi-thread process that uses a repositoy injection into a Runnable class that executes in a parallel approach.
- Java with Spring Boot + JPA;
- java.util.concurrent.CountDownLatch;
- java.util.concurrent.ExecutorService;
- java.util.concurrent.Executors.
- A regular Runnable class cannot inject Repositories interfaces (@Autowired) in a Spring Boot Application unless you make it as a @Component with @Scope("prototype") to return each Runnable Classe individually.
Example:
/**
* @author Bruno Paz (bscpaz)
*/
@Component
@Scope("prototype")
public class MyRunnable implements Runnable {
private CountDownLatch countDownLatch = null;
private someDto someDto = null;
@Autowired
private MyRepository repository;
public MyRunnable() {
super();
}
//Getters ans Setters
@Override
public void run() {
// some code...
List<MyEntity> myEntities = repository.findByName(this.someDto.getName());
// more code...
}