/
FailureSettings.java
157 lines (139 loc) · 4.28 KB
/
FailureSettings.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package ru.yoomoney.tech.dbqueue.settings;
import javax.annotation.Nonnull;
import java.time.Duration;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import static java.util.Objects.requireNonNull;
/**
* Settings for task execution strategy in case of failure.
*
* @author Oleg Kandaurov
* @since 01.10.2021
*/
public class FailureSettings extends DynamicSetting<FailureSettings> {
@Nonnull
private FailRetryType retryType;
@Nonnull
private Duration retryInterval;
private FailureSettings(@Nonnull FailRetryType retryType,
@Nonnull Duration retryInterval) {
this.retryType = requireNonNull(retryType, "retryType must not be null");
this.retryInterval = requireNonNull(retryInterval, "retryInterval must not be null");
}
/**
* Get task execution retry strategy in case of failure.
*
* @return Task execution retry strategy.
*/
@Nonnull
public FailRetryType getRetryType() {
return retryType;
}
/**
* Get retry interval for task execution in case of failure.
*
* @return Task retry interval.
* @see FailRetryType
*/
@Nonnull
public Duration getRetryInterval() {
return retryInterval;
}
/**
* Create a new builder for failure settings.
*
* @return A new builder for failure settings.
*/
public static Builder builder() {
return new Builder();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
FailureSettings that = (FailureSettings) obj;
return retryType == that.retryType && retryInterval.equals(that.retryInterval);
}
@Override
public int hashCode() {
return Objects.hash(retryType, retryInterval);
}
@Override
public String toString() {
return "{" +
"retryType=" + retryType +
", retryInterval=" + retryInterval +
'}';
}
@Nonnull
@Override
protected String getName() {
return "failureSettings";
}
@Nonnull
@Override
protected BiFunction<FailureSettings, FailureSettings, String> getDiffEvaluator() {
return (oldVal, newVal) -> {
StringJoiner diff = new StringJoiner(",", getName() + '(', ")");
if (!Objects.equals(oldVal.retryType, newVal.retryType)) {
diff.add("retryType=" +
newVal.retryType + '<' + oldVal.retryType);
}
if (!Objects.equals(oldVal.retryInterval, newVal.retryInterval)) {
diff.add("retryInterval=" +
newVal.retryInterval + '<' + oldVal.retryInterval);
}
return diff.toString();
};
}
@Nonnull
@Override
protected FailureSettings getThis() {
return this;
}
@Override
protected void copyFields(@Nonnull FailureSettings newValue) {
this.retryType = newValue.retryType;
this.retryInterval = newValue.retryInterval;
}
/**
* A builder for failure settings.
*/
public static class Builder {
private FailRetryType retryType;
private Duration retryInterval;
/**
* Set task execution retry strategy in case of failure.
*
* @param retryType Task execution retry strategy.
* @return Reference to the same builder.
*/
public Builder withRetryType(@Nonnull FailRetryType retryType) {
this.retryType = retryType;
return this;
}
/**
* Set retry interval for task execution in case of failure.
*
* @param retryInterval Task retry interval.
* @return Reference to the same builder.
*/
public Builder withRetryInterval(@Nonnull Duration retryInterval) {
this.retryInterval = retryInterval;
return this;
}
/**
* Create new failure settings object.
*
* @return A new failure settings object.
*/
public FailureSettings build() {
return new FailureSettings(retryType, retryInterval);
}
}
}