Skip to content

Commit de6b7a3

Browse files
committed
Merge pull request #25278 from eddumelendez
* pr/25278: Polish "Add support to auto-configure javax.jms.ExceptionListener" Add support to auto-configure javax.jms.ExceptionListener Closes gh-25278
2 parents 193fdd7 + 8c0c30e commit de6b7a3

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -19,6 +19,7 @@
1919
import java.time.Duration;
2020

2121
import javax.jms.ConnectionFactory;
22+
import javax.jms.ExceptionListener;
2223

2324
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
2425
import org.springframework.jms.support.converter.MessageConverter;
@@ -30,6 +31,7 @@
3031
* Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults.
3132
*
3233
* @author Stephane Nicoll
34+
* @author Eddú Meléndez
3335
* @since 1.3.3
3436
*/
3537
public final class DefaultJmsListenerContainerFactoryConfigurer {
@@ -38,6 +40,8 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
3840

3941
private MessageConverter messageConverter;
4042

43+
private ExceptionListener exceptionListener;
44+
4145
private JtaTransactionManager transactionManager;
4246

4347
private JmsProperties jmsProperties;
@@ -60,6 +64,15 @@ void setMessageConverter(MessageConverter messageConverter) {
6064
this.messageConverter = messageConverter;
6165
}
6266

67+
/**
68+
* Set the {@link ExceptionListener} to use or {@code null} if no exception listener
69+
* should be associated by default.
70+
* @param exceptionListener the {@link ExceptionListener}
71+
*/
72+
void setExceptionListener(ExceptionListener exceptionListener) {
73+
this.exceptionListener = exceptionListener;
74+
}
75+
6376
/**
6477
* Set the {@link JtaTransactionManager} to use or {@code null} if the JTA support
6578
* should not be used.
@@ -100,6 +113,9 @@ public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFact
100113
if (this.messageConverter != null) {
101114
factory.setMessageConverter(this.messageConverter);
102115
}
116+
if (this.exceptionListener != null) {
117+
factory.setExceptionListener(this.exceptionListener);
118+
}
103119
JmsProperties.Listener listener = this.jmsProperties.getListener();
104120
factory.setAutoStartup(listener.isAutoStartup());
105121
if (listener.getAcknowledgeMode() != null) {

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.jms;
1818

1919
import javax.jms.ConnectionFactory;
20+
import javax.jms.ExceptionListener;
2021

2122
import org.springframework.beans.factory.ObjectProvider;
2223
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -38,6 +39,7 @@
3839
*
3940
* @author Phillip Webb
4041
* @author Stephane Nicoll
42+
* @author Eddú Meléndez
4143
*/
4244
@Configuration(proxyBeanMethods = false)
4345
@ConditionalOnClass(EnableJms.class)
@@ -49,14 +51,17 @@ class JmsAnnotationDrivenConfiguration {
4951

5052
private final ObjectProvider<MessageConverter> messageConverter;
5153

54+
private final ObjectProvider<ExceptionListener> exceptionListener;
55+
5256
private final JmsProperties properties;
5357

5458
JmsAnnotationDrivenConfiguration(ObjectProvider<DestinationResolver> destinationResolver,
5559
ObjectProvider<JtaTransactionManager> transactionManager, ObjectProvider<MessageConverter> messageConverter,
56-
JmsProperties properties) {
60+
ObjectProvider<ExceptionListener> exceptionListener, JmsProperties properties) {
5761
this.destinationResolver = destinationResolver;
5862
this.transactionManager = transactionManager;
5963
this.messageConverter = messageConverter;
64+
this.exceptionListener = exceptionListener;
6065
this.properties = properties;
6166
}
6267

@@ -67,6 +72,7 @@ DefaultJmsListenerContainerFactoryConfigurer jmsListenerContainerFactoryConfigur
6772
configurer.setDestinationResolver(this.destinationResolver.getIfUnique());
6873
configurer.setTransactionManager(this.transactionManager.getIfUnique());
6974
configurer.setMessageConverter(this.messageConverter.getIfUnique());
75+
configurer.setExceptionListener(this.exceptionListener.getIfUnique());
7076
configurer.setJmsProperties(this.properties);
7177
return configurer;
7278
}

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
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.jms;
1818

1919
import javax.jms.ConnectionFactory;
20+
import javax.jms.ExceptionListener;
2021
import javax.jms.Session;
2122

2223
import org.apache.activemq.ActiveMQConnectionFactory;
@@ -56,6 +57,7 @@
5657
* @author Greg Turnquist
5758
* @author Stephane Nicoll
5859
* @author Aurélien Leboulanger
60+
* @author Eddú Meléndez
5961
*/
6062
class JmsAutoConfigurationTests {
6163

@@ -210,6 +212,16 @@ void testDefaultContainerFactoryWithMessageConverters() {
210212
});
211213
}
212214

215+
@Test
216+
void testDefaultContainerFactoryWithExceptionListener() {
217+
ExceptionListener exceptionListener = mock(ExceptionListener.class);
218+
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
219+
.withBean(ExceptionListener.class, () -> exceptionListener).run((context) -> {
220+
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
221+
assertThat(container.getExceptionListener()).isSameAs(exceptionListener);
222+
});
223+
}
224+
213225
@Test
214226
void testCustomContainerFactoryWithConfigurer() {
215227
this.contextRunner.withUserConfiguration(TestConfiguration9.class, EnableJmsConfiguration.class)

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5416,7 +5416,7 @@ If a `DestinationResolver` or a `MessageConverter` bean is defined, it is associ
54165416
==== Receiving a Message
54175417
When the JMS infrastructure is present, any bean can be annotated with `@JmsListener` to create a listener endpoint.
54185418
If no `JmsListenerContainerFactory` has been defined, a default one is configured automatically.
5419-
If a `DestinationResolver` or a `MessageConverter` beans is defined, it is associated automatically to the default factory.
5419+
If a `DestinationResolver`, a `MessageConverter`, or a `javax.jms.ExceptionListener` beans are defined, they are associated automatically with the default factory.
54205420

54215421
By default, the default factory is transactional.
54225422
If you run in an infrastructure where a `JtaTransactionManager` is present, it is associated to the listener container by default.

0 commit comments

Comments
 (0)