Skip to content

Commit 2654900

Browse files
vpavicsnicoll
authored andcommitted
Add support for configuring SimpleMessageListenerContainer
This commit introduces a `SimpleJmsListenerContainerFactoryConfigurer` that can be used to conveniently apply settings provided by JMS auto-configuration to `SimpleJmsListenerContainerFactory` instances. See spring-projectsgh-47716 Signed-off-by: Vedran Pavic <vedran@vedranpavic.com>
1 parent 024bb72 commit 2654900

File tree

6 files changed

+200
-88
lines changed

6 files changed

+200
-88
lines changed

documentation/spring-boot-docs/src/docs/antora/modules/reference/pages/messaging/jms.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,8 @@ NOTE: In the example above, the customization uses javadoc:org.springframework.b
191191
Then you can use the factory in any javadoc:org.springframework.jms.annotation.JmsListener[format=annotation]-annotated method as follows:
192192

193193
include-code::custom/MyBean[]
194+
195+
Analogous to `DefaultJmsListenerContainerFactoryConfigurer`, Spring Boot also provides a javadoc:org.springframework.boot.jms.autoconfigure.SimpleJmsListenerContainerFactoryConfigurer[] that you can use to initialize a javadoc:org.springframework.jms.config.SimpleJmsListenerContainerFactory[] and apply the related settings that auto-configuration provides.
196+
197+
TIP: In contrast to javadoc:org.springframework.jms.listener.DefaultMessageListenerContainer[] that uses a pull-based mechanism (polling) to process messages, javadoc:org.springframework.jms.listener.SimpleMessageListenerContainer[] uses a push-based mechanism that's very close to the spirit of the standalone JMS specification.
198+
To learn more about the differences between the two listener containers, consult their respective javadocs and {url-spring-framework-docs}/integration/jms/using.html#jms-mdp[Spring Framework reference documentation].
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jms.autoconfigure;
18+
19+
import io.micrometer.observation.ObservationRegistry;
20+
import jakarta.jms.ConnectionFactory;
21+
import jakarta.jms.ExceptionListener;
22+
import org.jspecify.annotations.Nullable;
23+
24+
import org.springframework.boot.context.properties.PropertyMapper;
25+
import org.springframework.boot.jms.autoconfigure.JmsProperties.Listener.Session;
26+
import org.springframework.jms.config.AbstractJmsListenerContainerFactory;
27+
import org.springframework.jms.support.converter.MessageConverter;
28+
import org.springframework.jms.support.destination.DestinationResolver;
29+
import org.springframework.util.Assert;
30+
31+
/**
32+
* Configures {@link AbstractJmsListenerContainerFactory} with sensible defaults.
33+
*
34+
* @param <T> the connection factory type.
35+
* @author Vedran Pavic
36+
* @since 4.0.0
37+
*/
38+
public abstract class AbstractJmsListenerContainerFactoryConfigurer<T extends AbstractJmsListenerContainerFactory<?>> {
39+
40+
private @Nullable DestinationResolver destinationResolver;
41+
42+
private @Nullable MessageConverter messageConverter;
43+
44+
private @Nullable ExceptionListener exceptionListener;
45+
46+
private @Nullable ObservationRegistry observationRegistry;
47+
48+
private @Nullable JmsProperties jmsProperties;
49+
50+
/**
51+
* Set the {@link DestinationResolver} to use or {@code null} if no destination
52+
* resolver should be associated with the factory by default.
53+
* @param destinationResolver the {@link DestinationResolver}
54+
*/
55+
void setDestinationResolver(@Nullable DestinationResolver destinationResolver) {
56+
this.destinationResolver = destinationResolver;
57+
}
58+
59+
/**
60+
* Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box
61+
* converter should be used.
62+
* @param messageConverter the {@link MessageConverter}
63+
*/
64+
void setMessageConverter(@Nullable MessageConverter messageConverter) {
65+
this.messageConverter = messageConverter;
66+
}
67+
68+
/**
69+
* Set the {@link ExceptionListener} to use or {@code null} if no exception listener
70+
* should be associated by default.
71+
* @param exceptionListener the {@link ExceptionListener}
72+
*/
73+
void setExceptionListener(@Nullable ExceptionListener exceptionListener) {
74+
this.exceptionListener = exceptionListener;
75+
}
76+
77+
/**
78+
* Set the {@link JmsProperties} to use.
79+
* @param jmsProperties the {@link JmsProperties}
80+
*/
81+
void setJmsProperties(@Nullable JmsProperties jmsProperties) {
82+
this.jmsProperties = jmsProperties;
83+
}
84+
85+
/**
86+
* Set the {@link ObservationRegistry} to use.
87+
* @param observationRegistry the {@link ObservationRegistry}
88+
*/
89+
void setObservationRegistry(@Nullable ObservationRegistry observationRegistry) {
90+
this.observationRegistry = observationRegistry;
91+
}
92+
93+
/**
94+
* Configure the specified jms listener container factory. The factory can be further
95+
* tuned and default settings can be overridden.
96+
* @param factory the {@link AbstractJmsListenerContainerFactory} instance to
97+
* configure
98+
* @param connectionFactory the {@link ConnectionFactory} to use
99+
*/
100+
public void configure(T factory, ConnectionFactory connectionFactory) {
101+
Assert.notNull(factory, "'factory' must not be null");
102+
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
103+
Assert.state(this.jmsProperties != null, "'jmsProperties' must not be null");
104+
JmsProperties.Listener listenerProperties = this.jmsProperties.getListener();
105+
Session sessionProperties = listenerProperties.getSession();
106+
factory.setConnectionFactory(connectionFactory);
107+
PropertyMapper map = PropertyMapper.get();
108+
map.from(this.jmsProperties::isPubSubDomain).to(factory::setPubSubDomain);
109+
map.from(this.jmsProperties::isSubscriptionDurable).to(factory::setSubscriptionDurable);
110+
map.from(this.jmsProperties::getClientId).to(factory::setClientId);
111+
map.from(this.destinationResolver).to(factory::setDestinationResolver);
112+
map.from(this.messageConverter).to(factory::setMessageConverter);
113+
map.from(this.exceptionListener).to(factory::setExceptionListener);
114+
map.from(sessionProperties.getAcknowledgeMode()::getMode).to(factory::setSessionAcknowledgeMode);
115+
map.from(this.observationRegistry).to(factory::setObservationRegistry);
116+
map.from(sessionProperties::getTransacted).to(factory::setSessionTransacted);
117+
map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup);
118+
configure(factory, connectionFactory, this.jmsProperties);
119+
}
120+
121+
/**
122+
* Configures the given {@code factory} using the given {@code connectionFactory} and
123+
* {@code jmsProperties}.
124+
* @param factory the {@link AbstractJmsListenerContainerFactory} instance to
125+
* configure
126+
* @param connectionFactory the {@link ConnectionFactory} to use
127+
* @param jmsProperties the {@link JmsProperties} to use
128+
*/
129+
protected abstract void configure(T factory, ConnectionFactory connectionFactory, JmsProperties jmsProperties);
130+
131+
}

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

