【问题标题】:How to make configurable Retryable maxAttempts and backoff from application.properties file in spring boot如何在 Spring Boot 中从 application.properties 文件中进行可配置的 Retryable maxAttempts 和退避
【发布时间】:2019-11-03 23:29:55
【问题描述】:

下面的参数,尽量让可配置的

@Async("threadPoolTaskExecutor")
@Retryable(value = MessagingException.class, maxAttempts = 2, backoff = @Backoff(delay = 5000))

我做了以下更改 application.properties 文件

my.app.maxAttempts = 2
my.app.backOffDelay = 5000

@Retryable(value = MessagingException.class, maxAttempts = "${my.app.maxAttempts}", backoff = @Backoff(delay = "${my.app.my.app.backOffDelay}"))

但出现以下错误。 不兼容的类型。找到java.lang.String。必需的'int'

在 build.gradle 文件中我有

dependencies {
    compile ("org.springframework.boot:spring-boot-starter-web")
    compile ("org.springframework.boot:spring-boot-starter-mail:2.1.2.RELEASE")
    compile ("org.springframework.boot:spring-boot-starter-security:2.1.2.RELEASE")
    compile ("org.springframework.boot:spring-boot-starter-aop")
    compile ("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-jdbc:2.1.0.RELEASE")
    compile ("org.springframework.retry:spring-retry:1.2.4.RELEASE")
    compile ("io.springfox:springfox-swagger2:2.9.2")
    compile ("io.springfox:springfox-swagger-ui:2.9.2")
    compile ("io.springfox:springfox-bean-validators:2.9.2")
    compile ("javax.validation:validation-api:2.0.0.Final")
    compile ("org.hibernate.validator:hibernate-validator")
    compile ("org.hibernate.validator:hibernate-validator-annotation-processor")
//  compile group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.9.8'
    compile ("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.8")
    compile("org.postgresql:postgresql")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    testImplementation('org.springframework.batch:spring-batch-test')
    testCompile group: 'org.springframework.security', name: 'spring-security-test', version: '4.0.0.RELEASE'
}

【问题讨论】:

    标签: java spring multithreading spring-boot spring-retry


    【解决方案1】:

    使用表达式来

    @Retryable(value = MessagingException.class, maxAttemptsExpression = "${my.app.maxAttempts}", backoff = @Backoff(delayExpression = "${my.app.my.app.backOffDelay}"))
    

    这应该可以工作

    【讨论】:

    • 是的,我最近这样做了。此外,您可以使用该 Spring EL 来调用方法并根据需要从不同的位置加载。
    • 如何测试 Retryable 是否使用配置的最大尝试?
    • @PraveenD 使用 RetryListenerSupport 记录尝试次数
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 2023-04-01
    相关资源
    最近更新 更多