Good afternoon, I have a question, if in my Spring Boot application a client makes a request, I make an insert of an entity in the database and then I wait for a field of the entity to have the value 'x' and return the result to the client. The field with the value 'x' is modified by another service, so I have to wait and consult this field every few seconds.
In other words, all this can take even minutes.
I have this done but I don't think it's the best it can be.
By the way I have looked at dozens of threads on this topic.
**
@PostMapping("/some/new")
@PreAuthorize("hasRole('USER') or hasRole('ADMIN')")
public void newSome(@RequestBody Some some, HttpServletRequest req, Principal principal) throws InterruptedException, ExecutionException {
Usuario user = usuarioRepository.findByUsuario(principal.getName());
if(user != null) {
some.setUsuario_id(user.getId());
some.setRealization(false);
Some someDB = someRepository.save(some); ----- SAVE ENTITY
return TestGreile(someDB.getId()).get()); ----- RETURN UNTIL REACH FIELD isRealization true in the BD, this is changed by another service
}
}
@Async
public CompletableFuture<Boolean>TestGreile(long some_id) throws InterruptedException {
Some someTmp = (Some) peticionesRepository.findById(peticion_id);
while(!someTmp.isRealization()) {
entityManager.clear();
someTmp = peticionesRepository.findById(some_id);
Thread.sleep(3000);
System.out.println("Iteración! --> " + someTmp);
}
return CompletableFuture.completedFuture(someTmp.isRealization());
}
**