letsDoThis
letsDoThis

Reputation: 71

@Retryable method not working that also is @Scheduled and @EnableSchedulerLock

I want to create a cron job which is retry-able and only 1 instance should execute it when we deploy multiple instances of the application.

I have also referred @Recover annotated method is not discovered for a @Retryable method that also is @Scheduled, but I am unable to resolve the issue of ArrayIndexOutOfBoundsException.

I am using 2.1.8.RELEASE version spring-boot

@Configuration
@EnableScheduling
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")
@EnableRetry
public class MyScheduler {

    @Scheduled(cron = "0 16 16 * * *")
    @SchedulerLock(name = "MyScheduler_lock", lockAtLeastForString = ""PT5M", lockAtMostForString ="PT14M")
    @Retryable(value = Exception.class, maxAttempts = 2)
    public void retryAndRecover() {
        retry++;
        log.info("Scheduling Service Failed " + retry);
        throw new Exception();
    }

    @Recover
    public void recover(Exception e, String str) {
        log.info("Service recovering");
    }
}

Detailed exception:

2019-12-07 19:42:00.109  INFO [my-service,false] 16767 --- [   scheduling-1] r.t.p.scheduler.MyScheduler   : Scheduling Service Failed 1
2019-12-07 19:42:01.114  INFO [my-service,false] 16767 --- [   scheduling-1] r.t.p.scheduler.MyScheduler   : Scheduling Service Failed 2
2019-12-07 19:42:01.123 ERROR [my-service,,,] 16767 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.

java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 1 out of bounds for object array[0]
    at java.base/java.lang.System.arraycopy(Native Method) ~[na:na]
    at org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler$SimpleMetadata.getArgs(RecoverAnnotationRecoveryHandler.java:166) ~[spring-retry-1.2.1.RELEASE.jar:na]
    at org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler.recover(RecoverAnnotationRecoveryHandler.java:62) ~[spring-retry-1.2.1.RELEASE.jar:na]

Upvotes: 3

Views: 3430

Answers (1)

Gary Russell
Gary Russell

Reputation: 174504

Your recover method can't have more parameters than the main method (aside from the exception.

String str

Upvotes: 1

Related Questions