@@ -71,12 +71,17 @@ public class JMSWorker {
71
71
private boolean connected = false ; // Whether connected to MQ
72
72
private AtomicBoolean closeNow ; // Whether close has been requested
73
73
private AbstractConfig config ;
74
- private long receiveTimeout ; // Receive timeout for the jms consumer
74
+ private long initialReceiveTimeoutMs ; // Receive timeout for the jms consumer
75
+ private long subsequentReceiveTimeoutMs ; // Receive timeout for the jms consumer on the subsequent calls
75
76
private long reconnectDelayMillisMin ; // Delay between repeated reconnect attempts min
76
77
private long reconnectDelayMillisMax ; // Delay between repeated reconnect attempts max
77
78
78
- long getReceiveTimeout () {
79
- return receiveTimeout ;
79
+ long getInitialReceiveTimeoutMs () {
80
+ return initialReceiveTimeoutMs ;
81
+ }
82
+
83
+ long getSubsequentReceiveTimeoutMs () {
84
+ return subsequentReceiveTimeoutMs ;
80
85
}
81
86
82
87
long getReconnectDelayMillisMin () {
@@ -144,7 +149,8 @@ public void configure(final AbstractConfig config) {
144
149
userName = config .getString (MQSourceConnector .CONFIG_NAME_MQ_USER_NAME );
145
150
password = config .getPassword (MQSourceConnector .CONFIG_NAME_MQ_PASSWORD );
146
151
topic = config .getString (MQSourceConnector .CONFIG_NAME_TOPIC );
147
- receiveTimeout = config .getLong (MQSourceConnector .CONFIG_MAX_RECEIVE_TIMEOUT );
152
+ initialReceiveTimeoutMs = config .getLong (MQSourceConnector .CONFIG_MAX_RECEIVE_TIMEOUT );
153
+ subsequentReceiveTimeoutMs = config .getLong (MQSourceConnector .CONFIG_SUBSEQUENT_RECEIVE_TIMEOUT );
148
154
reconnectDelayMillisMin = config .getLong (MQSourceConnector .CONFIG_RECONNECT_DELAY_MIN );
149
155
reconnectDelayMillisMax = config .getLong (MQSourceConnector .CONFIG_RECONNECT_DELAY_MAX );
150
156
} catch (JMSException | JMSRuntimeException jmse ) {
@@ -222,10 +228,14 @@ public void connect() {
222
228
*
223
229
* @param queueName The name of the queue to get messages from
224
230
* @param queueConfig Any particular queue configuration that should be applied
225
- * @param wait Whether to wait indefinitely for a message
231
+ * @param initialCall Indicates whether this is the initial receive call in the polling cycle.
232
+ * Determines which configured timeout to use:
233
+ * - If true, uses the initial receive timeout.
234
+ * - If false, uses the subsequent receive timeout.
235
+ * A timeout value of 0 results in a non-blocking receiveNoWait() call.
226
236
* @return The Message retrieved from MQ
227
237
*/
228
- public Message receive (final String queueName , final QueueConfig queueConfig , final boolean wait ) throws JMSRuntimeException , JMSException {
238
+ public Message receive (final String queueName , final QueueConfig queueConfig , final boolean initialCall ) throws JMSRuntimeException , JMSException {
229
239
log .trace ("[{}] Entry {}.receive" , Thread .currentThread ().getId (), this .getClass ().getName ());
230
240
231
241
if (!maybeReconnect ()) {
@@ -243,21 +253,25 @@ public Message receive(final String queueName, final QueueConfig queueConfig, fi
243
253
jmsConsumers .put (queueName , internalConsumer );
244
254
}
245
255
246
- Message message = null ;
247
- if (wait ) {
248
- log .debug ("Waiting {} ms for message" , receiveTimeout );
249
256
250
- message = internalConsumer .receive (receiveTimeout );
257
+ final long timeoutMs = initialCall
258
+ ? initialReceiveTimeoutMs
259
+ : subsequentReceiveTimeoutMs ;
260
+
261
+ Message message = null ;
262
+ if (timeoutMs > 0 ) {
263
+ // block up to timeoutMs
264
+ message = internalConsumer .receive (timeoutMs );
251
265
252
266
if (message == null ) {
253
- log .debug ("No message received" );
267
+ log .debug ("No message received within {} ms on queue={}" , timeoutMs , queueName );
254
268
}
255
269
} else {
270
+ // non‐blocking
256
271
message = internalConsumer .receiveNoWait ();
257
272
}
258
273
259
274
log .trace ("[{}] Exit {}.receive, retval={}" , Thread .currentThread ().getId (), this .getClass ().getName (), message );
260
-
261
275
return message ;
262
276
}
263
277
0 commit comments