Skip to content

Commit 1f00c27

Browse files
eddumelendezsnicoll
authored andcommitted
Add support to auto-configure javax.jms.ExceptionListener
See gh-25278
1 parent 193fdd7 commit 1f00c27

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

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

Lines changed: 16 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 {
@@ -42,6 +44,8 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
4244

4345
private JmsProperties jmsProperties;
4446

47+
private ExceptionListener exceptionListener;
48+
4549
/**
4650
* Set the {@link DestinationResolver} to use or {@code null} if no destination
4751
* resolver should be associated with the factory by default.
@@ -77,6 +81,14 @@ void setJmsProperties(JmsProperties jmsProperties) {
7781
this.jmsProperties = jmsProperties;
7882
}
7983

84+
/**
85+
* Set the {@link ExceptionListener}.
86+
* @param exceptionListener the {@link ExceptionListener}
87+
*/
88+
void setExceptionListener(ExceptionListener exceptionListener) {
89+
this.exceptionListener = exceptionListener;
90+
}
91+
8092
/**
8193
* Configure the specified jms listener container factory. The factory can be further
8294
* tuned and default settings can be overridden.
@@ -113,6 +125,9 @@ public void configure(DefaultJmsListenerContainerFactory factory, ConnectionFact
113125
if (receiveTimeout != null) {
114126
factory.setReceiveTimeout(receiveTimeout.toMillis());
115127
}
128+
if (this.exceptionListener != null) {
129+
factory.setExceptionListener(this.exceptionListener);
130+
}
116131
}
117132

118133
}

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: 22 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

@@ -393,6 +395,15 @@ void enableJmsAutomatically() {
393395
.hasBean(JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME));
394396
}
395397

398+
@Test
399+
void testDefaultContainerFactoryWithExceptionListener() {
400+
this.contextRunner.withUserConfiguration(TestConfiguration11.class, EnableJmsConfiguration.class)
401+
.run((context) -> {
402+
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
403+
assertThat(container.getExceptionListener()).isSameAs(context.getBean("exceptionListener"));
404+
});
405+
}
406+
396407
@Configuration(proxyBeanMethods = false)
397408
static class TestConfiguration {
398409

@@ -548,6 +559,16 @@ ConnectionFactory connectionFactory2() {
548559

549560
}
550561

562+
@Configuration(proxyBeanMethods = false)
563+
static class TestConfiguration11 {
564+
565+
@Bean
566+
ExceptionListener exceptionListener() {
567+
return mock(ExceptionListener.class);
568+
}
569+
570+
}
571+
551572
@Configuration(proxyBeanMethods = false)
552573
@EnableJms
553574
static class EnableJmsConfiguration {

0 commit comments

Comments
 (0)