|
| 1 | +/* |
| 2 | + * Copyright 2018-present 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.kafka.annotation; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
| 24 | + |
| 25 | +import org.springframework.core.annotation.AliasFor; |
| 26 | +import org.springframework.core.retry.RetryPolicy; |
| 27 | +import org.springframework.format.annotation.DurationFormat; |
| 28 | +import org.springframework.util.backoff.ExponentialBackOff; |
| 29 | +import org.springframework.util.backoff.FixedBackOff; |
| 30 | + |
| 31 | +/** |
| 32 | + * Collects metadata for creating a {@link org.springframework.util.backoff.BackOff BacOff} |
| 33 | + * instance as part of a {@link RetryPolicy}. Values can be provided as is or using a |
| 34 | + * {@code *String} equivalent that supports more format, as well as expression evaluations. |
| 35 | + * <p> |
| 36 | + * The available attributes lead to the following: |
| 37 | + * <ul> |
| 38 | + * <li>With no explicit settings, the default is a {@link FixedBackOff} with a delay of |
| 39 | + * {@value #DEFAULT_DELAY} ms</li> |
| 40 | + * <li>With only {@link #delay()} set: the backoff is a fixed delay with that value</li> |
| 41 | + * <li>In all other cases, an {@link ExponentialBackOff} is created with the values of |
| 42 | + * {@link #delay()} (default: {@value RetryPolicy.Builder#DEFAULT_DELAY} ms), |
| 43 | + * {@link #maxDelay()} (default: no maximum), {@link #multiplier()} |
| 44 | + * (default: {@value RetryPolicy.Builder#DEFAULT_MULTIPLIER}) and {@link #jitter()} |
| 45 | + * (default: no jitter).</li> |
| 46 | + * </ul> |
| 47 | + * |
| 48 | + * @author Dave Syer |
| 49 | + * @author Gary Russell |
| 50 | + * @author Aftab Shaikh |
| 51 | + * @author Stephane Nicoll |
| 52 | + * @since 4.0 |
| 53 | + */ |
| 54 | +@Target(ElementType.ANNOTATION_TYPE) |
| 55 | +@Retention(RetentionPolicy.RUNTIME) |
| 56 | +@Documented |
| 57 | +public @interface BackOff { |
| 58 | + |
| 59 | + /** |
| 60 | + * Default {@link #delay()} in milliseconds. |
| 61 | + */ |
| 62 | + long DEFAULT_DELAY = 1000; |
| 63 | + |
| 64 | + /** |
| 65 | + * Alias for {@link #delay()}. |
| 66 | + * <p>Intended to be used when no other attributes are needed, for example: |
| 67 | + * {@code @BackOff(2000)}. |
| 68 | + * |
| 69 | + * @return the based delay in milliseconds (default{@value DEFAULT_DELAY}) |
| 70 | + */ |
| 71 | + @AliasFor("delay") |
| 72 | + long value() default DEFAULT_DELAY; |
| 73 | + |
| 74 | + /** |
| 75 | + * Specify the base delay after the initial invocation. |
| 76 | + * <p>If only a {@code delay} is specified, a {@link FixedBackOff} with that value |
| 77 | + * as the interval is configured. |
| 78 | + * <p>If a {@linkplain #multiplier() multiplier} is specified, this serves as the |
| 79 | + * initial delay to multiply from. |
| 80 | + * <p>The default is {@value DEFAULT_DELAY} milliseconds. |
| 81 | + * |
| 82 | + * @return the based delay in milliseconds (default{@value DEFAULT_DELAY}) |
| 83 | + */ |
| 84 | + @AliasFor("value") |
| 85 | + long delay() default DEFAULT_DELAY; |
| 86 | + |
| 87 | + /** |
| 88 | + * Specify the base delay after the initial invocation using a String format. If |
| 89 | + * this is specified, takes precedence over {@link #delay()}. |
| 90 | + * <p>The delay String can be in several formats: |
| 91 | + * <ul> |
| 92 | + * <li>a plain long — which is interpreted to represent a duration in |
| 93 | + * milliseconds</li> |
| 94 | + * <li>any of the known {@link DurationFormat.Style}: the {@link DurationFormat.Style#ISO8601 ISO8601} |
| 95 | + * style or the {@link DurationFormat.Style#SIMPLE SIMPLE} style — using |
| 96 | + * milliseconds as fallback if the string doesn't contain an explicit unit</li> |
| 97 | + * <li>Regular expressions, such as {@code ${example.property}} to use the |
| 98 | + * {@code example.property} from the environment</li> |
| 99 | + * </ul> |
| 100 | + * |
| 101 | + * @return the based delay as a String value — for example a placeholder |
| 102 | + * @see #delay() |
| 103 | + */ |
| 104 | + String delayString() default ""; |
| 105 | + |
| 106 | + /** |
| 107 | + * Specify the maximum delay for any retry attempt, limiting how far |
| 108 | + * {@linkplain #jitter jitter} and the {@linkplain #multiplier() multiplier} can |
| 109 | + * increase the {@linkplain #delay() delay}. |
| 110 | + * <p>Ignored if only {@link #delay()} is set, otherwise an {@link ExponentialBackOff} |
| 111 | + * with the given max delay or an unlimited delay if not set. |
| 112 | + * |
| 113 | + * @return the maximum delay |
| 114 | + */ |
| 115 | + long maxDelay() default 0; |
| 116 | + |
| 117 | + /** |
| 118 | + * Specify the maximum delay for any retry attempt using a String format. If this is |
| 119 | + * specified, takes precedence over {@link #maxDelay()}.. |
| 120 | + * <p>The max delay String can be in several formats: |
| 121 | + * <ul> |
| 122 | + * <li>a plain long — which is interpreted to represent a duration in |
| 123 | + * milliseconds</li> |
| 124 | + * <li>any of the known {@link DurationFormat.Style}: the {@link DurationFormat.Style#ISO8601 ISO8601} |
| 125 | + * style or the {@link DurationFormat.Style#SIMPLE SIMPLE} style — using |
| 126 | + * milliseconds as fallback if the string doesn't contain an explicit unit</li> |
| 127 | + * <li>Regular expressions, such as {@code ${example.property}} to use the |
| 128 | + * {@code example.property} from the environment</li> |
| 129 | + * </ul> |
| 130 | + * |
| 131 | + * @return the max delay as a String value — for example a placeholder |
| 132 | + * @see #maxDelay() |
| 133 | + */ |
| 134 | + String maxDelayString() default ""; |
| 135 | + |
| 136 | + /** |
| 137 | + * Specify a multiplier for a delay for the next retry attempt, applied to the previous |
| 138 | + * delay, starting with the initial {@linkplain #delay() delay} as well as to the |
| 139 | + * applicable {@linkplain #jitter() jitter} for each attempt. |
| 140 | + * <p>Ignored if only {@link #delay()} is set, otherwise an {@link ExponentialBackOff} |
| 141 | + * with the given multiplier or {@code 1.0} if not set. |
| 142 | + * |
| 143 | + * @return the value to multiply the current interval by for each attempt |
| 144 | + */ |
| 145 | + double multiplier() default 0; |
| 146 | + |
| 147 | + /** |
| 148 | + * Specify a multiplier for a delay for the next retry attempt using a String format. |
| 149 | + * If this is specified, takes precedence over {@link #multiplier()}. |
| 150 | + * <p>The multiplier String can be in several formats: |
| 151 | + * <ul> |
| 152 | + * <li>a plain double</li> |
| 153 | + * <li>Regular expressions, such as {@code ${example.property}} to use the |
| 154 | + * {@code example.property} from the environment</li> |
| 155 | + * </ul> |
| 156 | + * |
| 157 | + * @return the value to multiply the current interval by for each attempt — |
| 158 | + * for example a placeholder |
| 159 | + * @see #multiplier() |
| 160 | + */ |
| 161 | + String multiplierString() default ""; |
| 162 | + |
| 163 | + /** |
| 164 | + * Specify a jitter value for the base retry attempt, randomly subtracted or added to |
| 165 | + * the calculated delay, resulting in a value between {@code delay - jitter} and |
| 166 | + * {@code delay + jitter} but never below the {@linkplain #delay() base delay} or |
| 167 | + * above the {@linkplain #maxDelay() max delay}. |
| 168 | + * <p>If a {@linkplain #multiplier() multiplier} is specified, it is applied to the |
| 169 | + * jitter value as well. |
| 170 | + * <p>Ignored if only {@link #delay()} is set, otherwise an {@link ExponentialBackOff} |
| 171 | + * with the given jitter or no jitter if not set. |
| 172 | + * |
| 173 | + * @return the jitter value in milliseconds |
| 174 | + * @see #delay() |
| 175 | + * @see #maxDelay() |
| 176 | + * @see #multiplier() |
| 177 | + */ |
| 178 | + long jitter() default 0; |
| 179 | + |
| 180 | + /** |
| 181 | + * Specify a jitter value for the base retry attempt using a String format. If this is |
| 182 | + * specified, takes precedence over {@link #jitter()}. |
| 183 | + * <p>The jitter String can be in several formats: |
| 184 | + * <ul> |
| 185 | + * <li>a plain long — which is interpreted to represent a duration in |
| 186 | + * milliseconds</li> |
| 187 | + * <li>any of the known {@link DurationFormat.Style}: the {@link DurationFormat.Style#ISO8601 ISO8601} |
| 188 | + * style or the {@link DurationFormat.Style#SIMPLE SIMPLE} style — using |
| 189 | + * milliseconds as fallback if the string doesn't contain an explicit unit</li> |
| 190 | + * <li>Regular expressions, such as {@code ${example.property}} to use the |
| 191 | + * {@code example.property} from the environment</li> |
| 192 | + * </ul> |
| 193 | + * |
| 194 | + * @return the jitter as a String value — for example a placeholder |
| 195 | + * @see #jitter() |
| 196 | + */ |
| 197 | + String jitterString() default ""; |
| 198 | + |
| 199 | +} |
0 commit comments