1、Spring 中集成camel-ftp
??近期項目中涉及到定期讀取并解析ftp服務(wù)器上的文件,自己實(shí)現(xiàn)ftp-client有些復(fù)雜,而且還不知道要踩多少坑,因此考慮集成camel-ftp的方式來解決ftp文件的下載問題。自己則專注于文件的解析工作.
附上demo地址: https://github.com/LuckyDL/java-demos
1.1、POM引用
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.22.1</version>
</dependency>
- 注意:
在選擇版本的時候,如果SpringBoot版本是1.5.10.RELEASE的話,那么camel的版本最高只能使用2.21.2,使用2.22版本將會報錯。經(jīng)測試的配套關(guān)系如下:
SrpingBoot Camel 1.5 <=2.21.2 2.0 >=2.22.x 其他情況都會出現(xiàn)錯誤.
1.2、SpringBoot application.yml配置
ftp:
addr: 172.18.18.19:21 # ftp地址、端口
name: ftpuser
password: ftp2018
options: password=${ftp.password}&readLock=rename&delay=10s&binary=true&filter=#zipFileFilter&noop=true&recursive=true
url: ftp://${ftp.name}@${ftp.addr}/?${ftp.options}
# 本地下載目錄
local-dir: /var/data
# 后臺運(yùn)行進(jìn)程
camel:
springboot:
main-run-controller: true
management:
endpoint:
camelroutes:
enabled: true
read-only: true
配置說明:
- delay:每次讀取時間間隔
- filter: 指定文件過濾器
- noop:讀取后對源文件不做任何處理
- recursive:遞歸掃描子目錄,需要在過濾器中允許掃描子目錄
- readLock:對正在寫入的文件的處理機(jī)制
更多參數(shù)配置見官方手冊
1.3、配置路由
??要配置從遠(yuǎn)端服務(wù)器下載文件到本地,格式如下,from內(nèi)部為我們在上面配置的url,to為本地文件路徑。
@Component
public class DownloadRoute extends RouteBuilder {
/** logger */
private static final Logger logger = LoggerFactory.getLogger(DownloadRoute.class);
@Value("${ftp.server.info}")
private String sftpServer;
@Value("${ftp.local.dir}")
private String downloadLocation;
@Autowired
private DataProcessor dataProcessor;
@Override
public void configure() throws Exception{
from(sftpServer)
.to(downloadLocation)
.process(dataProcessor)
.log(LoggingLevel.INFO, logger, "Download file ${file:name} complete.");
}
}
說明:
?若將from配置為本地地址,to配置為遠(yuǎn)端地址,則可以實(shí)現(xiàn)向遠(yuǎn)端服務(wù)器上傳文件
?process是數(shù)據(jù)處理器,如果僅僅是下載文件到本地,那么就不需要該配置。
我們也可以配置多條路由也處理不同的業(yè)務(wù),比如有多個ftp服務(wù)器需要連接的場景:
@Override
public void configure() throws Exception{
// route1
from(sftpServer)
.to(downloadLocation)
.process(dataProcessor)
.log(LoggingLevel.INFO, logger, "Download file ${file:name} complete.");
// route2
from(xxx).to(xxxx);
// route3
from(xxxx).to(xxx).process(xxx);
}
1.4、配置文件過濾
??如果ftp服務(wù)器上有很多文件,但是我們需要的只是其中的一種,由業(yè)務(wù)層來實(shí)現(xiàn)文件過濾肯定不合適,我們可以使用camel-ftp的文件過濾器,通過url中的filter來指定,如“filter=#zipFileFilter”, 用戶需要實(shí)現(xiàn)GenericFileFilter接口的accept方法。
??例如我們只需要下載后綴名為.zip的壓縮包到本地,過濾器的編寫方法如下,因?yàn)槲乙f歸掃描子目錄,因此類型為目錄的文件也需要允許通過。
/**
* camel ftp zip文件過濾器
*/
@Component
public class ZipFileFilter implements GenericFileFilter {
@Override
public boolean accept(GenericFile file) {
return file.getFileName().endsWith(".zip") || file.isDirectory();
}
}
1.5、文件處理器
??文件處理器就是我們對下載到本地的文件進(jìn)行處理的操作,比如我們可能需要對下載的文件重新規(guī)劃目錄;或者解析文件并進(jìn)行入庫操作等。這就需要通過實(shí)現(xiàn)Processer的process方法。
??本文中的demo就是通過processor來解析zip包中的文件內(nèi)容:
@Component
public class DataProcessor implements Processor {
/** logger */
private static final Logger logger = LoggerFactory.getLogger(DataProcessor.class);
@Value("${ftp.local-dir}")
private String fileDir;
@Override
public void process(Exchange exchange) throws Exception {
GenericFileMessage<RandomAccessFile> inFileMessage = (GenericFileMessage<RandomAccessFile>) exchange.getIn();
String fileName = inFileMessage.getGenericFile().getFileName();
String file_path = fileDir + '/' + fileName;
readZip(file_path);
}
... // 省略數(shù)據(jù)處理方法
}
2、參考資料
??關(guān)于camel ftp的各個參數(shù)配置,參見官方手冊:http://camel.apache.org/ftp2.html
??此處需要注意的是,camel ftp手冊里面只寫了ftp獨(dú)有的一些配置項,camel-ftp組件繼承自camel-file,手冊里面有說明,就一句話,稍不注意就可能忽略了,筆者就是沒注意,被遞歸掃描子目錄的問題折騰了2天(╥﹏╥ 閱讀文檔要細(xì)心,可能原作者字字珠璣)。。。因此有一些參數(shù)配置項可能在camel-ftp手冊里面找不到,請移步至:http://camel.apache.org/file2.html