I am doing a function in my Spring Boot application where in a REST request, I have to wait for a variable from the DB to have 'x' value and then return the response to the client.
I did it with Thread.sleep() in a while, but is it the best way to do it in terms of performance?
Code with Thread.sleep:
while(!peticionTmp.isRealizada()) {
entityManager.clear();
peticionTmp = peticionesRepository.findById(peticion_id);
Thread.sleep(3000);
System.out.println("Iteración! --> " + peticionTmp);
}
return peticionTmp.isRealizada();
I saw what could be done with this:
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor ();
scheduler.schedule (() -> {
/ *
* define work to be done inside this lambda
* /
}, diffTime, TimeUnit.MILLISECONDS);
But I don't know how to implement it in the while.
The client sends a request, the spring boot application saves the request to the DB, now another program looks at the DB in search of requests, makes the service that contain the request and changes a field in the database of that request, the spring boot when it sees that it has been changed that field, I return the response to the client ...