Lines changed: 9 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,17 @@
1818

1919
import java.time.Duration;
2020

21-
import io.micrometer.observation.ObservationRegistry;
2221
import jakarta.jms.ConnectionFactory;
23-
import jakarta.jms.ExceptionListener;
2422
import org.jspecify.annotations.Nullable;
2523

2624
import org.springframework.boot.context.properties.PropertyMapper;
2725
import org.springframework.boot.jms.autoconfigure.JmsProperties.Listener.Session;
2826
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
29-
import org.springframework.jms.support.converter.MessageConverter;
30-
import org.springframework.jms.support.destination.DestinationResolver;
3127
import org.springframework.transaction.jta.JtaTransactionManager;
32-
import org.springframework.util.Assert;
3328

3429
/**
35-
* Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults tuned using
36-
* configuration properties.
30+
* Configures {@link DefaultJmsListenerContainerFactory} with sensible defaults tuned
31+
* using configuration properties.
3732
* <p>
3833
* Can be injected into application code and used to define a custom
3934
* {@code DefaultJmsListenerContainerFactory} whose configuration is based upon that
@@ -45,47 +40,11 @@
4540
* @author Lasse Wulff
4641
* @since 4.0.0
4742
*/
48-
public final class DefaultJmsListenerContainerFactoryConfigurer {
49-
50-
private @Nullable DestinationResolver destinationResolver;
51-
52-
private @Nullable MessageConverter messageConverter;
53-
54-
private @Nullable ExceptionListener exceptionListener;
43+
public final class DefaultJmsListenerContainerFactoryConfigurer
44+
extends AbstractJmsListenerContainerFactoryConfigurer<DefaultJmsListenerContainerFactory> {
5545

5646
private @Nullable JtaTransactionManager transactionManager;
5747

58-
private @Nullable JmsProperties jmsProperties;
59-
60-
private @Nullable ObservationRegistry observationRegistry;
61-
62-
/**
63-
* Set the {@link DestinationResolver} to use or {@code null} if no destination
64-
* resolver should be associated with the factory by default.
65-
* @param destinationResolver the {@link DestinationResolver}
66-
*/
67-
void setDestinationResolver(@Nullable DestinationResolver destinationResolver) {
68-
this.destinationResolver = destinationResolver;
69-
}
70-
71-
/**
72-
* Set the {@link MessageConverter} to use or {@code null} if the out-of-the-box
73-
* converter should be used.
74-
* @param messageConverter the {@link MessageConverter}
75-
*/
76-
void setMessageConverter(@Nullable MessageConverter messageConverter) {
77-
this.messageConverter = messageConverter;
78-
}
79-
80-
/**
81-
* Set the {@link ExceptionListener} to use or {@code null} if no exception listener
82-
* should be associated by default.
83-
* @param exceptionListener the {@link ExceptionListener}
84-
*/
85-
void setExceptionListener(@Nullable ExceptionListener exceptionListener) {
86-
this.exceptionListener = exceptionListener;
87-
}
88-
8948
/**
9049
* Set the {@link JtaTransactionManager} to use or {@code null} if the JTA support
9150
* should not be used.
@@ -95,50 +54,16 @@ void setTransactionManager(@Nullable JtaTransactionManager transactionManager) {
9554
this.transactionManager = transactionManager;
9655
}
9756

98-
/**
99-
* Set the {@link JmsProperties} to use.
100-
* @param jmsProperties the {@link JmsProperties}
101-
*/
102-
void setJmsProperties(@Nullable JmsProperties jmsProperties) {
103-
this.jmsProperties = jmsProperties;
104-
}
105-
106-
/**
107-
* Set the {@link ObservationRegistry} to use.
108-
* @param observationRegistry the {@link ObservationRegistry}
109-
*/
110-
void setObservationRegistry(@Nullable ObservationRegistry observationRegistry) {
111-
this.observationRegistry = observationRegistry;
112-
}
113-
114-
/**
115-
* Configure the specified jms listener container factory. The factory can be further
116-
* tuned and default settings can be overridden.
117-
* @param factory the {@link DefaultJmsListenerContainerFactory} instance to configure
118-
* @param connectionFactory the {@link ConnectionFactory} to use
119-
*/
120-
public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFactory connectionFactory) {
121-
Assert.notNull(factory, "'factory' must not be null");
122-
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
123-
Assert.state(this.jmsProperties != null, "'jmsProperties' must not be null");
124-
JmsProperties.Listener listenerProperties = this.jmsProperties.getListener();
125-
Session sessionProperties = listenerProperties.getSession();
126-
factory.setConnectionFactory(connectionFactory);
57+
@Override
58+
protected void configure(DefaultJmsListenerContainerFactory factory, ConnectionFactory connectionFactory,
59+
JmsProperties jmsProperties) {
12760
PropertyMapper map = PropertyMapper.get();
128-
map.from(this.jmsProperties::isPubSubDomain).to(factory::setPubSubDomain);
129-
map.from(this.jmsProperties::isSubscriptionDurable).to(factory::setSubscriptionDurable);
130-
map.from(this.jmsProperties::getClientId).to(factory::setClientId);
61+
JmsProperties.Listener listenerProperties = jmsProperties.getListener();
62+
Session sessionProperties = listenerProperties.getSession();
13163
map.from(this.transactionManager).to(factory::setTransactionManager);
132-
map.from(this.destinationResolver).to(factory::setDestinationResolver);
133-
map.from(this.messageConverter).to(factory::setMessageConverter);
134-
map.from(this.exceptionListener).to(factory::setExceptionListener);
135-
map.from(sessionProperties.getAcknowledgeMode()::getMode).to(factory::setSessionAcknowledgeMode);
13664
if (this.transactionManager == null && sessionProperties.getTransacted() == null) {
13765
factory.setSessionTransacted(true);
13866
}
139-
map.from(this.observationRegistry).to(factory::setObservationRegistry);
140-
map.from(sessionProperties::getTransacted).to(factory::setSessionTransacted);
141-
map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup);
14267
map.from(listenerProperties::formatConcurrency).to(factory::setConcurrency);
14368
map.from(listenerProperties::getReceiveTimeout).as(Duration::toMillis).to(factory::setReceiveTimeout);
14469
map.from(listenerProperties::getMaxMessagesPerTask).to(factory::setMaxMessagesPerTask);

module/spring-boot-jms/src/main/java/org/springframework/boot/jms/autoconfigure/JmsAnnotationDrivenConfiguration.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Phillip Webb
4343
* @author Stephane Nicoll
4444
* @author Eddú Meléndez
45+
* @author Vedran Pavic
4546
*/
4647
@Configuration(proxyBeanMethods = false)
4748
@ConditionalOnClass(EnableJms.class)
@@ -73,8 +74,7 @@ class JmsAnnotationDrivenConfiguration {
7374

7475
@Bean
7576
@ConditionalOnMissingBean
76-
@SuppressWarnings("removal")
77-
DefaultJmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigurer() {
77+
DefaultJmsListenerContainerFactoryConfigurer defaultJmsListenerContainerFactoryConfigurer() {
7878
DefaultJmsListenerContainerFactoryConfigurer configurer = new DefaultJmsListenerContainerFactoryConfigurer();
7979
configurer.setDestinationResolver(this.destinationResolver.getIfUnique());
8080
configurer.setTransactionManager(this.transactionManager.getIfUnique());
@@ -85,6 +85,18 @@ DefaultJmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigur
8585
return configurer;
8686
}
8787

88+
@Bean
89+
@ConditionalOnMissingBean
90+
SimpleJmsListenerContainerFactoryConfigurer simpleJmsListenerContainerFactoryConfigurer() {
91+
SimpleJmsListenerContainerFactoryConfigurer configurer = new SimpleJmsListenerContainerFactoryConfigurer();
92+
configurer.setDestinationResolver(this.destinationResolver.getIfUnique());
93+
configurer.setMessageConverter(this.messageConverter.getIfUnique());
94+
configurer.setExceptionListener(this.exceptionListener.getIfUnique());
95+
configurer.setObservationRegistry(this.observationRegistry.getIfUnique());
96+
configurer.setJmsProperties(this.properties);
97+
return configurer;
98+
}
99+
88100
@Bean
89101
@ConditionalOnSingleCandidate(ConnectionFactory.class)
90102
@ConditionalOnMissingBean(name = "jmsListenerContainerFactory")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2012-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.jms.autoconfigure;
18+
19+
import jakarta.jms.ConnectionFactory;
20+
21+
import org.springframework.jms.config.SimpleJmsListenerContainerFactory;
22+
23+
/**
24+
* Configures {@link SimpleJmsListenerContainerFactory} with sensible defaults.
25+
*
26+
* @author Vedran Pavic
27+
* @since 4.0.0
28+
*/
29+
public final class SimpleJmsListenerContainerFactoryConfigurer
30+
extends AbstractJmsListenerContainerFactoryConfigurer<SimpleJmsListenerContainerFactory> {
31+
32+
@Override
33+
protected void configure(SimpleJmsListenerContainerFactory factory, ConnectionFactory connectionFactory,
34+
JmsProperties jmsProperties) {
35+
}
36+
37+
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void testNoConnectionFactoryJmsConfiguration() {
7575
.doesNotHaveBean(JmsMessagingTemplate.class)
7676
.doesNotHaveBean(JmsClient.class)
7777
.doesNotHaveBean(DefaultJmsListenerContainerFactoryConfigurer.class)
78+
.doesNotHaveBean(SimpleJmsListenerContainerFactoryConfigurer.class)
7879
.doesNotHaveBean(DefaultJmsListenerContainerFactory.class));
7980
}
8081

@@ -495,9 +496,10 @@ JmsMessagingTemplate jmsMessagingTemplate(JmsTemplate jmsTemplate) {
495496
static class TestConfiguration6 {
496497

497498
@Bean
498-
JmsListenerContainerFactory<?> jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
499+
JmsListenerContainerFactory<?> jmsListenerContainerFactory(
500+
SimpleJmsListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
499501
SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
500-
factory.setConnectionFactory(connectionFactory);
502+
configurer.configure(factory, connectionFactory);
501503
return factory;
502504
}
503505

0 commit comments

Comments
 (0)