春季集成:再次更新标头值时面临问题

时间:2018-11-14 15:18:14

标签: spring spring-integration spring-integration-sftp spring-integration-aws

尝试使用单个出站适配器通过spring-integration-file将文件放置在多个目录中。

为了实现这一点,在file:outbound-gateway前面有一个循环,可以在每次迭代中修改消息头目标目录,并将它们一次又一次地发送到同一通道,直到目标计数减少到0。

可以循环返回标头扩展器通道。但是由于我们再次为同一标头名称扩展了标头值。标头名称TARGET_DIR的值未更新。也没有例外。

想知道一些解决方案,是否可以针对相同的标头名称一次又一次地更新标头值,或者是否可以在运行时提供动态标头名称。

尝试回送时使用header:filter删除TARGET_DIR标头。但是没有解决。

 !-- header enricher -->
 <integration:header-enricher input-channel="filesHeaderEnricherChannel" output-channel="filesOut">
<integration:header name="TARGET_COUNT" method="getTargetCount" ref="headerEnricher"/>
<integration:header name="TARGET_DIR" method="getTargetPath" ref="headerEnricher"/>     
</integration:header-enricher>

        <integration:chain id="filesOutChain" input-channel="filesOut" output-channel="filesOutChainChannel">
            <integration:transformer expression="headers.FILE"/>
                <file:outbound-channel-adapter id="fileMover" 
                    auto-create-directory="true"
                    directory-expression="headers.TARGET_DIR"
                    mode="REPLACE">
                    <file:request-handler-advice-chain>
                        <ref bean="retryAdvice" />
                    </file:request-handler-advice-chain>
                </file:outbound-channel-adapter>    
           </integration:chain> 

     <!-- decreasing the count on each loop -->
    <!-- looping to header enricher channel again as output channel to update the target directory -->
  <integration:filter input-channel="filesOutChainChannel"  expression="headers.TARGET_COUNT != 0" output-channel="filesHeaderEnricherChannel"
                        discard-channel="filesArchiveChannel">                       
  </<integration:filter>

1 个答案:

答案 0 :(得分:2)

您错过了header-enricher有一个额外选择的事实:

    <xsd:attribute name="default-overwrite">
        <xsd:annotation>
            <xsd:documentation>
                Specify the default boolean value for whether to overwrite existing
                header values. This will
                only
                take effect for
                sub-elements that do not provide their own 'overwrite' attribute. If the
                'default-overwrite'
                attribute is not
                provided, then the specified header values will NOT overwrite any
                existing ones with the same
                header
                names.
                </xsd:documentation>
        </xsd:annotation>
        <xsd:simpleType>
            <xsd:union memberTypes="xsd:boolean xsd:string" />
        </xsd:simpleType>
    </xsd:attribute>

以及子元素header都有自己的特色:

    <xsd:attribute name="overwrite">
        <xsd:annotation>
            <xsd:documentation>
                Boolean value to indicate whether this header value should overwrite an
                existing header value for
                the same name.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:simpleType>
            <xsd:union memberTypes="xsd:boolean xsd:string" />
        </xsd:simpleType>
    </xsd:attribute>

另请参阅文档:https://docs.spring.io/spring-integration/reference/html/messaging-transformation-chapter.html#header-enricher