Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iainporter/b0b034b369bd81f964a1e45b814cd831 to your computer and use it in GitHub Desktop.
Save iainporter/b0b034b369bd81f964a1e45b814cd831 to your computer and use it in GitHub Desktop.
Spring Integration DSL transform with header annotation
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@DirtiesContext
public class TransformTest {
@Autowired
@Qualifier("transformInput")
private MessageChannel transformInput;
@Test
public void transformWithHeader() {
QueueChannel replyChannel = new QueueChannel();
Message<?> message = MessageBuilder.withPayload("Foo")
.setHeader(MessageHeaders.REPLY_CHANNEL, replyChannel)
.build();
this.transformInput.send(message);
Message<?> receive = replyChannel.receive(5000);
assertNotNull(receive);
Object payload = receive.getPayload();
assertThat(payload, instanceOf(MyPojo.class));
MyPojo result = (MyPojo) payload;
assertEquals("FooBar", result.getName());
}
@Configuration
@EnableIntegration
public static class TestConfig {
@Bean
@Autowired
public IntegrationFlow myFlow(MyTransform myTransform) {
return IntegrationFlows.from("transformInput")
.enrichHeaders(h -> h.header("Foo", "Bar"))
// .transform(myTransform) //this won't compile
.handle((p, h) -> myTransform.transform((String) p, (String) h.get("Foo")))
.get();
}
@Bean
public MyTransform myTransformBean() {
return new MyTransform();
}
}
private static class MyPojo {
private final String name;
private MyPojo(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
@Component
public static class MyTransform {
@Transformer
public MyPojo transform(String originalMessage, @Header("Foo") String header) {
return new MyPojo(originalMessage + header);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment