|
15 | 15 | */
|
16 | 16 | package com.ibm.eventstreams.connect.mqsource;
|
17 | 17 |
|
18 |
| -import com.ibm.eventstreams.connect.mqsource.builders.RecordBuilderException; |
19 |
| -import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState; |
20 |
| -import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceStateClient; |
21 |
| -import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceStateException; |
22 |
| -import com.ibm.eventstreams.connect.mqsource.util.LogMessages; |
23 |
| -import com.ibm.eventstreams.connect.mqsource.util.ExceptionProcessor; |
24 |
| -import com.ibm.eventstreams.connect.mqsource.util.QueueConfig; |
25 |
| -import org.apache.kafka.common.config.AbstractConfig; |
26 |
| -import org.apache.kafka.connect.errors.ConnectException; |
27 |
| -import org.apache.kafka.connect.source.SourceRecord; |
28 |
| -import org.apache.kafka.connect.source.SourceTask; |
29 |
| -import org.slf4j.Logger; |
30 |
| -import org.slf4j.LoggerFactory; |
31 |
| - |
32 |
| -import javax.jms.JMSException; |
33 |
| -import javax.jms.JMSRuntimeException; |
34 |
| -import javax.jms.Message; |
| 18 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_MAX_POLL_BLOCKED_TIME_MS; |
| 19 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_MAX_POLL_TIME; |
| 20 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_BATCH_SIZE; |
| 21 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_EXACTLY_ONCE_STATE_QUEUE; |
| 22 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_QUEUE; |
| 23 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_QUEUE_MANAGER; |
| 24 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_VALUE_MQ_BATCH_SIZE_DEFAULT; |
| 25 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.NORMAL_OPERATION; |
| 26 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.REDELIVER_UNSENT_BATCH; |
| 27 | +import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.REMOVE_DELIVERED_MESSAGES_FROM_SOURCE_QUEUE; |
| 28 | +import static com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState.LastKnownState.DELIVERED; |
| 29 | +import static com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState.LastKnownState.IN_FLIGHT; |
35 | 30 |
|
36 | 31 | import java.util.ArrayList;
|
37 | 32 | import java.util.Collections;
|
|
47 | 42 | import java.util.function.Consumer;
|
48 | 43 | import java.util.stream.Collectors;
|
49 | 44 |
|
50 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_MAX_POLL_BLOCKED_TIME_MS; |
51 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_BATCH_SIZE; |
52 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_EXACTLY_ONCE_STATE_QUEUE; |
53 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_QUEUE; |
54 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_NAME_MQ_QUEUE_MANAGER; |
55 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceConnector.CONFIG_VALUE_MQ_BATCH_SIZE_DEFAULT; |
| 45 | +import javax.jms.JMSException; |
| 46 | +import javax.jms.JMSRuntimeException; |
| 47 | +import javax.jms.Message; |
56 | 48 |
|
57 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.REMOVE_DELIVERED_MESSAGES_FROM_SOURCE_QUEUE; |
58 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.NORMAL_OPERATION; |
59 |
| -import static com.ibm.eventstreams.connect.mqsource.MQSourceTaskStartUpAction.REDELIVER_UNSENT_BATCH; |
60 |
| -import static com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState.LastKnownState.DELIVERED; |
61 |
| -import static com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState.LastKnownState.IN_FLIGHT; |
| 49 | +import org.apache.kafka.common.config.AbstractConfig; |
| 50 | +import org.apache.kafka.connect.errors.ConnectException; |
| 51 | +import org.apache.kafka.connect.source.SourceRecord; |
| 52 | +import org.apache.kafka.connect.source.SourceTask; |
| 53 | +import org.slf4j.Logger; |
| 54 | +import org.slf4j.LoggerFactory; |
| 55 | + |
| 56 | +import com.ibm.eventstreams.connect.mqsource.builders.RecordBuilderException; |
| 57 | +import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceState; |
| 58 | +import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceStateClient; |
| 59 | +import com.ibm.eventstreams.connect.mqsource.sequencestate.SequenceStateException; |
| 60 | +import com.ibm.eventstreams.connect.mqsource.util.ExceptionProcessor; |
| 61 | +import com.ibm.eventstreams.connect.mqsource.util.LogMessages; |
| 62 | +import com.ibm.eventstreams.connect.mqsource.util.QueueConfig; |
62 | 63 |
|
63 | 64 | public class MQSourceTask extends SourceTask {
|
64 | 65 | private static final Logger log = LoggerFactory.getLogger(MQSourceTask.class);
|
65 | 66 |
|
66 | 67 | // The maximum number of records returned per call to poll()
|
67 | 68 | private int batchSize = CONFIG_VALUE_MQ_BATCH_SIZE_DEFAULT;
|
| 69 | + // The maximum time to spend polling messages before returning a batch |
| 70 | + private long maxPollTime = CONFIG_VALUE_MQ_BATCH_SIZE_DEFAULT; |
68 | 71 |
|
69 | 72 | // Used to signal completion of a batch
|
70 | 73 | // After returning a batch of messages to Connect, the SourceTask waits
|
@@ -174,6 +177,7 @@ protected void start(final Map<String, String> props, final JMSWorker reader, fi
|
174 | 177 | startUpAction = NORMAL_OPERATION;
|
175 | 178 |
|
176 | 179 | batchSize = config.getInt(CONFIG_NAME_MQ_BATCH_SIZE);
|
| 180 | + maxPollTime = config.getLong(CONFIG_MAX_POLL_TIME); |
177 | 181 | try {
|
178 | 182 | reader.configure(config);
|
179 | 183 | reader.connect();
|
@@ -415,22 +419,31 @@ private void initOrResetBatchCompleteSignal(final boolean predicate, final List<
|
415 | 419 |
|
416 | 420 | private List<Message> pollSourceQueue(final int numberOfMessagesToBePolled) throws JMSException {
|
417 | 421 | final List<Message> localList = new ArrayList<>();
|
418 |
| - |
419 |
| - if (!stopNow.get()) { |
420 |
| - log.debug("Polling for records"); |
421 |
| - Message message; |
422 |
| - do { |
423 |
| - message = reader.receive(sourceQueue, sourceQueueConfig, localList.size() == 0); |
424 |
| - if (message != null) { |
425 |
| - localList.add(message); |
426 |
| - } |
427 |
| - } while (message != null && localList.size() < numberOfMessagesToBePolled && !stopNow.get()); |
428 |
| - } else { |
| 422 | + if (stopNow.get()) { |
429 | 423 | log.info("Stopping polling for records");
|
| 424 | + return localList; |
430 | 425 | }
|
| 426 | + |
| 427 | + log.debug("Polling for records"); |
| 428 | + final long startTime = System.currentTimeMillis(); |
| 429 | + |
| 430 | + Message message; |
| 431 | + do { |
| 432 | + message = reader.receive(sourceQueue, sourceQueueConfig, localList.isEmpty()); |
| 433 | + if (message != null) { |
| 434 | + localList.add(message); |
| 435 | + } |
| 436 | + } while ( |
| 437 | + message != null && |
| 438 | + localList.size() < numberOfMessagesToBePolled && |
| 439 | + !stopNow.get() && |
| 440 | + (maxPollTime <= 0 || (System.currentTimeMillis() - startTime) < maxPollTime) |
| 441 | + ); |
| 442 | + |
431 | 443 | return localList;
|
432 | 444 | }
|
433 | 445 |
|
| 446 | + |
434 | 447 | private boolean isFirstMsgOnSourceQueueARequiredMsg(final List<String> msgIds) throws JMSException {
|
435 | 448 | final Message message = reader.browse(sourceQueue).get();
|
436 | 449 | return msgIds.contains(message.getJMSMessageID());
|
|
0 commit comments