Camel實現FTP下載
阿新 • • 發佈:2018-12-04
FTP路徑有存在日期資料夾,且世界時、北京時混合
public void threadFTP(ExecutorService cachedThreadPool) {
cachedThreadPool.execute(new Runnable() {
@Override
public void run() {
CamelContext context = new DefaultCamelContext();
try {
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
String day = DateUtils.buildNowStr(DateUtils.DAY_FORMAT);
IdempotentRepository<String> ir = FileIdempotentRepository.fileIdempotentRepository(new File("target/fire.dat"), 250, 512000);
//二進位制傳輸、處理後不移動檔案、遞迴處理目錄、被動模式、目錄輪詢1分鐘、如果檔案已存在忽略
from("ftp://xxx:[email protected]/ShanDong/"+day+"/Fire_Result?binary=true&noop=true&recursive=true&passiveMode=true&useFixedDelay=true&delay=60000&idempotent=true" )
.idempotentConsumer(header("CamelFileName"), ir)
.to("?fileExist=Ignore&tempFileName=fire.tmp"+day+"?fileExist=Ignore")
.log(LoggingLevel.INFO, logger, "Downloaded FTP-Fire file ${file:name} complete.");
}
});
context.start();
Thread.sleep(24*60*60*1000);
context.stop();
} catch (Exception e) {
logger.info("exception-------------"+e);
}
}
});
}
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
@Scheduled(cron="0 0 0 * * ?")
private void downftp() throws InterruptedException {
new FileCopierThread().threadFTP(cachedThreadPool);
Thread.sleep(10000);
int threadCount = ((ThreadPoolExecutor)cachedThreadPool).getActiveCount();
}
@PostConstruct
public void initMethod() throws Exception {
new FileCopierThread().threadFTP(cachedThreadPool);
}