From c202f53a85f2f24e53b205c59bcfa6c4f17cf463 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 | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 e80bc69f2..0cf2c2e83 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 @@ -42,11 +42,16 @@ 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.junit4.SpringRunner; +import org.springframework.util.SerializationUtils; import static org.junit.Assert.assertNotNull; import static org.springframework.cloud.sleuth.assertions.SleuthAssertions.then; @@ -314,6 +319,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 {