Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件 (转)
1、概要:
本项目主要是通过在Spring平台上配置Camel、FTP,实现定时从FTP服务器下载文件到本地、解析文件、存入数据库等功能。
2、搭建空项目:
Spring Boot有几种自动生成空项目的机制:CLI、Spring tool suite、网站Spring Initializr,我们选择第三个。
- 访问网站http://start.spring.io/,如下图

- 在dependencies添加依赖包的时候,在框中输入camle、jdbc、mysql会自动弹出提示,确认即为选中,如下图:

- 点击 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> - 完整版的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>
- 待所有依赖jar下载到本地,基础项目搭建完成
3、配置Camel完成从ftp服务器定时下载文件到本地
- 在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读取正在被写入的文件
- 配置路由,完成文件下载

@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文件中)
- 为了让java进程在后台运行,需要在application.properties文件中增加如下配置
camel.springboot.main-run-controller=true
- 从ftp服务器下载文件的所有工作都已完成,运行CamelFtpSpringApplication.java,如果你的ftp服务器相应的位置上有文件,就会下载到本地所配置的文件夹下
4、通过camel定时解析本地文件并保存到数据库
- 在application.properties中增加如下配置
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配置,即业务逻辑处理配置
- 至此,所有工作都已完成,重新执行CamelFtpSpringApplication.java即可实现ftp文件定时下载、业务处理等(其中省去了很多,例如入库操作等)
备注:只是camle spring ftp的一个演示demo,要运用于生产,还有好多需要完善的地方
转载至:https://www.cnblogs.com/kanjiabin/p/5954833.html
Apache Camel 与 Spring Boot 集成,通过FTP定时采集、处理文件 (转)的更多相关文章
- 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法
spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...
- Apache Camel继承Spring Boot 实现文件远程复制和转移
pom.xml <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-f ...
- Spring Boot集成持久化Quartz定时任务管理和界面展示
本文是对之前的一篇文章Spring+SpringMVC+mybatis+Quartz整合代码部分做的一个修改和补充, 其中最大的变化就是后台框架变成了Spring Boot. 本工程所用到的技术或工具 ...
- 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 ...
- Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件
上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...
- Spring Boot 集成 Swagger2 与配置 OAuth2.0 授权
Spring Boot 集成 Swagger2 很简单,由于接口采用了OAuth2.0 & JWT 协议做了安全验证,使用过程中也遇到了很多小的问题,多次尝试下述配置可以正常使用. Maven ...
- Spring Boot 集成Shiro和CAS
Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报 分类: Spring(42) 版 ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 玩转Spring Boot 集成Dubbo
玩转Spring Boot 集成Dubbo 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...
随机推荐
- struts1 总结吧
以前都是使用struts2,但是新公司要使用struts1,所有只有硬着头皮上了. 一.Dynamic Method Invoc : 自定义的 Action 必须继承 DispatchAction 而 ...
- centos7如何配置yum仓库
centos7如何配置yum仓库 一.总结 一句话总结: 备份原仓库配置原件,接来下按需求 百度 是指定本地光盘作为yum仓库,还是使用网络源作为yum仓库 二.centos7如何配置yum仓库 1. ...
- Spring AOP(三)--XML方式实现
本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...
- Python基础-列表、元组、字典、字符串(精简解析),全网最齐全。
一.列表 =====================================================1.列表的定义及格式: 列表是个有序的,可修改的,元素用逗号隔开,用中括号包围的序列 ...
- 2019-8-31-C#-程序集数量对软件启动性能的影响
title author date CreateTime categories C# 程序集数量对软件启动性能的影响 lindexi 2019-08-31 16:55:58 +0800 2018-10 ...
- 2018-8-17-C#-从零开始写-SharpDx-应用-控制台创建-Sharpdx-窗口
title author date CreateTime categories C# 从零开始写 SharpDx 应用 控制台创建 Sharpdx 窗口 lindexi 2018-8-17 9:3:3 ...
- 关于Git回退再前进造成本地代码和远程仓库代码不一致的问题
事情经过: git push 提交之后(版本2.0), 回退, 然后做了一些修改, 发现有问题,于是脑抽回退git reset --hard HEAD^ (版本1,0), 然后又前进到之前那个版本( ...
- 关于parseInt进行进制的转换
["1", "2", "3"].map(parseInt) 答案是多少? 考察点:1 . ES5的map方法,接收参数并且callback计 ...
- LintCode_41 最大子数组
题目 给定一个整数数组,找到一个具有最大和的子数组,返回其最大和. 注意事项 子数组最少包含一个数 样例 给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1] ...
- mysql5 msi安装版
有安装版为啥要用解压版? 搞不懂为啥大佬们都喜欢解压版? http://ftp.ntu.edu.tw/MySQL/Downloads/MySQLInstaller/mysql-installer-co ...