java - Spring 集成: issue with failureChannel

标签 java spring spring-integration sftp

我正在使用 Spring SFTP 出站 channel 适配器通过重试机制在 SFTP 服务器上上传文件。如果上传出错,我会发送电子邮件并在重试一定次数后将文件移动到错误文件夹。

下面是我的配置。

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

我面临的问题是每个文件都会收到两封电子邮件。无法找出问题所在。

这里是 spring 集成日志:spring-integration.logs

最佳答案

根据您的日志,我们从源目录中有两个文件:

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]

也许这让您有点困惑,因为它们都以相同的后缀结尾?

这两个文件都无法传输到 FTP,因此有两封有关错误的电子邮件。每个文件一个。

根据您的日志,不再发送额外的电子邮件。

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

好吧,让我们再看一次您的配置!

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

这不会通过 GMail 向我发送两份副本,但这并不意味着其他邮件服务器不会将其像两封电子邮件一样传递给同一收件人。

关于java - Spring 集成: issue with failureChannel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39751843/

相关文章:

spring-integration - Spring集成聚合器: meaning of "send-timeout" XML configuration

java - Spring集成验证

java - 如何在 hazelcast 中高效地将对象添加到分布式队列中?

java - Spring MVC 中的请求响应

java - 设置 weblogic 应用程序的系统属性

java - Tomcat 服务器已启动,但未从 Controller 访问 URL

spring - 无法在 Eclipse 项目属性中看到用于将项目与 tomcat 集成的选项 targated runtime

java - 我的 android studio 构建中不断出现错误

java - JUnit - 初始化错误中的异常

java - 防止子类从父类(super class)( Activity )进一步处理