获取变压器中的消息头 - Spring集成

时间:2017-08-07 06:02:21

标签: java spring spring-integration

我正在使用control-bus和inbound-channel-adapter来读取csv文件的内容。用例是我应该能够检索在Transformer中启动入站适配器时使用的标头。

1)Spring-config.xml

        

<int:channel id="channel">
    <int:queue capacity="10" />
</int:channel>

<file:inbound-channel-adapter id="filesIn" 
    auto-startup="false" directory="file:D:/sample" filename-pattern="*.csv" >
    <int:poller id="poller" fixed-delay="500" />
</file:inbound-channel-adapter>

<int:outbound-channel-adapter auto-startup="true" id="dataout" ref="FileToOutputChannel" method="processContent"/>

<int:transformer id="filetoPojoTransformer" 
    input-channel="filesIn" method="processContent"
    ref="FileToPOJOTransformer" output-channel="dataout"/>



<bean id="FileToPOJOTransformer" class="com.process.FileToPOJOTransformer">

</bean>

<bean id="FileToOutputChannel" class="com.process.FileToOutputChannel">

</bean>

2)FileToPOJOTransformer

 public class FileToPOJOTransformer {

public Message<String> processContent(Message<String> msfile)
{
    System.out.println("This is sample"+msfile.getHeaders());
    // Output here..headers={id=541b6b09-0ace-238b-a30d-25d5a347b93e, timestamp=1502100632757}]
    return msfile;
}

 }

3)调用类

  public static void main(String[] args) {
         ApplicationContext context = new ClassPathXmlApplicationContext("HelloWorld.xml");


    DirectChannel controlchannel = (DirectChannel) context.getBean("controlchannel");

    Message<String> message = MessageBuilder.withPayload("@'filesIn.adapter'.start()")
            .setHeader("Message_Header1", "Message_Header1_Value")
            .setHeader("Message_Header2", "Message_Header2_Value")
            .build();
    controlchannel.send(message);

}

我需要在FileToPOJOTransformer的ProcessContent方法中使用 Message_Header1

1 个答案:

答案 0 :(得分:1)

控制总线消息只是启动适配器。它的标题不会被转移到适配器生成的任何消息。您需要在适配器和转换器之间添加<int:header-enricher/>以向这些消息添加标头。