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
pull/25299/head
Stephane Nicoll 4 years ago
commit de6b7a31ec

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.jms;
import java.time.Duration;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.support.converter.MessageConverter;
@ -30,6 +31,7 @@ import org.springframework.util.Assert;
* Configure {@link DefaultJmsListenerContainerFactory} with sensible defaults.
*
* @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.3.3
*/
public final class DefaultJmsListenerContainerFactoryConfigurer {
@ -38,6 +40,8 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
private MessageConverter messageConverter;
private ExceptionListener exceptionListener;
private JtaTransactionManager transactionManager;
private JmsProperties jmsProperties;
@ -60,6 +64,15 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
this.messageConverter = messageConverter;
}
/**
* Set the {@link ExceptionListener} to use or {@code null} if no exception listener
* should be associated by default.
* @param exceptionListener the {@link ExceptionListener}
*/
void setExceptionListener(ExceptionListener exceptionListener) {
this.exceptionListener = exceptionListener;
}
/**
* Set the {@link JtaTransactionManager} to use or {@code null} if the JTA support
* should not be used.
@ -100,6 +113,9 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
if (this.messageConverter != null) {
factory.setMessageConverter(this.messageConverter);
}
if (this.exceptionListener != null) {
factory.setExceptionListener(this.exceptionListener);
}
JmsProperties.Listener listener = this.jmsProperties.getListener();
factory.setAutoStartup(listener.isAutoStartup());
if (listener.getAcknowledgeMode() != null) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -38,6 +39,7 @@ import org.springframework.transaction.jta.JtaTransactionManager;
*
* @author Phillip Webb
* @author Stephane Nicoll
* @author Eddú Meléndez
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(EnableJms.class)
@ -49,14 +51,17 @@ class JmsAnnotationDrivenConfiguration {
private final ObjectProvider<MessageConverter> messageConverter;
private final ObjectProvider<ExceptionListener> exceptionListener;
private final JmsProperties properties;
JmsAnnotationDrivenConfiguration(ObjectProvider<DestinationResolver> destinationResolver,
ObjectProvider<JtaTransactionManager> transactionManager, ObjectProvider<MessageConverter> messageConverter,
JmsProperties properties) {
ObjectProvider<ExceptionListener> exceptionListener, JmsProperties properties) {
this.destinationResolver = destinationResolver;
this.transactionManager = transactionManager;
this.messageConverter = messageConverter;
this.exceptionListener = exceptionListener;
this.properties = properties;
}
@ -67,6 +72,7 @@ class JmsAnnotationDrivenConfiguration {
configurer.setDestinationResolver(this.destinationResolver.getIfUnique());
configurer.setTransactionManager(this.transactionManager.getIfUnique());
configurer.setMessageConverter(this.messageConverter.getIfUnique());
configurer.setExceptionListener(this.exceptionListener.getIfUnique());
configurer.setJmsProperties(this.properties);
return configurer;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.jms;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
@ -56,6 +57,7 @@ import static org.mockito.Mockito.mock;
* @author Greg Turnquist
* @author Stephane Nicoll
* @author Aurélien Leboulanger
* @author Eddú Meléndez
*/
class JmsAutoConfigurationTests {
@ -210,6 +212,16 @@ class JmsAutoConfigurationTests {
});
}
@Test
void testDefaultContainerFactoryWithExceptionListener() {
ExceptionListener exceptionListener = mock(ExceptionListener.class);
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
.withBean(ExceptionListener.class, () -> exceptionListener).run((context) -> {
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
assertThat(container.getExceptionListener()).isSameAs(exceptionListener);
});
}
@Test
void testCustomContainerFactoryWithConfigurer() {
this.contextRunner.withUserConfiguration(TestConfiguration9.class, EnableJmsConfiguration.class)

@ -5416,7 +5416,7 @@ If a `DestinationResolver` or a `MessageConverter` bean is defined, it is associ
==== Receiving a Message
When the JMS infrastructure is present, any bean can be annotated with `@JmsListener` to create a listener endpoint.
If no `JmsListenerContainerFactory` has been defined, a default one is configured automatically.
If a `DestinationResolver` or a `MessageConverter` beans is defined, it is associated automatically to the default factory.
If a `DestinationResolver`, a `MessageConverter`, or a `javax.jms.ExceptionListener` beans are defined, they are associated automatically with the default factory.
By default, the default factory is transactional.
If you run in an infrastructure where a `JtaTransactionManager` is present, it is associated to the listener container by default.

Loading…
Cancel
Save