Spring integration : Facing issue while updating the header value again

710 views Asked by At

Trying to place a file in multiple directories using single outbound adapter using spring-integration-file.

In order to achieve that, having a loop in front of the file:outbound-gateway to modify message header target directory on each iteration and send all of them to the same channel again and again till the target count decreases to 0.

Able to loop back to the header enricher channel.but Since we are enriching the header value for the same header name again. the value is not getting updated for header name TARGET_DIR. There is no exception also.

Would like to know some solution if its possible to update the header value again and again for the same header name or is it possible to give the dynamic header name at runtime.

Have tried to delete the TARGET_DIR header using header:filter while looping back. But did not work out.

 !-- 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

There are 1 answers

1
Artem Bilan On BEST ANSWER

You are missing the fact that header-enricher has an extra option:

    <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>

As well as the sub-element header has its own:

    <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>

See also docs on the matter: https://docs.spring.io/spring-integration/docs/current/reference/html/message-transformation.html#header-enricher