Skip to content

Auto-configure MessageConverter for JMS classes #4284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerConfigUtils;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.JndiDestinationResolver;
import org.springframework.transaction.jta.JtaTransactionManager;
Expand All @@ -45,6 +46,9 @@ class JmsAnnotationDrivenConfiguration {
@Autowired(required = false)
private DestinationResolver destinationResolver;

@Autowired(required = false)
private MessageConverter messageConverter;

@Autowired(required = false)
private JtaTransactionManager transactionManager;

Expand All @@ -67,6 +71,9 @@ public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(
if (this.destinationResolver != null) {
factory.setDestinationResolver(this.destinationResolver);
}
if (this.messageConverter != null) {
factory.setMessageConverter(this.messageConverter);
}
JmsProperties.Listener listener = this.properties.getListener();
factory.setAutoStartup(listener.isAutoStartup());
if (listener.getAcknowledgeMode() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;

/**
Expand All @@ -53,6 +54,9 @@ public class JmsAutoConfiguration {
@Autowired(required = false)
private DestinationResolver destinationResolver;

@Autowired(required = false)
private MessageConverter messageConverter;

@Bean
@ConditionalOnMissingBean
public JmsTemplate jmsTemplate() {
Expand All @@ -61,6 +65,11 @@ public JmsTemplate jmsTemplate() {
if (this.destinationResolver != null) {
jmsTemplate.setDestinationResolver(this.destinationResolver);
}

if (this.messageConverter != null) {
jmsTemplate.setMessageConverter(this.messageConverter);
}

return jmsTemplate;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.springframework.boot.autoconfigure.jms;

import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
Expand All @@ -42,6 +44,8 @@
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.support.converter.MessageConversionException;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.transaction.jta.JtaTransactionManager;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -330,6 +334,16 @@ public void enableJmsAutomatically() throws Exception {
ctx.getBean(JmsListenerConfigUtils.JMS_LISTENER_ENDPOINT_REGISTRY_BEAN_NAME);
}

@Test
public void testMessageConverterConfigured() {
load(TestConfigurationWithMessageConverter.class);
MessageConverter messageConverter = this.context.getBean(MessageConverter.class);
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
assertNotNull(messageConverter);
assertNotNull(jmsTemplate);
assertSame(messageConverter, jmsTemplate.getMessageConverter());
}

private AnnotationConfigApplicationContext createContext(
Class<?>... additionalClasses) {
return doLoad(additionalClasses);
Expand Down Expand Up @@ -456,4 +470,23 @@ protected static class EnableJmsConfiguration {
protected static class NoEnableJmsConfiguration {
}

@Configuration
protected static class TestConfigurationWithMessageConverter {

@Bean
public MessageConverter messageConverter() {
return new MessageConverter() {
@Override
public Message toMessage(Object o, Session session) throws JMSException, MessageConversionException {
return null;
}

@Override
public Object fromMessage(Message message) throws JMSException, MessageConversionException {
return null;
}
};
}
}

}