1

尝试使用 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>
4

1 回答 1

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/docs/current/reference/html/message-transformation.html#header-enricher

于 2018-11-14T15:23:43.097 回答