1、概要:  

  本项目主要是通过在Spring平台上配置Camel、FTP,实现定时从FTP服务器下载文件到本地、解析文件、存入数据库等功能。  

2、搭建空项目:

  Spring Boot有几种自动生成空项目的机制:CLI、Spring tool suite、网站Spring Initializr,我们选择第三个。

  1. 访问网站http://start.spring.io/,如下图
  2. 在dependencies添加依赖包的时候,在框中输入camle、jdbc、mysql会自动弹出提示,确认即为选中,如下图:
  3. 点击 generate project按钮,生成项目,并将其导入到ecipse,在pom.xml中添加camel-ftp依赖,注意版本号选择与camel-spring-boot-stater的相同
    <dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-ftp</artifactId>
    <version>2.18.0</version>
    </dependency>
  4. 完整版的pom.xml文件如下:
    <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>
  5. 待所有依赖jar下载到本地,基础项目搭建完成

3、配置Camel完成从ftp服务器定时下载文件到本地

  1. 在application.properties中配置远程FTP服务器的地址、端口、用户名和密码等信息

    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读取正在被写入的文件

  2. 配置路由,完成文件下载
    @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文件中)

  3. 为了让java进程在后台运行,需要在application.properties文件中增加如下配置
    camel.springboot.main-run-controller=true
  4. 从ftp服务器下载文件的所有工作都已完成,运行CamelFtpSpringApplication.java,如果你的ftp服务器相应的位置上有文件,就会下载到本地所配置的文件夹下

4、通过camel定时解析本地文件并保存到数据库

  1. 在application.properties中增加如下配置

    route.parserfile.info = {{ftp.local.dir}}?delay=10s&move=done&readLock=rename
    route.parserfile.dir = {{ftp.local.dir}}/done

    注意两个花括号是引用其他变量的配置

  2. 编写解析文件、入库程序等处理器
    @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);//解析入库等操作 } }
  3. 配置路由,完成业务逻辑的串联
    @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配置,即业务逻辑处理配置

  4. 至此,所有工作都已完成,重新执行CamelFtpSpringApplication.java即可实现ftp文件定时下载、业务处理等(其中省去了很多,例如入库操作等)

备注:只是camle spring ftp的一个演示demo,要运用于生产,还有好多需要完善的地方

转载至:https://www.cnblogs.com/kanjiabin/p/5954833.html

Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件 (转)的更多相关文章

  1. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  2. Apache Camel继承Spring Boot 实现文件远程复制和转移

    pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-f ...

  3. Spring Boot集成持久化Quartz定时任务管理和界面展示

    本文是对之前的一篇文章Spring+SpringMVC+mybatis+Quartz整合代码部分做的一个修改和补充, 其中最大的变化就是后台框架变成了Spring Boot. 本工程所用到的技术或工具 ...

  4. spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    spring boot集成mybatis-plus插件进行自定义sql方法开发时报nested exception is org.apache.ibatis.binding.BindingExcept ...

  5. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  6. Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权

    Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...

  7. Spring Boot 集成Shiro和CAS

    Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报  分类: Spring(42)  版 ...

  8. Spring Boot集成MyBatis开发Web项目

    1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...

  9. 玩转Spring Boot 集成Dubbo

    玩转Spring Boot 集成Dubbo 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...

随机推荐

  1. webpack配置根据浏览器自动添加css前缀的loader

    1.安装 postcss-loader autoprefixer npm install postcss-loader autoprefixer --save-dev 2.配置webpack.conf ...

  2. 60行JavaScript代码俄罗斯方块

    教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏   早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下( ...

  3. Cesium打包命令总结

    引言 Cesium实验室QQ群里有人在问Cesium的打包问题.我想干脆总结一下Cesium的打包命令特点,写篇文章,顺带庆祝一下1024程序员节.. Cesium的npm脚本有好多,其中几个和打包相 ...

  4. PAT甲级——A1033 To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  5. 【MFC 】关于对话框中的OnVScroll() 和 OnHScroll

    原文地址:[MFC 中]关于对话框中的OnVScroll() 和 OnHScroll()函数作者:Winters     对话框中的滑块,微调控件都会向OnVScroll() 和OnHScroll() ...

  6. Junit5的依赖添加及RunWith(SpringJUnit4ClassRunner.class)注解使用

    首先Junit5依赖应该配置为 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId&g ...

  7. LA2238 Fixed Partition Memory Management

    题目大意: m(m<=10)个内存区域,n(n<=50)个程序.找出一个方案来,使得平均结束时刻尽量小.题目保证有解. 同一个程序运行在不同大小的内存区域内,其运行时间不同.(注意,这里说 ...

  8. 删除n天前的文件或文件夹 bat批处理

    @echo off @echo deleting... FORFILES /p "D:\a" /D -1 /C "cmd /c echo deleting @file . ...

  9. (5)连续非周期信号的傅里叶变换(频谱) & 周期信号的傅里叶变换

    参考资料:<信号与系统(第二版)> 杨晓非 何丰 从傅里叶级数到傅里叶变换 通过分析连续周期信号的周期与频谱的关系,当周期趋于无穷大的时候,周期信号变成非周期信号.从频谱分析观点来看,当T ...

  10. 通过游戏学python 3.6 第一季 第五章 实例项目 猜数字游戏--核心代码--猜测次数--随机函数和屏蔽错误代码--优化代码及注释--简单账号密码登陆 可复制直接使用 娱乐 可封装 函数

    #猜数字--核心代码--猜测次数--随机函数和屏蔽错误代码---优化代码及注释--账号密码登陆 #!usr/bin/env python #-*-coding:utf-8-*- #QQ12411129 ...