-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathSQSMessageConsumer.java
More file actions
280 lines (243 loc) · 10.1 KB
/
SQSMessageConsumer.java
File metadata and controls
280 lines (243 loc) · 10.1 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright 2010-2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazon.sqs.javamessaging;
import com.amazon.sqs.javamessaging.acknowledge.Acknowledger;
import com.amazon.sqs.javamessaging.acknowledge.NegativeAcknowledger;
import com.amazon.sqs.javamessaging.acknowledge.SQSMessageIdentifier;
import com.amazon.sqs.javamessaging.message.SQSMessage;
import jakarta.jms.IllegalStateException;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.jms.MessageConsumer;
import jakarta.jms.MessageListener;
import jakarta.jms.QueueReceiver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* A client uses a MessageConsumer object to receive messages from a
* destination. A MessageConsumer object is created by passing a Destination
* object to a message-consumer creation method supplied by a session.
* <P>
* This message consumer does not support message selectors
* <P>
* A client may either synchronously receive a message consumer's messages or
* have the consumer asynchronously deliver them as they arrive via registering
* a MessageListener object.
* <P>
* The message consumer creates a background thread to prefetch the messages to
* improve the <code>receive</code> turn-around times.
*/
public class SQSMessageConsumer implements MessageConsumer, QueueReceiver {
private static final Logger LOG = LoggerFactory.getLogger(SQSMessageConsumer.class);
public static final int PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME = 30;
protected volatile boolean closed = false;
private final SQSQueueDestination sqsDestination;
private final SQSSession parentSQSSession;
private final SQSSessionCallbackScheduler sqsSessionRunnable;
/**
* Executor for prefetch thread.
*/
private final ExecutorService prefetchExecutor;
/**
* Prefetch Runnable. This includes keeping internal message buffer filled and call MessageListener if set.
*/
private final SQSMessageConsumerPrefetch sqsMessageConsumerPrefetch;
SQSMessageConsumer(SQSConnection parentSQSConnection, SQSSession parentSQSSession,
SQSSessionCallbackScheduler sqsSessionRunnable, SQSQueueDestination destination,
Acknowledger acknowledger, NegativeAcknowledger negativeAcknowledger, ThreadFactory threadFactory) {
this(parentSQSConnection, parentSQSSession,
sqsSessionRunnable, destination,
acknowledger, negativeAcknowledger, threadFactory,
new SQSMessageConsumerPrefetch(sqsSessionRunnable, acknowledger, negativeAcknowledger, destination,
parentSQSConnection.getWrappedAmazonSQSClient(),
parentSQSConnection.getNumberOfMessagesToPrefetch()));
}
SQSMessageConsumer(SQSConnection parentSQSConnection, SQSSession parentSQSSession,
SQSSessionCallbackScheduler sqsSessionRunnable, SQSQueueDestination destination,
Acknowledger acknowledger, NegativeAcknowledger negativeAcknowledger, ThreadFactory threadFactory,
SQSMessageConsumerPrefetch sqsMessageConsumerPrefetch) {
this.parentSQSSession = parentSQSSession;
this.sqsDestination = destination;
this.sqsSessionRunnable = sqsSessionRunnable;
this.sqsMessageConsumerPrefetch = sqsMessageConsumerPrefetch;
this.sqsMessageConsumerPrefetch.setMessageConsumer(this);
prefetchExecutor = Executors.newSingleThreadExecutor(threadFactory);
prefetchExecutor.execute(sqsMessageConsumerPrefetch);
}
/**
* Gets the queue destination associated with this queue receiver, where the
* messages are delivered from.
*
* @return a queue destination
*/
@Override
public SQSQueueDestination getQueue() throws JMSException {
return sqsDestination;
}
/**
* Gets the message consumer's MessageListener.
*
* @return a message listener
*/
@Override
public MessageListener getMessageListener() throws JMSException {
return sqsMessageConsumerPrefetch.getMessageListener();
}
/**
* Sets the message consumer's MessageListener.
*
* @param listener
* a message listener to use for asynchronous message delivery
* @throws JMSException
* If the message consumer is closed
*/
@Override
public void setMessageListener(MessageListener listener) throws JMSException {
checkClosed();
this.sqsMessageConsumerPrefetch.setMessageListener(listener);
}
/**
* This call blocks indefinitely until a message is produced or until this
* message consumer is closed. When ConnectionState is stopped receive is
* paused.
*
* @return the next message produced for this message consumer, or null if
* this message consumer is closed during the receive call
* @throws JMSException
* On internal error
*/
@Override
public SQSMessage receive() throws JMSException {
checkClosed();
return sqsMessageConsumerPrefetch.receive();
}
/**
* This call blocks until a message arrives, the timeout expires, or this
* message consumer is closed. A timeout of zero never expires, and the call
* blocks indefinitely.
*
* @param timeout
* the timeout value (in milliseconds)
* @return the next message produced for this message consumer, or null if
* the timeout expires or this message consumer is closed during the receive call
* @throws JMSException
* On internal error
*/
@Override
public Message receive(long timeout) throws JMSException {
checkClosed();
return sqsMessageConsumerPrefetch.receive(timeout);
}
/**
* Receives the next message if one is immediately available.
*
* @return the next message produced for this message consumer, or null if
* no message is available
* @throws JMSException
* On internal error
*/
@Override
public Message receiveNoWait() throws JMSException {
checkClosed();
return sqsMessageConsumerPrefetch.receiveNoWait();
}
/**
* Closes the message consumer.
* <P>
* This will not return until receives and/or message listeners in progress
* have completed. A blocked message consumer receive call returns null when
* this consumer is closed.
* <P>
* Since consumer prefetch threads use SQS long-poll feature with 20 seconds
* timeout, closing each consumer prefetch thread can take up to 20 seconds,
* which in-turn will impact the time on consumer close.
* <P>
* This method may be called from a message listener's onMessage method on
* its own consumer. After this method returns the onMessage method will be
* allowed to complete normally, and the callback scheduler thread will be
* closing the message consumer.
*
* @throws JMSException
* On internal error.
*/
@Override
public void close() throws JMSException {
if (closed) {
return;
}
if (parentSQSSession.isActiveCallbackSessionThread()) {
sqsSessionRunnable.setConsumerCloseAfterCallback(this);
return;
}
doClose();
}
void doClose() {
if (closed) {
return;
}
sqsMessageConsumerPrefetch.close();
parentSQSSession.removeConsumer(this);
try {
if (!prefetchExecutor.isShutdown()) {
LOG.debug("Shutting down {} executor", SQSSession.CONSUMER_PREFETCH_EXECUTOR_NAME);
// Shut down executor.
prefetchExecutor.shutdown();
}
parentSQSSession.waitForConsumerCallbackToComplete(this);
if (!prefetchExecutor.awaitTermination(PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME, TimeUnit.SECONDS)) {
LOG.warn("Can't terminate executor service {} after {} seconds, some running threads will be shutdown immediately",
SQSSession.CONSUMER_PREFETCH_EXECUTOR_NAME, PREFETCH_EXECUTOR_GRACEFUL_SHUTDOWN_TIME);
prefetchExecutor.shutdownNow();
}
} catch (InterruptedException e) {
LOG.error("Interrupted while closing the consumer.", e);
}
closed = true;
}
boolean isClosed() {
return closed;
}
/** This method is not supported. */
@Override
public String getMessageSelector() throws JMSException {
throw new JMSException(SQSMessagingClientConstants.UNSUPPORTED_METHOD);
}
/** This stops the prefetching */
protected void stopPrefetch() {
if (!closed) {
sqsMessageConsumerPrefetch.stop();
}
}
/** This starts the prefetching */
protected void startPrefetch() {
if (!closed) {
sqsMessageConsumerPrefetch.start();
}
}
private void checkClosed() throws IllegalStateException {
if (closed) {
throw new IllegalStateException("Consumer is closed");
}
}
List<SQSMessageIdentifier> purgePrefetchedMessagesWithGroups(Set<String> affectedGroups) throws JMSException {
return sqsMessageConsumerPrefetch.purgePrefetchedMessagesWithGroups(affectedGroups);
}
}