GH-1010: @StreamListener: Fix Header Propagation

Fixes #1010

Propagate headers by default.

Add a `copyHeaders` property to `@StreamListener` to allow suppression of header propagation.

Honour header propagation settings
This commit is contained in:
Gary Russell
2017-07-13 15:23:07 -04:00
committed by Marius Bogoevici
parent 2910d27e09
commit 205d4579a4
4 changed files with 135 additions and 18 deletions

View File

@@ -63,6 +63,7 @@ import static org.springframework.cloud.stream.binding.StreamListenerErrorMessag
/**
* @author Marius Bogoevici
* @author Ilayaperumal Gopinathan
* @author Gary Russell
*/
public class StreamListenerHandlerMethodTests {
@@ -82,8 +83,6 @@ public class StreamListenerHandlerMethodTests {
ConfigurableApplicationContext context = SpringApplication.run(TestMethodWithObjectAsMethodArgument.class,
"--server.port=0");
Processor processor = context.getBean(Processor.class);
String id = UUID.randomUUID().toString();
final CountDownLatch latch = new CountDownLatch(1);
final String testMessage = "testing";
processor.input().send(MessageBuilder.withPayload(testMessage).build());
MessageCollector messageCollector = context.getBean(MessageCollector.class);
@@ -93,6 +92,40 @@ public class StreamListenerHandlerMethodTests {
context.close();
}
@Test
public void testMethodHeadersPropagatged() throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersPropagated.class,
"--server.port=0");
Processor processor = context.getBean(Processor.class);
final String testMessage = "testing";
processor.input().send(MessageBuilder.withPayload(testMessage)
.setHeader("foo", "bar")
.build());
MessageCollector messageCollector = context.getBean(MessageCollector.class);
Message<?> result = messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
assertThat(result).isNotNull();
assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
assertThat(result.getHeaders().get("foo")).isEqualTo("bar");
context.close();
}
@Test
public void testMethodHeadersNotPropagatged() throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(TestMethodHeadersNotPropagated.class,
"--server.port=0");
Processor processor = context.getBean(Processor.class);
final String testMessage = "testing";
processor.input().send(MessageBuilder.withPayload(testMessage)
.setHeader("foo", "bar")
.build());
MessageCollector messageCollector = context.getBean(MessageCollector.class);
Message<?> result = messageCollector.forChannel(processor.output()).poll(1000, TimeUnit.MILLISECONDS);
assertThat(result).isNotNull();
assertThat(result.getPayload()).isEqualTo(testMessage.toUpperCase());
assertThat(result.getHeaders().get("foo")).isNull();
context.close();
}
@Test
public void testStreamListenerMethodWithTargetBeanFromOutside() throws Exception {
ConfigurableApplicationContext context = SpringApplication
@@ -333,6 +366,30 @@ public class StreamListenerHandlerMethodTests {
}
}
@EnableBinding({ Processor.class })
@EnableAutoConfiguration
public static class TestMethodHeadersPropagated {
@StreamListener(Processor.INPUT)
@SendTo(Processor.OUTPUT)
public String receive(String received) {
return received.toUpperCase();
}
}
@EnableBinding({ Processor.class })
@EnableAutoConfiguration
public static class TestMethodHeadersNotPropagated {
@StreamListener(value = Processor.INPUT, copyHeaders = "${foo.bar:false}")
@SendTo(Processor.OUTPUT)
public String receive(String received) {
return received.toUpperCase();
}
}
@EnableBinding(Sink.class)
@EnableAutoConfiguration
public static class TestStreamListenerMethodWithTargetBeanFromOutside {