@Retryable 方法不起作用,也是@Scheduled 和@EnableSchedulerLock

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

我想创建一个可重试的 cron 作业,当我们部署应用程序的多个实例时,只有 1 个实例应该执行它。

我也提到了,但是我无法解决ArrayIndexOutOfBoundsException的问题。

我使用的是 2.1.8.RELEASE 版本 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");
    }
}

详细异常:

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]

你的 recover 方法不能有比 main 方法更多的参数(异常除外。

String str