2016-09-28 133 views
0

我正在使用Spring SFTP出站通道適配器上傳SFTP服務器上的文件與重試機制。如果上傳出現錯誤,我會發送電子郵件並在重試一定次數後將文件移動到錯誤文件夾中。春季集成:問題與故障ChannelChannel

下面是我的配置。

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

我面臨的問題是,我收到每個文件2封電子郵件。無法找出問題。

這裏是彈簧集成日誌:spring-integration.logs

+0

請爲'org.springframework.integration'分享'DEBUG'日誌。沒有'retryAdvice',你會收到多少電子郵件? –

+0

@ArtemBilan:爲彈簧整合日誌提供了鏈接。沒有'retryAdvice'時試過2封郵件, – Vinod

回答

1

根據你的日誌,我們從源代碼目錄兩個文件:

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,因此有兩個關於錯誤的電子郵件。每個文件一個。

根據您的日誌沒有更多額外的電子郵件發送。

不,我爲每個文件收到2封郵件。所以,總共有4封郵件,因爲有2個文件。

好吧,讓我們再看看你的配置一次!

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

這不通過Gmail發送我的兩個副本,但這並不意味着其他的郵件服務器不會將它像兩封電子郵件向同一收件人。

+0

不,我爲每個文件收到2封郵件。所以,總共有4封郵件,因爲有2個文件。 – Vinod

+0

謝謝,這是問題所在。我爲to和cc使用了相同的電子郵件ID。 – Vinod