Skip to content

Commit 1cde657

Browse files
ibmmqmetwilkinsona
authored andcommitted
Add a config property for JMS listener container's receive timeout
See gh-17332
1 parent 52479ee commit 1cde657

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.jms;
1818

19+
import java.time.Duration;
20+
1921
import javax.jms.ConnectionFactory;
2022

2123
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
@@ -107,6 +109,10 @@ public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFact
107109
if (concurrency != null) {
108110
factory.setConcurrency(concurrency);
109111
}
112+
Duration receiveTimeout = listener.getReceiveTimeout();
113+
if (receiveTimeout != null) {
114+
factory.setReceiveTimeout(receiveTimeout.toMillis());
115+
}
110116
}
111117

112118
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ public static class Listener {
155155
*/
156156
private Integer maxConcurrency;
157157

158+
/**
159+
* Timeout to use for receive calls. By default, the listener uses a 1s timeout on
160+
* its polling loop. See
161+
* @see org.springframework.jms.listener.AbstractPollingMessageListenerContainer#setReceiveTimeout
162+
* for more details on this value and the meaning of special values 0 and -1.
163+
*/
164+
private Duration receiveTimeout;
165+
158166
public boolean isAutoStartup() {
159167
return this.autoStartup;
160168
}
@@ -195,6 +203,14 @@ public String formatConcurrency() {
195203
: String.valueOf(this.concurrency));
196204
}
197205

206+
public Duration getReceiveTimeout() {
207+
return this.receiveTimeout;
208+
}
209+
210+
public void setReceiveTimeout(Duration receiveTimeout) {
211+
this.receiveTimeout = receiveTimeout;
212+
}
213+
198214
}
199215

200216
public static class Template {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void testJmsListenerContainerFactoryWithCustomSettings() {
147147
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
148148
.withPropertyValues("spring.jms.listener.autoStartup=false",
149149
"spring.jms.listener.acknowledgeMode=client", "spring.jms.listener.concurrency=2",
150-
"spring.jms.listener.maxConcurrency=10")
150+
"spring.jms.listener.receiveTimeout=2s", "spring.jms.listener.maxConcurrency=10")
151151
.run(this::testJmsListenerContainerFactoryWithCustomSettings);
152152
}
153153

@@ -157,6 +157,18 @@ private void testJmsListenerContainerFactoryWithCustomSettings(AssertableApplica
157157
assertThat(container.getSessionAcknowledgeMode()).isEqualTo(Session.CLIENT_ACKNOWLEDGE);
158158
assertThat(container.getConcurrentConsumers()).isEqualTo(2);
159159
assertThat(container.getMaxConcurrentConsumers()).isEqualTo(10);
160+
assertThat(container).hasFieldOrPropertyWithValue("receiveTimeout", 2000L);
161+
}
162+
163+
@Test
164+
void testJmsListenerContainerFactoryWithDefaultSettings() {
165+
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
166+
.run(this::testJmsListenerContainerFactoryWithDefaultSettings);
167+
}
168+
169+
private void testJmsListenerContainerFactoryWithDefaultSettings(AssertableApplicationContext loaded) {
170+
DefaultMessageListenerContainer container = getContainer(loaded, "jmsListenerContainerFactory");
171+
assertThat(container).hasFieldOrPropertyWithValue("receiveTimeout", 1000L);
160172
}
161173

162174
@Test

0 commit comments

Comments
 (0)