I have an application that defines a JmsTemplate bean in the following way:
@Bean
JmsTemplate jmsTemplate(ConnectionFactory connectionFactory, JmsProperties jmsProperties) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
jmsTemplate.setDefaultDestinationName(jmsProperties.getTemplate().getDefaultDestination());
jmsTemplate.setReceiveTimeout(jmsProperties.getTemplate().getReceiveTimeout().toMillis());
jmsTemplate.setMessageConverter(new CustomMessageConverter());
jmsTemplate.setSessionTransacted(true);
return jmsTemplate;
}
I would like to remove this bean definition and rely on the JmsTemplateConfiguration only:
https://github.com/spring-projects/spring-boot/blob/83666d48ccc33c26ba03028a3d299ac4bea76df4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsAutoConfiguration.java#L60-L106
but I wasn't able to configure the sessionTransacted property via the JmsProperties.
I could introduce a BeanPostProcessor to amend the property but it's not really my preference right now 🙂
The Javadoc of AcknowledgeMode mentions:
https://github.com/spring-projects/spring-boot/blob/83666d48ccc33c26ba03028a3d299ac4bea76df4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/JmsProperties.java#L322-L329
but I couldn't figure out where the call to setSessionTransacted actually happens, or in general how it's supposed to work.
If this property is indeed missing from JmsProperties, I would be happy to provide a PR to add it.
Otherwise, I would love to get some guidance 🙏
Comment From: vpavic
I just started working on a PR to propose the addition of configuration property that would allow configuring sessionTransacted directly, because (like you @scordio) I found this to be a limitation in current auto-configuration support for JMS listeners.
FWIW, the logic that sets sessionTransacted (based on the presence of JtaTransactionManager) is here:
https://github.com/spring-projects/spring-boot/blob/83666d48ccc33c26ba03028a3d299ac4bea76df4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jms/DefaultJmsListenerContainerFactoryConfigurer.java#L104-L109
So, following example from Receiving a Message section of reference manual portion dedicated to JMS you could do:
@Bean
DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory,
DefaultJmsListenerContainerFactoryConfigurer configurer) {
DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
configurer.configure(jmsListenerContainerFactory, connectionFactory);
jmsListenerContainerFactory.setSessionTransacted(false);
return jmsListenerContainerFactory;
}
This allows you to set sessionTransacted directly as you prefer while still having other spring.jms.listener.* properties set by the auto-configuration. However, having a configuration property would still be preferred.
Comment From: vpavic
Here's the PR: - https://github.com/spring-projects/spring-boot/pull/37473
Comment From: scottfrederick
Closing in favor of #37473
Comment From: scordio
Thanks for the pointers, @vpavic!
I understand from the DefaultJmsListenerContainerFactoryConfigurer and the docs that if I'm not running in an infrastructure where a JtaTransactionManager is present, the sessionTransacted flag is always set to true. However, this is valid only for the DefaultJmsListenerContainerFactory and not for the JmsTemplate, right?
To add some background to my use case, I'm particularly interested in the JmsTemplate because that's the dependency used by a Spring Batch JmsItemWriter in my application.
Do you plan to enhance the JmsAutoConfiguration as well in your PR?
@scottfrederick as far as I understood, here I'm targeting a slightly different use case than what #37473 tries to solve. However, as I mentioned above, maybe the PR could be enhanced to cover both use cases.
Comment From: vpavic
Sorry for the confusion @scordio, got tangled up a bit with all the JMS improvements I've been preparing these days.
Yes, your focus in this issue is on the producer side while my PR focuses on the consumer side of things so therefore it does not address your concern. I had in mind to also propose a PR to expose properties for sessionAcknowledgeMode and sessionTransacted on the JmsTemplate - I'll likely get to this later in the day (I believe it would be cleaner to tackle it in a separate PR vs consumer concerns).
Comment From: scordio
Sure, I leave it to you, @vpavic. In case you don't have time to tackle it, just let me know and I'll be happy to jump in.
Comment From: scordio
@scottfrederick I updated the title to highlight the difference with #37473. Would you mind reopening this issue?
Comment From: vpavic
There's now https://github.com/spring-projects/spring-boot/pull/37500 that targets this.
Comment From: scottfrederick
Closing in favor of ##37500.