1、Spring 中集成camel-ftp
近期項(xiàng)目中涉及到定期讀取并解析ftp服務(wù)器上的文件,自己實(shí)現(xiàn)ftp-client有些復(fù)雜,而且還不知道要踩多少坑,因此考慮集成camel-ftp的方式來(lái)解決ftp文件的下載問(wèn)題。自己則專注于文件的解析工作.
附上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>
- 注意:
在選擇版本的時(shí)候,如果SpringBoot版本是1.5.10.RELEASE的話,那么camel的版本最高只能使用2.21.2,使用2.22版本將會(huì)報(bào)錯(cuò)。經(jīng)測(cè)試的配套關(guān)系如下:
SrpingBoot Camel 1.5 <=2.21.2 2.0 >=2.22.x 其他情況都會(huì)出現(xiàn)錯(cuò)誤.
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
# 后臺(tái)運(yùn)行進(jìn)程
camel:
springboot:
main-run-controller: true
management:
endpoint:
camelroutes:
enabled: true
read-only: true
配置說(shuō)明:
- delay:每次讀取時(shí)間間隔
- filter: 指定文件過(guò)濾器
- noop:讀取后對(duì)源文件不做任何處理
- recursive:遞歸掃描子目錄,需要在過(guò)濾器中允許掃描子目錄
- readLock:對(duì)正在寫(xiě)入的文件的處理機(jī)制
更多參數(shù)配置見(jiàn)官方手冊(cè)
1.3、配置路由
要配置從遠(yuǎn)端服務(wù)器下載文件到本地,格式如下,from內(nèi)部為我們?cè)谏厦媾渲玫膗rl,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.");
}
}
說(shuō)明:
若將from配置為本地地址,to配置為遠(yuǎn)端地址,則可以實(shí)現(xiàn)向遠(yuǎn)端服務(wù)器上傳文件
process是數(shù)據(jù)處理器,如果僅僅是下載文件到本地,那么就不需要該配置。
我們也可以配置多條路由也處理不同的業(yè)務(wù),比如有多個(gè)ftp服務(wù)器需要連接的場(chǎng)景:
@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、配置文件過(guò)濾
如果ftp服務(wù)器上有很多文件,但是我們需要的只是其中的一種,由業(yè)務(wù)層來(lái)實(shí)現(xiàn)文件過(guò)濾肯定不合適,我們可以使用camel-ftp的文件過(guò)濾器,通過(guò)url中的filter來(lái)指定,如“filter=#zipFileFilter”, 用戶需要實(shí)現(xiàn)GenericFileFilter接口的accept方法。
??例如我們只需要下載后綴名為.zip的壓縮包到本地,過(guò)濾器的編寫(xiě)方法如下,因?yàn)槲乙f歸掃描子目錄,因此類型為目錄的文件也需要允許通過(guò)。
/**
* camel ftp zip文件過(guò)濾器
*/
@Component
public class ZipFileFilter implements GenericFileFilter {
@Override
public boolean accept(GenericFile file) {
return file.getFileName().endsWith(".zip") || file.isDirectory();
}
}
1.5、文件處理器
文件處理器就是我們對(duì)下載到本地的文件進(jìn)行處理的操作,比如我們可能需要對(duì)下載的文件重新規(guī)劃目錄;或者解析文件并進(jìn)行入庫(kù)操作等。這就需要通過(guò)實(shí)現(xiàn)Processer的process方法。
本文中的demo就是通過(guò)processor來(lái)解析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的各個(gè)參數(shù)配置,參見(jiàn)官方手冊(cè):http://camel.apache.org/ftp2.html
此處需要注意的是,camel ftp手冊(cè)里面只寫(xiě)了ftp獨(dú)有的一些配置項(xiàng),camel-ftp組件繼承自camel-file,手冊(cè)里面有說(shuō)明,就一句話,稍不注意就可能忽略了,筆者就是沒(méi)注意,被遞歸掃描子目錄的問(wèn)題折騰了2天(╥﹏╥ 閱讀文檔要細(xì)心,可能原作者字字珠璣)。。。因此有一些參數(shù)配置項(xiàng)可能在camel-ftp手冊(cè)里面找不到,請(qǐng)移步至:http://camel.apache.org/file2.html