Polish "Add config prop for JMS listener's sessionTransacted flag"

See gh-37473
pull/37607/head
Andy Wilkinson 1 year ago
parent 79e2cb3ec1
commit 0d2eaa716c

@ -21,6 +21,7 @@ import java.time.Duration;
import jakarta.jms.ConnectionFactory; import jakarta.jms.ConnectionFactory;
import jakarta.jms.ExceptionListener; import jakarta.jms.ExceptionListener;
import org.springframework.boot.autoconfigure.jms.JmsProperties.Listener.Session;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.support.converter.MessageConverter; import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver; import org.springframework.jms.support.destination.DestinationResolver;
@ -103,14 +104,16 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
factory.setConnectionFactory(connectionFactory); factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(this.jmsProperties.isPubSubDomain()); factory.setPubSubDomain(this.jmsProperties.isPubSubDomain());
JmsProperties.Listener listener = this.jmsProperties.getListener(); JmsProperties.Listener listener = this.jmsProperties.getListener();
Session session = listener.getSession();
Boolean sessionTransacted = session.getTransacted();
if (this.transactionManager != null) { if (this.transactionManager != null) {
factory.setTransactionManager(this.transactionManager); factory.setTransactionManager(this.transactionManager);
} }
else if (listener.getSessionTransacted() == null) { else if (sessionTransacted == null) {
factory.setSessionTransacted(true); factory.setSessionTransacted(true);
} }
if (listener.getSessionTransacted() != null) { if (sessionTransacted != null) {
factory.setSessionTransacted(listener.getSessionTransacted()); factory.setSessionTransacted(sessionTransacted);
} }
if (this.destinationResolver != null) { if (this.destinationResolver != null) {
factory.setDestinationResolver(this.destinationResolver); factory.setDestinationResolver(this.destinationResolver);
@ -122,7 +125,7 @@ public final class DefaultJmsListenerContainerFactoryConfigurer {
factory.setExceptionListener(this.exceptionListener); factory.setExceptionListener(this.exceptionListener);
} }
factory.setAutoStartup(listener.isAutoStartup()); factory.setAutoStartup(listener.isAutoStartup());
factory.setSessionAcknowledgeMode(listener.getSession().getAcknowledgeMode().getMode()); factory.setSessionAcknowledgeMode(session.getAcknowledgeMode().getMode());
String concurrency = listener.formatConcurrency(); String concurrency = listener.formatConcurrency();
if (concurrency != null) { if (concurrency != null) {
factory.setConcurrency(concurrency); factory.setConcurrency(concurrency);

@ -141,11 +141,6 @@ public class JmsProperties {
*/ */
private boolean autoStartup = true; private boolean autoStartup = true;
/**
* Whether the container should use transacted JMS sessions.
*/
private Boolean sessionTransacted;
/** /**
* Minimum number of concurrent consumers. When max-concurrency is not specified * Minimum number of concurrent consumers. When max-concurrency is not specified
* the minimum will also be used as the maximum. * the minimum will also be used as the maximum.
@ -185,14 +180,6 @@ public class JmsProperties {
this.session.setAcknowledgeMode(acknowledgeMode); this.session.setAcknowledgeMode(acknowledgeMode);
} }
public Boolean getSessionTransacted() {
return this.sessionTransacted;
}
public void setSessionTransacted(Boolean sessionTransacted) {
this.sessionTransacted = sessionTransacted;
}
@DeprecatedConfigurationProperty(replacement = "spring.jms.listener.min-concurrency", since = "3.2.0") @DeprecatedConfigurationProperty(replacement = "spring.jms.listener.min-concurrency", since = "3.2.0")
@Deprecated(since = "3.2.0", forRemoval = true) @Deprecated(since = "3.2.0", forRemoval = true)
public Integer getConcurrency() { public Integer getConcurrency() {
@ -247,6 +234,12 @@ public class JmsProperties {
*/ */
private AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO; private AcknowledgeMode acknowledgeMode = AcknowledgeMode.AUTO;
/**
* Whether the listener container should use transacted JMS sessions. Defaults
* to false in the presence of a JtaTransactionManager and true otherwise.
*/
private Boolean transacted;
public AcknowledgeMode getAcknowledgeMode() { public AcknowledgeMode getAcknowledgeMode() {
return this.acknowledgeMode; return this.acknowledgeMode;
} }
@ -255,6 +248,14 @@ public class JmsProperties {
this.acknowledgeMode = acknowledgeMode; this.acknowledgeMode = acknowledgeMode;
} }
public Boolean getTransacted() {
return this.transacted;
}
public void setTransacted(Boolean transacted) {
this.transacted = transacted;
}
} }
} }

@ -144,9 +144,9 @@ class JmsAutoConfigurationTests {
void testJmsListenerContainerFactoryWithCustomSettings() { void testJmsListenerContainerFactoryWithCustomSettings() {
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class) this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
.withPropertyValues("spring.jms.listener.autoStartup=false", .withPropertyValues("spring.jms.listener.autoStartup=false",
"spring.jms.listener.session.acknowledgeMode=client", "spring.jms.listener.sessionTransacted=false", "spring.jms.listener.session.acknowledgeMode=client",
"spring.jms.listener.minConcurrency=2", "spring.jms.listener.receiveTimeout=2s", "spring.jms.listener.session.transacted=false", "spring.jms.listener.minConcurrency=2",
"spring.jms.listener.maxConcurrency=10") "spring.jms.listener.receiveTimeout=2s", "spring.jms.listener.maxConcurrency=10")
.run(this::testJmsListenerContainerFactoryWithCustomSettings); .run(this::testJmsListenerContainerFactoryWithCustomSettings);
} }
@ -185,7 +185,7 @@ class JmsAutoConfigurationTests {
@Test @Test
void testDefaultContainerFactoryWithJtaTransactionManagerAndSessionTransactedEnabled() { void testDefaultContainerFactoryWithJtaTransactionManagerAndSessionTransactedEnabled() {
this.contextRunner.withUserConfiguration(TestConfiguration7.class, EnableJmsConfiguration.class) this.contextRunner.withUserConfiguration(TestConfiguration7.class, EnableJmsConfiguration.class)
.withPropertyValues("spring.jms.listener.sessionTransacted=true") .withPropertyValues("spring.jms.listener.session.transacted=true")
.run((context) -> { .run((context) -> {
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory"); DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
assertThat(container.isSessionTransacted()).isTrue(); assertThat(container.isSessionTransacted()).isTrue();
@ -216,7 +216,7 @@ class JmsAutoConfigurationTests {
@Test @Test
void testDefaultContainerFactoryNoTransactionManagerAndSessionTransactedDisabled() { void testDefaultContainerFactoryNoTransactionManagerAndSessionTransactedDisabled() {
this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class) this.contextRunner.withUserConfiguration(EnableJmsConfiguration.class)
.withPropertyValues("spring.jms.listener.sessionTransacted=false") .withPropertyValues("spring.jms.listener.session.transacted=false")
.run((context) -> { .run((context) -> {
DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory"); DefaultMessageListenerContainer container = getContainer(context, "jmsListenerContainerFactory");
assertThat(container.isSessionTransacted()).isFalse(); assertThat(container.isSessionTransacted()).isFalse();

Loading…
Cancel
Save