Add properties for JmsTemplate session's ack mode and transacted flag

See gh-37500
pull/37603/head
Vedran Pavic 1 year ago committed by Andy Wilkinson
parent b63efd17b8
commit 5ba4e2793d

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

@ -27,6 +27,7 @@ import org.springframework.boot.context.properties.DeprecatedConfigurationProper
* @author Greg Turnquist * @author Greg Turnquist
* @author Phillip Webb * @author Phillip Webb
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Vedran Pavic
* @since 1.0.0 * @since 1.0.0
*/ */
@ConfigurationProperties(prefix = "spring.jms") @ConfigurationProperties(prefix = "spring.jms")
@ -227,6 +228,16 @@ public class JmsProperties {
public static class Template { 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 * Default destination to use on send and receive operations that do not have a
* destination parameter. * destination parameter.
@ -267,6 +278,22 @@ public class JmsProperties {
*/ */
private Duration receiveTimeout; 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() { public String getDefaultDestination() {
return this.defaultDestination; return this.defaultDestination;
} }

@ -253,7 +253,8 @@ class JmsAutoConfigurationTests {
@Test @Test
void testJmsTemplateFullCustomization() { void testJmsTemplateFullCustomization() {
this.contextRunner.withUserConfiguration(MessageConvertersConfiguration.class) 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.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.priority=6", "spring.jms.template.time-to-live=6000",
"spring.jms.template.receive-timeout=2000") "spring.jms.template.receive-timeout=2000")
@ -261,6 +262,8 @@ class JmsAutoConfigurationTests {
JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class); JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
assertThat(jmsTemplate.getMessageConverter()).isSameAs(context.getBean("myMessageConverter")); assertThat(jmsTemplate.getMessageConverter()).isSameAs(context.getBean("myMessageConverter"));
assertThat(jmsTemplate.isPubSubDomain()).isFalse(); assertThat(jmsTemplate.isPubSubDomain()).isFalse();
assertThat(jmsTemplate.getSessionAcknowledgeMode()).isEqualTo(Session.CLIENT_ACKNOWLEDGE);
assertThat(jmsTemplate.isSessionTransacted()).isTrue();
assertThat(jmsTemplate.getDefaultDestinationName()).isEqualTo("testQueue"); assertThat(jmsTemplate.getDefaultDestinationName()).isEqualTo("testQueue");
assertThat(jmsTemplate.getDeliveryDelay()).isEqualTo(500); assertThat(jmsTemplate.getDeliveryDelay()).isEqualTo(500);
assertThat(jmsTemplate.getDeliveryMode()).isOne(); assertThat(jmsTemplate.getDeliveryMode()).isOne();

Loading…
Cancel
Save