Tag: spring retry

Spring Retry Junit:使用自定义重试策略测试重试模板

我正在尝试测试使用自定义重试策略的重试模板。 为了做到这一点,我使用这个例子: https://github.com/spring-projects/spring-retry/blob/master/src/test/java/org/springframework/retry/support/RetryTemplateTests.java#L57 基本上,我的目标是在我获得某些特定的http错误状态(例如http 500错误状态)时测试我的重试逻辑。 这是我的junit的xml上下文: CustomRetryPolicy就像: public class CustomRetryPolicy extends ExceptionClassifierRetryPolicy { private String maxAttempts; @PostConstruct public void init() { this.setExceptionClassifier(new Classifier() { @Override public RetryPolicy classify(Throwable classifiable) { Throwable exceptionCause = classifiable.getCause(); if (exceptionCause instanceof HttpStatusCodeException) { int statusCode = ((HttpStatusCodeException) classifiable.getCause()).getStatusCode().value(); return handleHttpErrorCode(statusCode); } return neverRetry(); } }); } public void setMaxAttempts(String […]

尝试使用@Retryable排除exception – 导致抛出ExhaustedRetryException

我正在尝试在调用REST模板的方法上使用@Retryable 。 如果由于通信错误而返回错误,我想重试,否则我想在调用时抛出exception。 当ApiException发生时,我得到一个ExhaustedRetryException,并且没有找到足够的’recoverables’,即@Recover方法,而不是被@Retryable抛出和忽略。 我以为我会看到如果只有可恢复的方法存在可能使它快乐并仍然按照希望执行。 没那么多。 它不是抛出exception,而是调用可恢复的方法。 @Retryable(exclude = ApiException include = ConnectionException, maxAttempts = 5, backoff = @Backoff(multiplier = 2.5d, maxDelay = 1000000L, delay = 150000L)) Object call(String domainUri, ParameterizedTypeReference type, Optional domain = Optional.empty(), HttpMethod httpMethod = HttpMethod.POST) throws RestClientException { RequestEntity request = apiRequestFactory.createRequest(domainUri, domain, httpMethod) log.info “************************** Request Entity **************************” log.info […]

确保spring托管bean的单个实例

我创建了一个spring方面来处理Retry机制。 我还创建了一个重试注释。 以下是重试注释的代码和处理此注释的方面。 @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Retry { /** * List of exceptions for which we need to retry method invocation. * * @return Array of classes. */ Class[] exceptions(); /** * Number of retries. Default is 3. * * @return Number of retires. */ int retries() default 3; /** * Back of period […]

如何在Spring Boot中将配置属性注入Spring Retry注释?

在Spring启动应用程序中,我在yaml文件中定义了一些配置属性,如下所示。 my.app.maxAttempts = 10 my.app.backOffDelay = 500L 还有一个例子bean @ConfigurationProperties(prefix = “my.app”) public class ConfigProperties { private int maxAttempts; private long backOffDelay; public int getMaxAttempts() { return maxAttempts; } public void setMaxAttempts(int maxAttempts) { this.maxAttempts = maxAttempts; } public void setBackOffDelay(long backOffDelay) { this.backOffDelay = backOffDelay; } public long getBackOffDelay() { return backOffDelay; } 如何将my.app.maxAttempts和my.app.backOffdelay的值注入Spring Retry注释? […]