Skip to content

Commit 87b93a7

Browse files
committed
Aligned JmsTemplate and DefaultMessageListenerContainer receiveTimeout values
Issue: SPR-14212
1 parent 65a8f5e commit 87b93a7

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

spring-jms/src/main/java/org/springframework/jms/core/JmsTemplate.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,18 +89,6 @@
8989
*/
9090
public class JmsTemplate extends JmsDestinationAccessor implements JmsOperations {
9191

92-
/**
93-
* Timeout value indicating that a receive operation should
94-
* check if a message is immediately available without blocking.
95-
*/
96-
public static final long RECEIVE_TIMEOUT_NO_WAIT = -1;
97-
98-
/**
99-
* Timeout value indicating a blocking receive without timeout.
100-
*/
101-
public static final long RECEIVE_TIMEOUT_INDEFINITE_WAIT = 0;
102-
103-
10492
/** The JMS 2.0 MessageProducer.setDeliveryDelay method, if available */
10593
private static final Method setDeliveryDelayMethod =
10694
ClassUtils.getMethodIfAvailable(MessageProducer.class, "setDeliveryDelay", long.class);
@@ -315,11 +303,13 @@ public boolean isPubSubNoLocal() {
315303
* Set the timeout to use for receive calls (in milliseconds).
316304
* <p>The default is {@link #RECEIVE_TIMEOUT_INDEFINITE_WAIT}, which indicates
317305
* a blocking receive without timeout.
318-
* <p>Specify {@link #RECEIVE_TIMEOUT_NO_WAIT} to inidicate that a receive operation
319-
* should check if a message is immediately available without blocking.
306+
* <p>Specify {@link #RECEIVE_TIMEOUT_NO_WAIT} (or any other negative value)
307+
* to indicate that a receive operation should check if a message is
308+
* immediately available without blocking.
309+
* @see #receiveFromConsumer(MessageConsumer, long)
320310
* @see javax.jms.MessageConsumer#receive(long)
321-
* @see javax.jms.MessageConsumer#receive()
322311
* @see javax.jms.MessageConsumer#receiveNoWait()
312+
* @see javax.jms.MessageConsumer#receive()
323313
*/
324314
public void setReceiveTimeout(long receiveTimeout) {
325315
this.receiveTimeout = receiveTimeout;
@@ -800,7 +790,7 @@ protected Message doReceive(Session session, MessageConsumer consumer) throws JM
800790
if (resourceHolder != null && resourceHolder.hasTimeout()) {
801791
timeout = Math.min(timeout, resourceHolder.getTimeToLiveInMillis());
802792
}
803-
Message message = doReceive(consumer, timeout);
793+
Message message = receiveFromConsumer(consumer, timeout);
804794
if (session.getTransacted()) {
805795
// Commit necessary - but avoid commit call within a JTA transaction.
806796
if (isSessionLocallyTransacted(session)) {
@@ -821,25 +811,6 @@ else if (isClientAcknowledge(session)) {
821811
}
822812
}
823813

824-
/**
825-
* Actually receive a message from the given consumer.
826-
* @param consumer the JMS MessageConsumer to receive with
827-
* @param timeout the receive timeout
828-
* @return the JMS Message received, or {@code null} if none
829-
* @throws JMSException if thrown by JMS API methods
830-
*/
831-
private Message doReceive(MessageConsumer consumer, long timeout) throws JMSException {
832-
if (timeout == RECEIVE_TIMEOUT_NO_WAIT) {
833-
return consumer.receiveNoWait();
834-
}
835-
else if (timeout > 0) {
836-
return consumer.receive(timeout);
837-
}
838-
else {
839-
return consumer.receive();
840-
}
841-
}
842-
843814

844815
//---------------------------------------------------------------------------------------
845816
// Convenience methods for receiving auto-converted messages
@@ -952,7 +923,7 @@ protected Message doSendAndReceive(Session session, Destination destination, Mes
952923
logger.debug("Sending created message: " + requestMessage);
953924
}
954925
doSend(producer, requestMessage);
955-
return doReceive(consumer, getReceiveTimeout());
926+
return receiveFromConsumer(consumer, getReceiveTimeout());
956927
}
957928
finally {
958929
JmsUtils.closeMessageConsumer(consumer);

spring-jms/src/main/java/org/springframework/jms/listener/AbstractPollingMessageListenerContainer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,13 @@ public void setTransactionTimeout(int transactionTimeout) {
158158
* The default is 1000 ms, that is, 1 second.
159159
* <p><b>NOTE:</b> This value needs to be smaller than the transaction
160160
* timeout used by the transaction manager (in the appropriate unit,
161-
* of course). -1 indicates no timeout at all; however, this is only
162-
* feasible if not running within a transaction manager.
161+
* of course). 0 indicates no timeout at all; however, this is only
162+
* feasible if not running within a transaction manager and generally
163+
* discouraged since such a listener container cannot cleanly shut down.
164+
* A negative value such as -1 indicates a no-wait receive operation.
165+
* @see #receiveFromConsumer(MessageConsumer, long)
163166
* @see javax.jms.MessageConsumer#receive(long)
167+
* @see javax.jms.MessageConsumer#receiveNoWait()
164168
* @see javax.jms.MessageConsumer#receive()
165169
* @see #setTransactionTimeout
166170
*/
@@ -417,7 +421,7 @@ private void rollbackOnException(TransactionStatus status, Throwable ex) {
417421
* @throws JMSException if thrown by JMS methods
418422
*/
419423
protected Message receiveMessage(MessageConsumer consumer) throws JMSException {
420-
return (this.receiveTimeout < 0 ? consumer.receive() : consumer.receive(this.receiveTimeout));
424+
return receiveFromConsumer(consumer, getReceiveTimeout());
421425
}
422426

423427
/**

spring-jms/src/main/java/org/springframework/jms/support/destination/JmsDestinationAccessor.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import javax.jms.Destination;
2020
import javax.jms.JMSException;
21+
import javax.jms.Message;
22+
import javax.jms.MessageConsumer;
2123
import javax.jms.Session;
2224

2325
import org.springframework.jms.support.JmsAccessor;
@@ -38,6 +40,20 @@
3840
*/
3941
public abstract class JmsDestinationAccessor extends JmsAccessor {
4042

43+
/**
44+
* Timeout value indicating that a receive operation should
45+
* check if a message is immediately available without blocking.
46+
* @since 4.3
47+
*/
48+
public static final long RECEIVE_TIMEOUT_NO_WAIT = -1;
49+
50+
/**
51+
* Timeout value indicating a blocking receive without timeout.
52+
* @since 4.3
53+
*/
54+
public static final long RECEIVE_TIMEOUT_INDEFINITE_WAIT = 0;
55+
56+
4157
private DestinationResolver destinationResolver = new DynamicDestinationResolver();
4258

4359
private boolean pubSubDomain = false;
@@ -98,4 +114,27 @@ protected Destination resolveDestinationName(Session session, String destination
98114
return getDestinationResolver().resolveDestinationName(session, destinationName, isPubSubDomain());
99115
}
100116

117+
/**
118+
* Actually receive a message from the given consumer.
119+
* @param consumer the JMS MessageConsumer to receive with
120+
* @param timeout the receive timeout (a negative value indicates
121+
* a no-wait receive; 0 indicates an indefinite wait attempt)
122+
* @return the JMS Message received, or {@code null} if none
123+
* @throws JMSException if thrown by JMS API methods
124+
* @since 4.3
125+
* @see #RECEIVE_TIMEOUT_NO_WAIT
126+
* @see #RECEIVE_TIMEOUT_INDEFINITE_WAIT
127+
*/
128+
protected Message receiveFromConsumer(MessageConsumer consumer, long timeout) throws JMSException {
129+
if (timeout > 0) {
130+
return consumer.receive(timeout);
131+
}
132+
else if (timeout < 0) {
133+
return consumer.receiveNoWait();
134+
}
135+
else {
136+
return consumer.receive();
137+
}
138+
}
139+
101140
}

0 commit comments

Comments
 (0)