EventListener并且可以重试

问题描述 投票:0回答:1

我想在应用程序启动后调用一些代码。有没有办法处理事件:

Started SomeApp in 14.905 seconds (JVM running for 16.268) 

如果另一个应用程序启动,我将尝试。我尝试使用Retryable,但在应用程序启动之前没有执行它,并抛出异常,因此应用程序退出。

    @EventListener
    fun handleContextRefresh(event: ContextRefreshedEvent) {

        retryableInvokeConnection()
    }

    @Retryable(
        value = [RetryableException::class, ConnectionException::class],
        maxAttempts = 100000,
        backoff = Backoff(delay = 5)
    )
    private fun retryableInvokeConnection() {
    }

    @Recover
    private fun retryableInvokeConnectionExceptionHandler(ex: ConnectionException) {
    }

也许我应该使用PostConstruct和while循环。

spring spring-cloud-feign spring-retry
1个回答
1
投票

你不能在同一个bean中调用@Retryable方法,它会使用重试拦截器绕过代理。将方法移动到另一个bean并注入它。

这个事件比使用@PostConstruct更好。

© www.soinside.com 2019 - 2024. All rights reserved.