|
| 1 | +/* |
| 2 | + * Copyright 2002-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.aop.retry; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.time.Duration; |
| 21 | + |
| 22 | +import org.aopalliance.intercept.MethodInterceptor; |
| 23 | +import org.aopalliance.intercept.MethodInvocation; |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | +import org.reactivestreams.Publisher; |
| 26 | +import reactor.core.publisher.Flux; |
| 27 | +import reactor.core.publisher.Mono; |
| 28 | +import reactor.util.retry.Retry; |
| 29 | + |
| 30 | +import org.springframework.core.ReactiveAdapter; |
| 31 | +import org.springframework.core.ReactiveAdapterRegistry; |
| 32 | +import org.springframework.core.retry.RetryException; |
| 33 | +import org.springframework.core.retry.RetryPolicy; |
| 34 | +import org.springframework.core.retry.RetryTemplate; |
| 35 | +import org.springframework.core.retry.Retryable; |
| 36 | +import org.springframework.util.ClassUtils; |
| 37 | +import org.springframework.util.backoff.ExponentialBackOff; |
| 38 | + |
| 39 | +/** |
| 40 | + * Abstract retry interceptor implementation, adapting a given |
| 41 | + * retry specification to either {@link RetryTemplate} or Reactor. |
| 42 | + * |
| 43 | + * @author Juergen Hoeller |
| 44 | + * @since 7.0 |
| 45 | + * @see #getRetrySpec |
| 46 | + * @see RetryTemplate |
| 47 | + * @see Mono#retryWhen |
| 48 | + * @see Flux#retryWhen |
| 49 | + */ |
| 50 | +public abstract class AbstractRetryInterceptor implements MethodInterceptor { |
| 51 | + |
| 52 | + /** |
| 53 | + * Reactive Streams API present on the classpath? |
| 54 | + */ |
| 55 | + private static final boolean reactiveStreamsPresent = ClassUtils.isPresent( |
| 56 | + "org.reactivestreams.Publisher", AbstractRetryInterceptor.class.getClassLoader()); |
| 57 | + |
| 58 | + private final @Nullable ReactiveAdapterRegistry reactiveAdapterRegistry; |
| 59 | + |
| 60 | + |
| 61 | + public AbstractRetryInterceptor() { |
| 62 | + if (reactiveStreamsPresent) { |
| 63 | + this.reactiveAdapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); |
| 64 | + } |
| 65 | + else { |
| 66 | + this.reactiveAdapterRegistry = null; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + @Override |
| 72 | + public @Nullable Object invoke(MethodInvocation invocation) throws Throwable { |
| 73 | + Method method = invocation.getMethod(); |
| 74 | + Object target = invocation.getThis(); |
| 75 | + MethodRetrySpec spec = getRetrySpec(method, (target != null ? target.getClass() : method.getDeclaringClass())); |
| 76 | + |
| 77 | + if (spec == null) { |
| 78 | + return invocation.proceed(); |
| 79 | + } |
| 80 | + |
| 81 | + if (this.reactiveAdapterRegistry != null) { |
| 82 | + ReactiveAdapter adapter = this.reactiveAdapterRegistry.getAdapter(method.getReturnType()); |
| 83 | + if (adapter != null) { |
| 84 | + Object result = invocation.proceed(); |
| 85 | + if (result == null) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + return ReactorDelegate.adaptReactiveResult(result, adapter, spec, method); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + RetryTemplate retryTemplate = new RetryTemplate(); |
| 93 | + |
| 94 | + RetryPolicy.Builder policyBuilder = RetryPolicy.builder(); |
| 95 | + for (Class<? extends Throwable> include : spec.includes()) { |
| 96 | + policyBuilder.includes(include); |
| 97 | + } |
| 98 | + for (Class<? extends Throwable> exclude : spec.excludes()) { |
| 99 | + policyBuilder.excludes(exclude); |
| 100 | + } |
| 101 | + policyBuilder.predicate(spec.predicate().forMethod(method)); |
| 102 | + policyBuilder.maxAttempts(spec.maxAttempts()); |
| 103 | + retryTemplate.setRetryPolicy(policyBuilder.build()); |
| 104 | + |
| 105 | + ExponentialBackOff backOff = new ExponentialBackOff(); |
| 106 | + backOff.setInitialInterval(spec.delay()); |
| 107 | + backOff.setJitter(spec.jitterDelay()); |
| 108 | + backOff.setMultiplier(spec.delayMultiplier()); |
| 109 | + backOff.setMaxInterval(spec.maxDelay()); |
| 110 | + backOff.setMaxAttempts(spec.maxAttempts()); |
| 111 | + retryTemplate.setBackOffPolicy(backOff); |
| 112 | + |
| 113 | + try { |
| 114 | + return retryTemplate.execute(new Retryable<>() { |
| 115 | + @Override |
| 116 | + public @Nullable Object execute() throws Throwable { |
| 117 | + return invocation.proceed(); |
| 118 | + } |
| 119 | + @Override |
| 120 | + public String getName() { |
| 121 | + Object target = invocation.getThis(); |
| 122 | + return ClassUtils.getQualifiedMethodName(method, (target != null ? target.getClass() : null)); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + catch (RetryException ex) { |
| 127 | + Throwable cause = ex.getCause(); |
| 128 | + throw (cause != null ? cause : new IllegalStateException(ex.getMessage(), ex)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Determine the retry specification for the given method on the given target. |
| 134 | + * @param method the currently executing method |
| 135 | + * @param targetClass the class of the current target object |
| 136 | + * @return the retry specification as a {@link MethodRetrySpec} |
| 137 | + */ |
| 138 | + protected abstract @Nullable MethodRetrySpec getRetrySpec(Method method, Class<?> targetClass); |
| 139 | + |
| 140 | + |
| 141 | + /** |
| 142 | + * Inner class to avoid a hard dependency on Reactive Streams and Reactor at runtime. |
| 143 | + */ |
| 144 | + private static class ReactorDelegate { |
| 145 | + |
| 146 | + public static Object adaptReactiveResult( |
| 147 | + Object result, ReactiveAdapter adapter, MethodRetrySpec spec, Method method) { |
| 148 | + |
| 149 | + Publisher<?> publisher = adapter.toPublisher(result); |
| 150 | + Retry retry = Retry.backoff(spec.maxAttempts(), Duration.ofMillis(spec.delay())) |
| 151 | + .jitter((double) spec.jitterDelay() / spec.delay()) |
| 152 | + .multiplier(spec.delayMultiplier()) |
| 153 | + .maxBackoff(Duration.ofMillis(spec.maxDelay())) |
| 154 | + .filter(spec.combinedPredicate().forMethod(method)); |
| 155 | + publisher = (adapter.isMultiValue() ? Flux.from(publisher).retryWhen(retry) : |
| 156 | + Mono.from(publisher).retryWhen(retry)); |
| 157 | + return adapter.fromPublisher(publisher); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments