标签:
本项目主要是通过在Spring平台上配置Camel、FTP,实现定时从FTP服务器下载文件到本地、解析文件、存入数据库等功能。
Spring Boot有几种自动生成空项目的机制:CLI、Spring tool suite、网站Spring Initializr,我们选择第三个。
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>2.18.0</version> </dependency>
<dependencies> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-spring-boot-starter</artifactId> <version>2.18.0</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-ftp</artifactId> <version>2.18.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
ftp.server.info=sftp://172.16.20.133:22/../home/temp/data?username=root&password=root&delay=5s&move=done&readLock=rename ftp.local.dir=file:C:/ftp/test
注意:sftp服务器的文件位置是相对于root登录后的相对地址(被这里坑到了),delay=5s是每隔5秒钟扫描ftp服务器上是否有新文件生成,如果有下载到本地,并将服务器上的文件转移到done文件夹(/home/temp/data/done),readLock=rename可以阻止camel读取正在被写入的文件
@Component public class DownloadRouteDemo extends RouteBuilder { private static Logger logger = LoggerFactory.getLogger( DownloadRouteDemo.class ); @Value("${ftp.server.info}") private String sftpServer; @Value("${ftp.local.dir}") private String downloadLocation; @Override public void configure() throws Exception { from( sftpServer ).to( downloadLocation ).log(LoggingLevel.INFO, logger, "Downloaded file ${file:name} complete."); } }
注意:要继承camel的RouteBulider,重写configure方法,大意是从ftp服务器下载文件到本地,并输出文件名(运行时所需必要信息都配置在application.properties文件中)
camel.springboot.main-run-controller=true
route.parserfile.info = {{ftp.local.dir}}?delay=10s&move=done&readLock=rename route.parserfile.dir = {{ftp.local.dir}}/done
注意两个花括号是引用其他变量的配置
@Component public class LocationFileProcessor implements Processor { private static Logger logger = LoggerFactory.getLogger( LocationFileProcessor.class ); @Value("${ftp.local.dir}") private String fileDir; @Autowired OrderService orderService;//业务逻辑处理组件 @Override public void process(Exchange exchange) throws Exception { GenericFileMessage<RandomAccessFile> inFileMessage = (GenericFileMessage<RandomAccessFile>) exchange.getIn(); String fileName = inFileMessage.getGenericFile().getFileName();//文件名 String splitTag = File.separator;//系统文件分隔符 logger.info(fileDir + splitTag + fileName);//文件的绝对路径 orderService.process(fileDir + splitTag + fileName);//解析入库等操作 } }
@Component public class LocalTransformRoute extends RouteBuilder { private static Logger logger = LoggerFactory.getLogger( LocalTransformRoute.class ); @Value("${route.parserfile.info}") private String location; @Value("${route.parserfile.dir}") private String locationDir; @Autowired LocationFileProcessor locationFileProcessor; @Override public void configure() throws Exception { from( location ).process( locationFileProcessor ).to( locationDir ).log(LoggingLevel.INFO, logger, "tirans file ${file:name} complete."); } }
注意,比上面的路由多了process配置,即业务逻辑处理配置
Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件
标签:
原文地址:http://www.cnblogs.com/kanjiabin/p/5954833.html