Currently, we are facing an issue with a read timeout for a DB call. I am trying to add a time limit on the method to wait for only 3 seconds. If the method doesn't return data in 3 seconds my execution goes to the next line. I tried to achieve this with the below code but the control is not moving to the next step even after 3 seconds. In the below code I am adding a timeout for method b execution, When method b takes more than 3 seconds to return the control should go to catch block and go to the next line of code i.e. callingMethodC. But the control is not coming out even after 3 seconds am I missing anything? Can anyone suggest?
private String methodA(){
TimeLimiter timeLimiter = new SimpleTimeLimiter();
try {
String data = timeLimiter.callWithTimeout(
() -> methodB("abc"), 3, TimeUnit.SECONDS, true);
} catch (Exception e) {
log.error("Exception while read data from db after 3 seconds");
}
callmethodc();
}
private String methodB(String abc){
String result = dbrepository.get(abc);
return result;
}