|
| 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.core.retry; |
| 18 | + |
| 19 | +import java.time.Duration; |
| 20 | +import java.time.LocalDateTime; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.StringJoiner; |
| 23 | +import java.util.function.Predicate; |
| 24 | + |
| 25 | +import org.jspecify.annotations.Nullable; |
| 26 | + |
| 27 | +import org.springframework.util.Assert; |
| 28 | + |
| 29 | +/** |
| 30 | + * Default {@link RetryPolicy} created by {@link RetryPolicy.Builder}. |
| 31 | + * |
| 32 | + * @author Sam Brannen |
| 33 | + * @author Mahmoud Ben Hassine |
| 34 | + * @since 7.0 |
| 35 | + */ |
| 36 | +class DefaultRetryPolicy implements RetryPolicy { |
| 37 | + |
| 38 | + private final int maxAttempts; |
| 39 | + |
| 40 | + private final @Nullable Duration maxDuration; |
| 41 | + |
| 42 | + private final Set<Class<? extends Throwable>> includes; |
| 43 | + |
| 44 | + private final Set<Class<? extends Throwable>> excludes; |
| 45 | + |
| 46 | + private final @Nullable Predicate<Throwable> predicate; |
| 47 | + |
| 48 | + |
| 49 | + DefaultRetryPolicy(int maxAttempts, @Nullable Duration maxDuration, Set<Class<? extends Throwable>> includes, |
| 50 | + Set<Class<? extends Throwable>> excludes, @Nullable Predicate<Throwable> predicate) { |
| 51 | + |
| 52 | + Assert.isTrue((maxAttempts > 0 || maxDuration != null), "Max attempts or max duration must be specified"); |
| 53 | + |
| 54 | + this.maxAttempts = maxAttempts; |
| 55 | + this.maxDuration = maxDuration; |
| 56 | + this.includes = includes; |
| 57 | + this.excludes = excludes; |
| 58 | + this.predicate = predicate; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @Override |
| 63 | + public RetryExecution start() { |
| 64 | + return new DefaultRetryPolicyExecution(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String toString() { |
| 69 | + StringJoiner result = new StringJoiner(", ", "DefaultRetryPolicy[", "]"); |
| 70 | + if (this.maxAttempts > 0) { |
| 71 | + result.add("maxAttempts=" + this.maxAttempts); |
| 72 | + } |
| 73 | + if (this.maxDuration != null) { |
| 74 | + result.add("maxDuration=" + this.maxDuration.toMillis() + "ms"); |
| 75 | + } |
| 76 | + if (!this.includes.isEmpty()) { |
| 77 | + result.add("includes=" + names(this.includes)); |
| 78 | + } |
| 79 | + if (!this.excludes.isEmpty()) { |
| 80 | + result.add("excludes=" + names(this.excludes)); |
| 81 | + } |
| 82 | + if (this.predicate != null) { |
| 83 | + result.add("predicate=" + this.predicate.getClass().getSimpleName()); |
| 84 | + } |
| 85 | + return result.toString(); |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + private static String names(Set<Class<? extends Throwable>> types) { |
| 90 | + StringJoiner result = new StringJoiner(", ", "[", "]"); |
| 91 | + for (Class<? extends Throwable> type : types) { |
| 92 | + String name = type.getCanonicalName(); |
| 93 | + result.add(name != null? name : type.getName()); |
| 94 | + } |
| 95 | + return result.toString(); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + /** |
| 100 | + * {@link RetryExecution} for {@link DefaultRetryPolicy}. |
| 101 | + */ |
| 102 | + private class DefaultRetryPolicyExecution implements RetryExecution { |
| 103 | + |
| 104 | + private final LocalDateTime retryStartTime = LocalDateTime.now(); |
| 105 | + |
| 106 | + private int retryCount; |
| 107 | + |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean shouldRetry(Throwable throwable) { |
| 111 | + if (DefaultRetryPolicy.this.maxAttempts > 0 && |
| 112 | + this.retryCount++ >= DefaultRetryPolicy.this.maxAttempts) { |
| 113 | + return false; |
| 114 | + } |
| 115 | + if (DefaultRetryPolicy.this.maxDuration != null) { |
| 116 | + Duration retryDuration = Duration.between(this.retryStartTime, LocalDateTime.now()); |
| 117 | + if (retryDuration.compareTo(DefaultRetryPolicy.this.maxDuration) > 0) { |
| 118 | + return false; |
| 119 | + } |
| 120 | + } |
| 121 | + if (!DefaultRetryPolicy.this.excludes.isEmpty()) { |
| 122 | + for (Class<? extends Throwable> excludedType : DefaultRetryPolicy.this.excludes) { |
| 123 | + if (excludedType.isInstance(throwable)) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + if (!DefaultRetryPolicy.this.includes.isEmpty()) { |
| 129 | + boolean included = false; |
| 130 | + for (Class<? extends Throwable> includedType : DefaultRetryPolicy.this.includes) { |
| 131 | + if (includedType.isInstance(throwable)) { |
| 132 | + included = true; |
| 133 | + break; |
| 134 | + } |
| 135 | + } |
| 136 | + if (!included) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + } |
| 140 | + return DefaultRetryPolicy.this.predicate == null || DefaultRetryPolicy.this.predicate.test(throwable); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String toString() { |
| 145 | + StringJoiner result = new StringJoiner(", ", "DefaultRetryPolicyExecution[", "]"); |
| 146 | + if (DefaultRetryPolicy.this.maxAttempts > 0) { |
| 147 | + result.add("maxAttempts=" + DefaultRetryPolicy.this.maxAttempts); |
| 148 | + result.add("retryCount=" + this.retryCount); |
| 149 | + } |
| 150 | + if (DefaultRetryPolicy.this.maxDuration != null) { |
| 151 | + result.add("maxDuration=" + DefaultRetryPolicy.this.maxDuration.toMillis() + "ms"); |
| 152 | + result.add("retryStartTime=" + this.retryStartTime); |
| 153 | + } |
| 154 | + if (!DefaultRetryPolicy.this.includes.isEmpty()) { |
| 155 | + result.add("includes=" + names(DefaultRetryPolicy.this.includes)); |
| 156 | + } |
| 157 | + if (!DefaultRetryPolicy.this.excludes.isEmpty()) { |
| 158 | + result.add("excludes=" + names(DefaultRetryPolicy.this.excludes)); |
| 159 | + } |
| 160 | + if (DefaultRetryPolicy.this.predicate != null) { |
| 161 | + result.add("predicate=" + DefaultRetryPolicy.this.predicate.getClass().getSimpleName()); |
| 162 | + } |
| 163 | + return result.toString(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments