本文介绍了Spring Retry @Recover传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何关于我需要采取行动的可能性的信息。我正在使用带有@Recover处理程序方法的@Retryable注释。 Smth是这样的:

I couldn't find any info about a possibility of action I need. I am using @Retryable annotation with @Recover handler method. Smth like this:

    @Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
    public void update(Integer id)
    {
        execute(id);
    }

    @Recover
    public void recover(Exception ex)
    {
        logger.error("Error when updating object with id {}", id);
    }

问题是我不知道,如何传递我的参数 idto recover()方法。有任何想法吗?在此先感谢。

The problem is that I don't know, how to pass my parameter "id" to recover() method. Any ideas? Thanks in advance.

推荐答案

根据,只需在 @Retryable @Recover 方法:

According to the Spring Retry documentation, just align the parameters between the @Retryable and the @Recover methods :

@Service
class Service {
    @Retryable(RemoteAccessException.class)
    public void service(String str1, String str2) {
        // ... do something
    }
    @Recover
    public void recover(RemoteAccessException e, String str1, String str2) {
       // ... error handling making use of original args if required
    }
}


所以你可以写:

@Retryable(value = {Exception.class}, maxAttempts = 5, backoff = @Backoff(delay = 10000))
public void update(Integer id) {
    execute(id);
}

@Recover
public void recover(Exception ex, Integer id){
    logger.error("Error when updating object with id {}", id);
}

这篇关于Spring Retry @Recover传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:15