简体   繁体   English

在Transformer中获取消息标题-Spring集成

[英]Get Message Headers in transformer- Spring integration

I am using control-bus and inbound-channel-adapter to read contents of csv file. 我正在使用Control-bus和inbound-channel-adapter读取csv文件的内容。 Use case is i should be able to retrieve headers used while starting inbound adapter inside Transformer. 用例是我应该能够检索在Transformer中启动入站适配器时使用的标头。

1) Spring-config.xml 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 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) Invoke class 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);

}

I need to have Message_Header1 inside FileToPOJOTransformer's ProcessContent method. 我需要在FileToPOJOTransformer的ProcessContent方法中包含Message_Header1

The control bus message is simply to start the adapter. 控制总线消息仅是启动适配器。 It's headers won't be transferred to whatever message(s) are generated by the adapter. 它的标头不会传输到适配器生成的任何消息。 You need to add <int:header-enricher/> between the adapter and transformer to add headers to those messages. 您需要在适配器和转换器之间添加<int:header-enricher/> ,以向这些消息添加标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM