Add support for configuring JmsTemplate acknowledgeMode and sessionTransacted

Closes gh-37472
pull/37576/head
Vedran Pavic 1 year ago
parent 845c4dd057
commit 5cfc198e6f

@ -47,6 +47,7 @@ import org.springframework.jms.support.destination.DestinationResolver;
*
* @author Greg Turnquist
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.0.0
*/
@AutoConfiguration
@ -88,6 +89,10 @@ public class JmsAutoConfiguration {
private void mapTemplateProperties(Template properties, JmsTemplate template) {
PropertyMapper map = PropertyMapper.get();
map.from(properties::getAcknowledgeMode)
.whenNonNull()
.to((acknowledgeMode) -> template.setSessionAcknowledgeMode(acknowledgeMode.getMode()));
map.from(properties::getSessionTransacted).whenNonNull().to(template::setSessionTransacted);
map.from(properties::getDefaultDestination).whenNonNull().to(template::setDefaultDestinationName);
map.from(properties::getDeliveryDelay).whenNonNull().as(Duration::toMillis).to(template::setDeliveryDelay);
map.from(properties::determineQosEnabled).to(template::setExplicitQosEnabled);

@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.DeprecatedConfigurationProper
* @author Greg Turnquist
* @author Phillip Webb
* @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.0.0
*/
@ConfigurationProperties(prefix = "spring.jms")
@ -227,6 +228,16 @@ public class JmsProperties {
public static class Template {
/**
* Acknowledgement mode used when creating JMS sessions to send a message.
*/
private AcknowledgeMode acknowledgeMode;
/**
* Whether to use transacted JMS sessions.
*/
private Boolean sessionTransacted;
/**
* Default destination to use on send and receive operations that do not have a
* destination parameter.
@ -267,6 +278,22 @@ public class JmsProperties {
*/
private Duration receiveTimeout;
public AcknowledgeMode getAcknowledgeMode() {
return this.acknowledgeMode;
}
public void setAcknowledgeMode(AcknowledgeMode acknowledgeMode) {
this.acknowledgeMode = acknowledgeMode;
}
public Boolean getSessionTransacted() {
return this.sessionTransacted;
}
public void setSessionTransacted(Boolean sessionTransacted) {
this.sessionTransacted = sessionTransacted;
}
public String getDefaultDestination() {
return this.defaultDestination;
}

@ -253,7 +253,8 @@ class JmsAutoConfigurationTests {
@Test
void testJmsTemplateFullCustomization() {
this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class)
.withPropertyValues("spring.jms.template.default-destination=testQueue",
.withPropertyValues("spring.jms.template.acknowledge-mode=client",
"spring.jms.template.session-transacted=true", "spring.jms.template.default-destination=testQueue",
"spring.jms.template.delivery-delay=500", "spring.jms.template.delivery-mode=non-persistent",
"spring.jms.template.priority=6", "spring.jms.template.time-to-live=6000",
"spring.jms.template.receive-timeout=2000")
@ -261,6 +262,8 @@ class JmsAutoConfigurationTests {
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
assertThat(jmsTemplate.getMessageConverter()).isSameAs(context.getBean("myMessageConverter"));
assertThat(jmsTemplate.isPubSubDomain()).isFalse();
assertThat(jmsTemplate.getSessionAcknowledgeMode()).isEqualTo(Session.CLIENT_ACKNOWLEDGE);
assertThat(jmsTemplate.isSessionTransacted()).isTrue();
assertThat(jmsTemplate.getDefaultDestinationName()).isEqualTo("testQueue");
assertThat(jmsTemplate.getDeliveryDelay()).isEqualTo(500);
assertThat(jmsTemplate.getDeliveryMode()).isOne();

Loading…
Cancel
Save