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. DFS-深度优先搜索与BFS-广度优先搜索

    1.DFS DFS是一个递归过程.(类似于二叉树的前序遍历) 参考:深度优先搜索(Depth-First-Search)精髓 2.BFS 可以理解为按层遍历,借助队列结构来实现.(类似于二叉树的层次遍 ...

  2. 组件:slot插槽

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  3. vue 路由 URL传参

    源码如下: import Vue from 'vue' import VueRouter from 'vue-router' Vue.use(VueRouter) //全局使用该组件 const ro ...

  4. 简单的选项卡制作(原生JS)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  5. .h头文件 .lib动态链接库文件 .dll 动态链接库

    (1).h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib 不是.dll 若生成了DLL ,则肯定也生成 LIB文件 如果要完成源代码的编译和链接,有头文件和 ...

  6. KOA 学习(一)

    一.安装KOA 用npm下载KOA 就会在koa文件夹下生成 二.输出hello,world 我下载的KOA版本号是2.0.1 const Koa = require('koa'); const ap ...

  7. istio1.1(openshift) 流量路由

    1.准备测试应用 准备两个nginx Pod和一个proxy 创建应用 apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata ...

  8. poj 1041 John's trip——欧拉回路字典序输出

    题目:http://poj.org/problem?id=1041 明明是欧拉回路字典序输出的模板. 优先队列存边有毒.写跪.学习学习TJ发现只要按边权从大到小排序连边就能正常用邻接表了! 还有一种存 ...

  9. 调用本地摄像头并通过canvas拍照

    首先我们需要新建一个video标签,并且放到html里边 var video = document.createElement("video"); video.autoplay=& ...

  10. Linux学习(一):软链接和硬链接

    今天起,决定开始自学Linux命令及Shell脚本,并用Linux学习(命令行,Shell及其他知识点)这一系列记录下自己的心路历程,内容不分先后,只记录自己觉得有必要的,简单的就不记了! 第一个知识 ...