Skip to content

Commit 784fd6c

Browse files
committed
Merge pull request #47716 from vpavic
* pr/47716: Polish "Add support for configuring SimpleMessageListenerContainer" Add support for configuring SimpleMessageListenerContainer Closes gh-47716
2 parents bc05406 + 30a56c2 commit 784fd6c

File tree

6 files changed

+247
-97
lines changed

6 files changed

+247
-97
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,141 @@
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.config.JmsListenerContainerFactory;
28+
import org.springframework.jms.support.converter.MessageConverter;
29+
import org.springframework.jms.support.destination.DestinationResolver;
30+
import org.springframework.util.Assert;
31+
32+
/**
33+
* Configure common {@link JmsListenerContainerFactory} settings with sensible defaults.
34+
* <p>
35+
* This includes:
36+
* <li>A {@link DestinationResolver} is such a component is present.</li>
37+
* <li>A {@link MessageConverter} is such a component is present.</li>
38+
* <li>An {@link ExceptionListener} is such a component is present.</li>
39+
* <li>An {@link ObservationRegistry} is such a component is present.</li>
40+
* <li>Configuration properties of the {@code spring.jms} namespace that are common to all
41+
* implementations.</li>
42+
*
43+
* @param <T> the connection factory type.
44+
* @author Stephane Nicoll
45+
* @author Eddú Meléndez
46+
* @author Vedran Pavic
47+
* @author Lasse Wulff
48+
* @since 4.1.0
49+
*/
50+
public abstract class AbstractJmsListenerContainerFactoryConfigurer<T extends AbstractJmsListenerContainerFactory<?>> {
51+
52+
private @Nullable DestinationResolver destinationResolver;
53+
54+
private @Nullable MessageConverter messageConverter;
55+
56+
private @Nullable ExceptionListener exceptionListener;
57+
58+
private @Nullable ObservationRegistry observationRegistry;
59+
60+
private @Nullable JmsProperties jmsProperties;
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+
89+
/**
90+
* Set the {@link JmsProperties} to use.
91+
* @param jmsProperties the {@link JmsProperties}
92+
*/
93+
void setJmsProperties(@Nullable JmsProperties jmsProperties) {
94+
this.jmsProperties = jmsProperties;
95+
}
96+
97+
/**
98+
* Set the {@link ObservationRegistry} to use.
99+
* @param observationRegistry the {@link ObservationRegistry}
100+
*/
101+
void setObservationRegistry(@Nullable ObservationRegistry observationRegistry) {
102+
this.observationRegistry = observationRegistry;
103+
}
104+
105+
/**
106+
* Return the {@link JmsProperties}.
107+
* @return the jms properties
108+
*/
109+
protected JmsProperties getJmsProperties() {
110+
Assert.state(this.jmsProperties != null, "'jmsProperties' must not be null");
111+
return this.jmsProperties;
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 AbstractJmsListenerContainerFactory} instance to
118+
* configure
119+
* @param connectionFactory the {@link ConnectionFactory} to use
120+
*/
121+
public void configure(T factory, ConnectionFactory connectionFactory) {
122+
Assert.notNull(factory, "'factory' must not be null");
123+
Assert.notNull(connectionFactory, "'connectionFactory' must not be null");
124+
JmsProperties properties = getJmsProperties();
125+
JmsProperties.Listener listenerProperties = properties.getListener();
126+
Session sessionProperties = listenerProperties.getSession();
127+
factory.setConnectionFactory(connectionFactory);
128+
PropertyMapper map = PropertyMapper.get();
129+
map.from(properties::isPubSubDomain).to(factory::setPubSubDomain);
130+
map.from(properties::isSubscriptionDurable).to(factory::setSubscriptionDurable);
131+
map.from(properties::getClientId).to(factory::setClientId);
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);
136+
map.from(this.observationRegistry).to(factory::setObservationRegistry);
137+
map.from(sessionProperties::getTransacted).to(factory::setSessionTransacted);
138+
map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup);
139+
}
140+
141+
}

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

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,13 @@
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
/**
3530
* Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults tuned using
@@ -44,48 +39,13 @@
4439
* @author Vedran Pavic
4540
* @author Lasse Wulff
4641
* @since 4.0.0
42+
* @see SimpleJmsListenerContainerFactoryConfigurer
4743
*/
48-
public final class DefaultJmsListenerContainerFactoryConfigurer {
49-
50-
private @Nullable DestinationResolver destinationResolver;
51-
52-
private @Nullable MessageConverter messageConverter;
53-
54-
private @Nullable ExceptionListener exceptionListener;
44+
public final class DefaultJmsListenerContainerFactoryConfigurer
45+
extends AbstractJmsListenerContainerFactoryConfigurer<DefaultJmsListenerContainerFactory> {
5546

5647
private @Nullable JtaTransactionManager transactionManager;
5748

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-
8949
/**
9050
* Set the {@link JtaTransactionManager} to use or {@code null} if the JTA support
9151
* should not be used.
@@ -95,50 +55,16 @@ void setTransactionManager(@Nullable JtaTransactionManager transactionManager) {
9555
this.transactionManager = transactionManager;
9656
}
9757

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-
*/
58+
@Override
12059
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);
60+
super.configure(factory, connectionFactory);
12761
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);
62+
JmsProperties.Listener listenerProperties = getJmsProperties().getListener();
63+
Session sessionProperties = listenerProperties.getSession();
13164
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);
13665
if (this.transactionManager == null && sessionProperties.getTransacted() == null) {
13766
factory.setSessionTransacted(true);
13867
}
139-
map.from(this.observationRegistry).to(factory::setObservationRegistry);
140-
map.from(sessionProperties::getTransacted).to(factory::setSessionTransacted);
141-
map.from(listenerProperties::isAutoStartup).to(factory::setAutoStartup);
14268
map.from(listenerProperties::formatConcurrency).to(factory::setConcurrency);
14369
map.from(listenerProperties::getReceiveTimeout).as(Duration::toMillis).to(factory::setReceiveTimeout);
14470
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,45 @@
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 org.springframework.jms.config.SimpleJmsListenerContainerFactory;
20+
import org.springframework.jms.listener.DefaultMessageListenerContainer;
21+
import org.springframework.jms.listener.SimpleMessageListenerContainer;
22+
23+
/**
24+
* Configure {@link SimpleJmsListenerContainerFactory} with sensible defaults. In contrast
25+
* to {@link DefaultMessageListenerContainer} that uses a pull-based mechanism (polling)
26+
* to process messages, the {@link SimpleMessageListenerContainer} instances created by
27+
* this factory use a push-based mechanism that's very close to the spirit of the
28+
* standalone JMS specification.
29+
* <p>
30+
* As such, concurrency-related configuration properties from the {@code spring.jms}
31+
* namespace are not taken into account by this implementation.
32+
* <p>
33+
* Can be injected into application code and used to define a custom
34+
* {@code SimpleJmsListenerContainerFactory} whose configuration is based upon that
35+
* produced by auto-configuration.
36+
*
37+
* @author Vedran Pavic
38+
* @author Stephane Nicoll
39+
* @since 4.0.0
40+
* @see DefaultJmsListenerContainerFactoryConfigurer
41+
*/
42+
public final class SimpleJmsListenerContainerFactoryConfigurer
43+
extends AbstractJmsListenerContainerFactoryConfigurer<SimpleJmsListenerContainerFactory> {
44+
45+
}

0 commit comments

Comments
 (0)