From 7caf522ecf705c4e63f138ceea859bd2d62b084c Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 23 Feb 2017 15:56:29 +0100 Subject: [PATCH] Showing a workaround to make message serializable this issue is there in core of Spring Framework (https://jira.spring.io/browse/SPR-15262) but this tests shows how to apply a workaround to get ridd of that problem until we bump up version of SF. related to https://github.com/spring-cloud/spring-cloud-sleuth/issues/520 and https://github.com/spring-cloud/spring-cloud-sleuth/issues/523 SO - http://stackoverflow.com/questions/42277170/messagedeliveryexception-thrown-when-using-amqp-backed-channel-with-brixton-sr7 fixes #520 --- .../TraceChannelInterceptorTests.java | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java index 35cfdc2b6..9fc8f1c8e 100644 --- a/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java +++ b/spring-cloud-sleuth-core/src/test/java/org/springframework/cloud/sleuth/instrument/messaging/TraceChannelInterceptorTests.java @@ -27,7 +27,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.cloud.sleuth.Sampler; import org.springframework.cloud.sleuth.Span; import org.springframework.cloud.sleuth.Tracer; @@ -44,24 +43,28 @@ import org.springframework.integration.channel.DirectChannel; import org.springframework.integration.core.MessagingTemplate; import org.springframework.integration.support.MessageBuilder; import org.springframework.messaging.Message; +import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; +import org.springframework.messaging.support.ChannelInterceptor; +import org.springframework.messaging.support.ChannelInterceptorAdapter; +import org.springframework.messaging.support.GenericMessage; import org.springframework.messaging.support.MessageHeaderAccessor; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.TestPropertySource; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.SerializationUtils; -import static org.assertj.core.api.BDDAssertions.then; import static org.junit.Assert.assertNotNull; import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; /** * @author Dave Syer */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.NONE) +@RunWith(SpringRunner.class) +@SpringBootTest(classes = App.class, + properties = "spring.sleuth.integration.patterns=traced*", + webEnvironment = SpringBootTest.WebEnvironment.NONE) @DirtiesContext -@TestPropertySource(properties = "spring.sleuth.integration.patterns=traced*") public class TraceChannelInterceptorTests implements MessageHandler { @Autowired @@ -274,6 +277,28 @@ public class TraceChannelInterceptorTests implements MessageHandler { then(TestSpanContextHolder.getCurrentSpan()).isNull(); } + @Test + public void serializeMutableHeaders() throws Exception { + Map headers = new HashMap<>(); + headers.put("foo", "bar"); + Message message = new GenericMessage<>("test", headers); + ChannelInterceptor immutableMessageInterceptor = new ChannelInterceptorAdapter() { + @Override + public Message preSend(Message message, MessageChannel channel) { + MessageHeaderAccessor headers = MessageHeaderAccessor.getMutableAccessor(message); + return new GenericMessage(message.getPayload(), headers.toMessageHeaders()); + } + }; + this.tracedChannel.addInterceptor(immutableMessageInterceptor); + + this.tracedChannel.send(message); + + Message output = (Message) SerializationUtils.deserialize(SerializationUtils.serialize(this.message)); + then(output.getPayload()).isEqualTo("test"); + then(output.getHeaders().get("foo")).isEqualTo("bar"); + this.tracedChannel.removeInterceptor(immutableMessageInterceptor); + } + @Configuration @EnableAutoConfiguration static class App {