I want to restrict the execution time of a method in java, I want my particular method that If it is executed in some certain defined time then ok otherwise it should throw an exception.
For this I tried using as :
@Timeable(limit = 1000, unit = TimeUnit.MILLISECONDS)
public void testMethod(){
try {
Thread.sleep(2000);
if (Thread.interrupted()) {
throw new InterruptedException();
}
}catch(InterruptedException e1){
logger.error("failed due to-" + e1, e1.getMessage());
throw new InterruptedException(e1.getMessage());
} catch (Exception e) {
logger.error(e.getMessage(), e);
throw new Exception(e.getMessage());
}
}
For this I took reference from : http://www.yegor256.com/2014/06/20/limit-method-execution-time.html
It should throw the exception after the line:
Thread.sleep(2000);
But this my code is not throwing the exception.
Please help what is missing.
Thanks in advance for any other suggested approach for the same purpose.