I tried spring retry framework with Simple java classes using SimpleRetryPolicy/RetryTemplate. It worked fine. So I thought of doing the same with Annotations. Annotations never worked for me. Didn't find much help online as well. The following code just worked like normal Java program throwing the exception at first attempt which is not the expected behavior. It should have tried atleast 5 times before throwing exception or recovering from it. Not sure where I went wrong. Do I need to do any XML/spring AOP configuration for this to work ? How would that be
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
@EnableRetry
@Configuration
public class App {
public static void main(String[] args) throws Exception {
Parentservice p = new SpringRetryWithHystrixService();
String status = p.getStatus();
System.out.println(status);
}
}
import org.springframework.retry.annotation.Retryable;
public interface Parentservice {
@Retryable(maxAttempts=5)
String getStatus() throws Exception;
}
import org.springframework.retry.annotation.Recover;
public class SpringRetryWithHystrixService implements Parentservice{
public static int i=0;
public String getStatus() throws Exception{
System.out.println("Attempt:"+i++);
throw new NullPointerException();
}
@Recover
public static String getRecoveryStatus(){
return "Recovered from NullPointer Exception";
}
}
I tried @Retryable(maxAttempts=5) on the top of interface and implementation which didn't make any difference.