天天看點

mule in action翻譯18 : 3.6 使用FTP傳輸mule in action翻譯18 : 3.6  使用FTP傳輸   

mule in action翻譯18 : 3.6  使用FTP傳輸   

    如果你曾涉足過網際網路,你一定記得曾經FTP是多麼的盛行。在HTTP和SSH之前,FTP是計算機間傳輸檔案的主要方式。雖然近年來由于HTTP、SCP、BitTorrent的升起,FTP日漸衰落,但你還是可能會偶爾遇到一些應用需要使用FTP。

   本節,學習使用FTP傳輸發送和接受資料。首先看如何輪詢一個遠端的FTP目錄。如何看如何通過outbound endpoint向遠端FTP站點發送資料。

配置FTP傳輸和配置FTP client是相似的。見表3.6 

mule in action翻譯18 : 3.6 使用FTP傳輸mule in action翻譯18 : 3.6  使用FTP傳輸   
mule in action翻譯18 : 3.6 使用FTP傳輸mule in action翻譯18 : 3.6  使用FTP傳輸   

3.6.1 使用 inbound FTP endpoint 接收檔案

    某些商戶隻以支援FTP的內建機制發送銷售資料給Prancing Donkey公司。為了接收這些資料,Prancing Donkey.使用mule去輪詢遠端的FTP伺服器,并把取得的資料儲存到檔案系統,以便進行後續的手工處理。下面清單展示了,他們是如何實作的。(圖3.16)

Listing 3.20 Polling a remote FTP directory every hour for new files 

<flow name="retrieveSalesStatistics">
<!--注釋1 配置ftp inbound endpoint-->
<ftp:inbound-endpoint user="joe" password="123456"
                      host="${ftp.host}"
                      port="${ftp.port}"
                      path="/ftp/incoming"
          pollingFrequency="3600000"/>
<!--儲存傳輸檔案到 ./data/sales/statistics-->
<file:outbound-endpoint path="./data/sales/statistics"/>
</flow>      
mule in action翻譯18 : 3.6 使用FTP傳輸mule in action翻譯18 : 3.6  使用FTP傳輸   

    注釋1處 配置inbound endpoint 。你指定了使用者、密碼、主機、端口、路徑和對遠端伺服器的輪詢頻率。

FTP傳輸每隔一個小時将建立連接配接并傳輸新檔案到注釋2定義的outbound endpoint 。這個outbound endpoint将把檔案寫到 ./data/sales/statistics 目錄.

    最佳實踐  FTP傳輸要和FTP inbound endpoint一起聯合使用。通過一個flow來流化FTP資料時,如果檔案         很大可能會有問題。比如一些FTP伺服器有逾時限制,當你正在現在檔案進行的處理時可能會出現逾時         問題。一個更好的選擇是發送FTP檔案到一個檔案outbound endpoint,并在這裡執行處理。

3.6.2 使用outbound FTP endpoint發送資料。

    有時你需要發送檔案到遠端的FTP伺服器。上例中的銷售商要求 Prancing Donkey 周期性的使用FTP方式發送他們産品目錄。為了實作這個功能,Prancing Donkey設定了一個存放檔案的共享目錄,mule會周期性的輪詢并發送檔案。如下清單所示(見表3.17)

Listing 3.21 Sending a file to a remote FTP server

<flow name="ftpProductCatalog">
<!--注釋1 輪詢該目錄的檔案-->
<file:inbound-endpoint path="./data/in"/>
<!--發送檔案到 ftp伺服器-->
<ftp:outbound-endpoint user="joe" 
                   password="123456"
                       host="${ftp.host}" 
                       port="${ftp.port}"
                       path="/data/prancingdonkey/catalog"/>
</flow>      
mule in action翻譯18 : 3.6 使用FTP傳輸mule in action翻譯18 : 3.6  使用FTP傳輸   

   注釋1處 配置inbound endpoint 。檔案将被放置到 ./data/in目錄,然後将被傳送給

   注釋2處的 FTP outbound endpoint。 這個ftp outbound endpoint将把檔案放到FTP伺服器的                                         /data/prancingdonkey/catalog  目錄。

繼續閱讀