Skip to content

Commit

Permalink
Configure MessageConverter (if available) on TracingJmsTemplate
Browse files Browse the repository at this point in the history
Signed-off-by: Gary Brown <gary@brownuk.com>
  • Loading branch information
objectiser committed Jul 3, 2018
1 parent 7685462 commit baeca1a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
*.iml
activemq-data/
target/

# Eclipse
.project
.classpath
.settings

# VS Code
.vscode
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,22 @@
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.target.AbstractLazyCreationTargetSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.JmsListenerConfigurer;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.converter.MessageConverter;

@Configuration
public class TracingJmsConfiguration {

private final ObjectProvider<MessageConverter> messageConverter;

public TracingJmsConfiguration(ObjectProvider<MessageConverter> messageConverter) {
this.messageConverter = messageConverter;
}

@Bean
public TracingJmsListenerEndpointRegistry createTracingJmsListenerEndpointRegistry(
Tracer tracer) {
Expand All @@ -44,7 +52,12 @@ public JmsTemplate jmsTemplate(BeanFactory beanFactory, Tracer tracer) {
// if JMS is used, and ConnectionFactory bean is not present,
// it will throw an error on first use, so imo, we should be all good
ConnectionFactory connectionFactory = createProxy(beanFactory);
return new TracingJmsTemplate(connectionFactory, tracer);
JmsTemplate ret = new TracingJmsTemplate(connectionFactory, tracer);
MessageConverter mc = messageConverter.getIfAvailable();
if (mc != null) {
ret.setMessageConverter(mc);
}
return ret;
}

private ConnectionFactory createProxy(final BeanFactory beanFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.destination.DestinationResolver;
import org.springframework.jms.support.destination.DynamicDestinationResolver;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageType;

@Configuration
@EnableJms
Expand Down Expand Up @@ -56,5 +59,11 @@ public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
return factory;
}


@Bean
public MessageConverter messageConverter() {
MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
converter.setTargetType(MessageType.TEXT);
converter.setTypeIdPropertyName("_type");
return converter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.awaitility.Awaitility.await;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -39,6 +40,7 @@
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {TracingJmsConfiguration.class, TestConfiguration.class})
Expand All @@ -55,6 +57,11 @@ public void before() throws IOException, JMSException {
mockTracer.reset();
}

@Test
public void messageConverterConfigured() {
assertTrue(jmsTemplate.getMessageConverter() instanceof MappingJackson2MessageConverter);
}

@Test
public void oneListener() throws Exception {
jmsTemplate.convertAndSend("TEST.SECOND", "test");
Expand Down

0 comments on commit baeca1a

Please sign in to comment.