From 09b57eff766a04bc9286f89da3242eb710f6f72f Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Tue, 17 Sep 2024 18:42:54 +0300 Subject: [PATCH 1/2] Add option for configuring max messages per task See gh-42341 --- ...efaultJmsListenerContainerFactoryConfigurer.java | 1 + .../boot/autoconfigure/jms/JmsProperties.java | 13 +++++++++++++ .../jms/JmsAutoConfigurationTests.java | 4 +++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java index 66ab3db1ee6c..be9d767d0f26 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java @@ -135,6 +135,7 @@ public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFact map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup); map.from(listenerProperties::formatConcurrency).to(factory::setConcurrency); map.from(listenerProperties::getReceiveTimeout).as(Duration::toMillis).to(factory::setReceiveTimeout); + map.from(listenerProperties::getMaxMessagesPerTask).to(factory::setMaxMessagesPerTask); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java index 7d87ec9169cb..051c12ef6b81 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java @@ -186,6 +186,11 @@ public static class Listener { */ private Duration receiveTimeout = Duration.ofSeconds(1); + /** + * Specify the maximum number of messages to process in one task. + */ + private Integer maxMessagesPerTask; + private final Session session = new Session(); public boolean isAutoStartup() { @@ -250,6 +255,14 @@ public void setReceiveTimeout(Duration receiveTimeout) { this.receiveTimeout = receiveTimeout; } + public Integer getMaxMessagesPerTask() { + return this.maxMessagesPerTask; + } + + public void setMaxMessagesPerTask(Integer maxMessagesPerTask) { + this.maxMessagesPerTask = maxMessagesPerTask; + } + public Session getSession() { return this.session; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index 906b9f390ffd..91d0589e7591 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -176,7 +176,8 @@ void testJmsListenerContainerFactoryWithCustomSettings() { "spring.jms.listener.session.acknowledgeMode=client", "spring.jms.listener.session.transacted=false", "spring.jms.listener.minConcurrency=2", "spring.jms.listener.receiveTimeout=2s", "spring.jms.listener.maxConcurrency=10", - "spring.jms.subscription-durable=true", "spring.jms.client-id=exampleId") + "spring.jms.subscription-durable=true", "spring.jms.client-id=exampleId", + "spring.jms.listener.max-messages-per-task=10") .run(this::testJmsListenerContainerFactoryWithCustomSettings); } @@ -188,6 +189,7 @@ private void testJmsListenerContainerFactoryWithCustomSettings(AssertableApplica assertThat(container.getConcurrentConsumers()).isEqualTo(2); assertThat(container.getMaxConcurrentConsumers()).isEqualTo(10); assertThat(container).hasFieldOrPropertyWithValue("receiveTimeout", 2000L); + assertThat(container).hasFieldOrPropertyWithValue("maxMessagesPerTask", 10); assertThat(container.isSubscriptionDurable()).isTrue(); assertThat(container.getClientId()).isEqualTo("exampleId"); } From e930a963aded6ba4374e615d27c1ef0f6e9eec68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Thu, 19 Sep 2024 14:19:01 +0200 Subject: [PATCH 2/2] Polish "Add option for configuring max messages per task" See gh-42341 --- .../springframework/boot/autoconfigure/jms/JmsProperties.java | 4 +++- .../boot/autoconfigure/jms/JmsAutoConfigurationTests.java | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java index 051c12ef6b81..aaf6c8a9ada6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java @@ -187,7 +187,9 @@ public static class Listener { private Duration receiveTimeout = Duration.ofSeconds(1); /** - * Specify the maximum number of messages to process in one task. + * Specify the maximum number of messages to process in one task. By default, + * unlimited unless a SchedulingTaskExecutor is configured on the listener (10 + * messages), as it indicates a preference for short-lived tasks. */ private Integer maxMessagesPerTask; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java index 91d0589e7591..7df4a2ad49c7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfigurationTests.java @@ -177,7 +177,7 @@ void testJmsListenerContainerFactoryWithCustomSettings() { "spring.jms.listener.session.transacted=false", "spring.jms.listener.minConcurrency=2", "spring.jms.listener.receiveTimeout=2s", "spring.jms.listener.maxConcurrency=10", "spring.jms.subscription-durable=true", "spring.jms.client-id=exampleId", - "spring.jms.listener.max-messages-per-task=10") + "spring.jms.listener.max-messages-per-task=5") .run(this::testJmsListenerContainerFactoryWithCustomSettings); } @@ -189,7 +189,7 @@ private void testJmsListenerContainerFactoryWithCustomSettings(AssertableApplica assertThat(container.getConcurrentConsumers()).isEqualTo(2); assertThat(container.getMaxConcurrentConsumers()).isEqualTo(10); assertThat(container).hasFieldOrPropertyWithValue("receiveTimeout", 2000L); - assertThat(container).hasFieldOrPropertyWithValue("maxMessagesPerTask", 10); + assertThat(container).hasFieldOrPropertyWithValue("maxMessagesPerTask", 5); assertThat(container.isSubscriptionDurable()).isTrue(); assertThat(container.getClientId()).isEqualTo("exampleId"); }