-
Notifications
You must be signed in to change notification settings - Fork 13
/
PrefetchingQueueListenerParser.java
228 lines (206 loc) · 10.1 KB
/
PrefetchingQueueListenerParser.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.jashmore.sqs.spring.container.prefetch;
import com.jashmore.documentation.annotations.Nullable;
import com.jashmore.documentation.annotations.Positive;
import com.jashmore.documentation.annotations.PositiveOrZero;
import com.jashmore.sqs.container.prefetching.PrefetchingMessageListenerContainerProperties;
import com.jashmore.sqs.spring.container.CoreAnnotationParser;
import java.time.Duration;
import java.util.function.Supplier;
import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils;
/**
* Parser that is used to transform a {@link PrefetchingQueueListener} annotation to a {@link PrefetchingMessageListenerContainerProperties}.
*/
public class PrefetchingQueueListenerParser
implements CoreAnnotationParser<PrefetchingQueueListener, PrefetchingMessageListenerContainerProperties> {
private final Environment environment;
public PrefetchingQueueListenerParser(final Environment environment) {
this.environment = environment;
}
@Override
public PrefetchingMessageListenerContainerProperties parse(final PrefetchingQueueListener annotation) {
final Supplier<Integer> concurrencySupplier = concurrencySupplier(annotation);
final Supplier<Duration> concurrencyPollingRateSupplier = concurrencyPollingRateSupplier(annotation);
final Supplier<Integer> desiredPrefetchedMessagesSupplier = desiredMinPrefetchedMessagesSupplier(annotation);
final Supplier<Integer> maxPrefetchedMessagesSupplier = maxPrefetchedMessagesSupplier(annotation);
final Supplier<Duration> errorBackoffTimeSupplier = errorBackoffTimeSupplier(annotation);
final Supplier<Duration> messageVisibilityTimeoutSupplier = messageVisibilityTimeoutSupplier(annotation);
final Supplier<Boolean> tryAndProcessAnyExtraRetrievedMessagesOnShutdownSupplier = tryAndProcessAnyExtraRetrievedMessagesOnShutdownSupplier(
annotation
);
final Supplier<Boolean> interruptThreadsProcessingMessagesOnShutdownSupplier = interruptThreadsProcessingMessagesOnShutdownSupplier(
annotation
);
return new PrefetchingMessageListenerContainerProperties() {
@PositiveOrZero
@Override
public int concurrencyLevel() {
return concurrencySupplier.get();
}
@Nullable
@Override
public Duration concurrencyPollingRate() {
return concurrencyPollingRateSupplier.get();
}
@Nullable
@Override
public Duration errorBackoffTime() {
return errorBackoffTimeSupplier.get();
}
@Positive
@Override
public int desiredMinPrefetchedMessages() {
return desiredPrefetchedMessagesSupplier.get();
}
@Positive
@Override
public int maxPrefetchedMessages() {
return maxPrefetchedMessagesSupplier.get();
}
@Nullable
@Positive
@Override
public Duration messageVisibilityTimeout() {
return messageVisibilityTimeoutSupplier.get();
}
@Override
public boolean processAnyExtraRetrievedMessagesOnShutdown() {
return tryAndProcessAnyExtraRetrievedMessagesOnShutdownSupplier.get();
}
@Override
public boolean interruptThreadsProcessingMessagesOnShutdown() {
return interruptThreadsProcessingMessagesOnShutdownSupplier.get();
}
};
}
/**
* Parse the annotation to construct a supplier that returns the concurrency rate for the listener.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the concurrency supplier
* @see PrefetchingMessageListenerContainerProperties#concurrencyLevel() for more details
*/
protected Supplier<Integer> concurrencySupplier(final PrefetchingQueueListener annotation) {
final int concurrencyLevel;
if (!StringUtils.hasText(annotation.concurrencyLevelString())) {
concurrencyLevel = annotation.concurrencyLevel();
} else {
concurrencyLevel = Integer.parseInt(environment.resolvePlaceholders(annotation.concurrencyLevelString()));
}
return () -> concurrencyLevel;
}
/**
* Parse the annotation to construct a supplier that returns the concurrency polling rate for the listener.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the concurrency supplier
* @see PrefetchingMessageListenerContainerProperties#concurrencyPollingRate() for more details
*/
protected Supplier<Duration> concurrencyPollingRateSupplier(final PrefetchingQueueListener annotation) {
return () -> null;
}
/**
* Parse the annotation to construct a supplier that returns the minimum desired messages to be prefetched.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the message visibility timeout supplier
* @see PrefetchingMessageListenerContainerProperties#desiredMinPrefetchedMessages() for more details
*/
protected Supplier<Integer> desiredMinPrefetchedMessagesSupplier(final PrefetchingQueueListener annotation) {
final int desiredMinPrefetchedMessages;
if (!StringUtils.hasText(annotation.desiredMinPrefetchedMessagesString())) {
desiredMinPrefetchedMessages = annotation.desiredMinPrefetchedMessages();
} else {
desiredMinPrefetchedMessages =
Integer.parseInt(environment.resolvePlaceholders(annotation.desiredMinPrefetchedMessagesString()));
}
return () -> desiredMinPrefetchedMessages;
}
/**
* Parse the annotation to construct a supplier that returns the maximum number of messages that can be prefetched.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the max prefetched messages supplier
* @see PrefetchingMessageListenerContainerProperties#maxPrefetchedMessages() for more details
*/
protected Supplier<Integer> maxPrefetchedMessagesSupplier(final PrefetchingQueueListener annotation) {
final int maxPrefetchedMessages;
if (!StringUtils.hasText(annotation.maxPrefetchedMessagesString())) {
maxPrefetchedMessages = annotation.maxPrefetchedMessages();
} else {
maxPrefetchedMessages = Integer.parseInt(environment.resolvePlaceholders(annotation.maxPrefetchedMessagesString()));
}
return () -> maxPrefetchedMessages;
}
/**
* Parse the annotation to construct a supplier that returns the duration the container should back off if there was an error handling messages within
* the framework.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the backoff time supplier
* @see PrefetchingMessageListenerContainerProperties#errorBackoffTime() for more details
*/
protected Supplier<Duration> errorBackoffTimeSupplier(final PrefetchingQueueListener annotation) {
return () -> null;
}
/**
* Parse the annotation to construct a supplier that returns the duration that the message should be invisible from other consumers.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the message visibility timeout supplier
* @see PrefetchingMessageListenerContainerProperties#messageVisibilityTimeout() for more details
*/
protected Supplier<Duration> messageVisibilityTimeoutSupplier(final PrefetchingQueueListener annotation) {
final Duration messageVisibilityTimeout;
if (!StringUtils.hasText(annotation.messageVisibilityTimeoutInSecondsString())) {
final int visibilityTimeout = annotation.messageVisibilityTimeoutInSeconds();
if (visibilityTimeout < 0) {
messageVisibilityTimeout = null;
} else {
messageVisibilityTimeout = Duration.ofSeconds(visibilityTimeout);
}
} else {
messageVisibilityTimeout =
Duration.ofSeconds(Integer.parseInt(environment.resolvePlaceholders(annotation.messageVisibilityTimeoutInSecondsString())));
}
return () -> messageVisibilityTimeout;
}
/**
* Parse the annotation to construct a supplier that returns whether any extra messages downloaded should be processed before shutdown.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the process extra messages supplier
* @see PrefetchingMessageListenerContainerProperties#processAnyExtraRetrievedMessagesOnShutdown() () for more details
*/
protected Supplier<Boolean> tryAndProcessAnyExtraRetrievedMessagesOnShutdownSupplier(final PrefetchingQueueListener annotation) {
final boolean tryAndProcessAnyExtraRetrievedMessagesOnShutdown = annotation.processAnyExtraRetrievedMessagesOnShutdown();
return () -> tryAndProcessAnyExtraRetrievedMessagesOnShutdown;
}
/**
* Parse the annotation to construct a supplier that returns whether currently processing messages should be interrupted on shutdown.
*
* <p>Can be overridden to provide custom logic.
*
* @param annotation the annotation to parse
* @return the interrupt message processing on shutdown supplier
* @see PrefetchingMessageListenerContainerProperties#interruptThreadsProcessingMessagesOnShutdown() for more details
*/
protected Supplier<Boolean> interruptThreadsProcessingMessagesOnShutdownSupplier(final PrefetchingQueueListener annotation) {
final boolean interruptThreadsProcessingMessagesOnShutdown = annotation.interruptThreadsProcessingMessagesOnShutdown();
return () -> interruptThreadsProcessingMessagesOnShutdown;
}
}