简体   繁体   中英

Spring integration: issue with failureChannel

I am using Spring SFTP outbound channel adapter to upload files on SFTP server with retry mechanism. In case of error in uploading, I am sending email and moving the file to error folder after certain number of retry.

Below is my configuration.

<file:inbound-channel-adapter id="csvFileChannel"
    directory="${csv.base.directory}" filename-regex="^(.*).csv"
    comparator="lastModifiedComparator" prevent-duplicates="true">
    <int:poller fixed-rate="5000" />
</file:inbound-channel-adapter>

<bean id="lastModifiedComparator"
    class="org.apache.commons.io.comparator.LastModifiedFileComparator" />
<bean id="sftpSessionFactory"
    class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory"
    lazy-init="true">
    <property name="host" value="${sftp.host}" />
    <property name="port" value="${sftp.port}" />
    <property name="user" value="${sftp.user}" />
    <property name="password" value="${sftp.password}" />
</bean>

<int-sftp:outbound-channel-adapter
    id="sftpOutboundAdapter" session-factory="sftpSessionFactory" channel="csvFileChannel"
    remote-file-separator="/" remote-filename-generator-expression="payload.getName()"
    remote-directory="/" mode="REPLACE">
    <int-sftp:request-handler-advice-chain>
        <bean
            class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice">
            <property name="onSuccessExpression"
                value="payload.renameTo(new java.io.File('${csv.archive.directory}' , payload.name))" />
            <property name="successChannel" ref="nullChannel" />
            <property name="onFailureExpression"
                value="payload.renameTo(new java.io.File('${csv.error.directory}' , payload.name))" />
            <property name="failureChannel" ref="failChannel" />
            <property name="trapException" value="true" />
        </bean>
        <ref bean="retryAdvice" />
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-channel-adapter>

<int:handler-retry-advice id="retryAdvice"
    max-attempts="5">
    <int:fixed-back-off interval="5000" />
</int:handler-retry-advice>

<int:channel id="failChannel" />

<int:chain input-channel="failChannel" output-channel="mailMessageChannel">
    <int:transformer
        expression="'SFTP Upload failed for the file: ' + payload.failedMessage.payload.name " />
</int:chain>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value="${smtp.host}" />
    <property name="port" value="${smtp.port}" />
    <property name="username" value="${smtp.userName}" />
    <property name="password" value="${smtp.password}" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtp.auth">true</prop>
        </props>
    </property>
</bean>

<int:channel id="mailMessageChannel" />
<int:chain input-channel="mailMessageChannel">
    <int-mail:header-enricher>
        <int-mail:to value="${fail.email.to}" />
        <int-mail:cc value="${fail.email.to}" />
        <int-mail:from value="${fail.email.to}" />
        <int-mail:subject value="SFTP Upload failed" />
    </int-mail:header-enricher>
    <int-mail:outbound-channel-adapter
        mail-sender="mailSender" />
</int:chain>

The problem I am facing is that I am getting 2 e-mails for each file. Not able to figure out the issue.

Here is spring-integration logs: spring-integration.logs

According to your logs, we have two files from the source dir:

2016-09-28 22:14:34,595 [DEBUG] [org.springframework.integration.file.FileReadingMessageSource] Added to queue: [D:\CSVFiles\MyCustomerTarget_20160928221429.csv, D:\CSVFiles\MyOrderTarget_20160928221429.csv]

Maybe that confuses you a bit since both of them finish with the same suffix?

Both of these files are failed to be transferred to FTP therefore there are two email about errors. One for each file.

There is no more extra email sending according your logs.

No, I got 2 mails for each file. So, in total 4 mails as there are 2 files.

Well, let's take a look to your config one more time!

<int-mail:to value="${fail.email.to}" />
<int-mail:cc value="${fail.email.to}" />

That doesn't send me two copies via GMail, but that doesn't mean that other Mail servers won't pass it like two emails to the same recipient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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