2015-04-27 102 views
0

我似乎無法弄清楚如何能夠做到以下幾點:郵件在一個郵件從入站文件連接多個文件

1)騾子需要從目錄拿起文件,並把它放在一個ftp (這部分正在工作)

2)接下來,它需要郵寄上傳到單個郵件中的文件,郵件正文中提到了上傳文件的文件名。

=>這第二部分我不明白如何使用騾塊來做到這一點。我嘗試使用不同的屬性範圍將我的文件名保存在列表中,但每次到達聚合塊時,我的所有屬性都消失了(包括會話屬性)。這些文件雖然彙總,但我也需要文件名。

我在這裏錯過了一些明顯的東西嗎?謝謝!

這裏是我的xml:

<flow name="KCM-FTP" doc:name="KCM-FTP"> 
    <file:inbound-endpoint responseTimeout="10000" doc:name="File" path="${path.kcm.uploadfolder}"></file:inbound-endpoint> 
    <message-properties-transformer doc:name="Message Properties"> 
     <add-message-property key="MULE_CORRELATION_ID" value="1"/> 
     <add-message-property key="MULE_CORRELATION_GROUP_SIZE" value="100"/> 
    </message-properties-transformer> 

    <ftp:outbound-endpoint host="${ftp.host}" port="${ftp.port}" path="${path.ftp.inputfolder}" user="${ftp.username}" password="${ftp.password}" responseTimeout="10000" doc:name="FTP"></ftp:outbound-endpoint> 
    <collection-aggregator timeout="15000" failOnTimeout="false" doc:name="Collection Aggregator"></collection-aggregator> 
    <message-properties-transformer doc:name="Message Properties"> 
     <add-message-property key="EmailHeader" value="#['KCM uploader \n Run: ' +server.dateTime + '\n\nFiles uploaded:\n']"></add-message-property> 
    </message-properties-transformer> 
    <foreach doc:name="For Each"> 
     <set-attachment attachmentName="test" value="#[payload]" contentType="text/plain" doc:name="Attachment"/> 
    </foreach> 

    <smtp:outbound-endpoint host="${smtp.host}" port="${smtp.port}" to="${mail.to}" subject="${mail.subject}" responseTimeout="10000" doc:name="SMTP" from="${mail.from}"></smtp:outbound-endpoint> 
</flow> 

回答

0

我能夠通過實現一個小的Java類自己解決它。很明顯,消息屬性並沒有丟失,你只是無法再在調試器中看到它們,因爲調試器只顯示了來自聚合器的messageCollection的屬性。

我的流量只需要運行1次/小時+文件數量不超過100 /小時,因此我的簡單聚合器在1分鐘後才超時,以決定沒有更多文件需要處理。

流量XML:

<file:connector name="File" autoDelete="true" streaming="false" validateConnections="true" doc:name="File"/> 
<flow name="KCM-FTP" doc:name="KCM-FTP"> 
    <file:inbound-endpoint responseTimeout="10000" doc:name="File" path="${path.kcm.uploadfolder}" ></file:inbound-endpoint> 
    <message-properties-transformer doc:name="Set Aggregation Properties"> 
     <add-message-property key="MULE_CORRELATION_ID" value="1"/> 
     <add-message-property key="MULE_CORRELATION_GROUP_SIZE" value="100"/> 
    </message-properties-transformer> 
    <ftp:outbound-endpoint host="${ftp.host}" port="${ftp.port}" path="${path.ftp.inputfolder}" user="${ftp.username}" password="${ftp.password}" responseTimeout="10000" doc:name="FTP"/> 
    <collection-aggregator timeout="15000" failOnTimeout="false" doc:name="Collection Aggregator"/> 
    <message-properties-transformer doc:name="Set Email Header"> 
     <add-message-property key="EmailHeader" value="#['KCM uploader \n Run: ' +server.dateTime + '\n\nFiles uploaded:\n']"/> 
    </message-properties-transformer> 
    <custom-transformer class="com.company.kcm.Transformer.FileParser" doc:name="Add mail attachments"/> 
    <smtp:outbound-endpoint host="${smtp.host}" port="${smtp.port}" to="${mail.to}" from="${mail.from}" subject="${mail.subject}" responseTimeout="10000" doc:name="SMTP"/> 

</flow> 

的Java變壓器實現:

public class FileParser extends AbstractMessageTransformer implements Transformer { 

private String body; 

@Override 
public Object transformMessage(MuleMessage src, String outputEncoding) throws TransformerException {   

    try { 

     if(src instanceof MuleMessageCollection) { 
      MuleMessageCollection messageCollection = (MuleMessageCollection)src; 

      MuleMessage[] messagesAsArray = messageCollection.getMessagesAsArray(); 
      body = messageCollection.getOutboundProperty("EmailHeader").toString(); 

      //Add all input files as email attachments 
      for (MuleMessage muleMessage : messagesAsArray) { 

       messageCollection.addOutboundAttachment((String) muleMessage.getInboundProperty("originalFilename"), muleMessage.getPayload(),"text/plain"); 

       body += (String) muleMessage.getInboundProperty("originalFilename") + "\n"; 
      } 

     } 


    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 

    return body; 

}}

0

你的流量將運行一次爲每個文件入站端點查找文件。除非將它們存儲在其他地方,否則您將無法將所有文件一起發送到同一封電子郵件中。但是......你怎麼知道你讀完所有文件?你什麼時候開始執行流程?您可以使用HTTP偵聽器觸發流程,然後使用Java組件拾取所有可用文件。