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集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...
随机推荐
- 原生微信小程序的生命周期
小程序的生命周期函数:onLaunch:function(){当启动小程序时触发小程序只会启动1次,一般为初次打开时一般只会触发一次},onShow:function(){当小程序从后台切入到前台时触 ...
- 2019-2019-2-20175332-实验三《敏捷开发与XP实践》实验报告
一.编码标准 题目要求: 在IDEA中使用工具(Code->Reformate Code)把下面代码重新格式化,再研究一下Code菜单,找出一项让自己感觉最好用的功能. 实验步骤 1.安装ali ...
- MyBatis配置文件(八)--databaseIdProvider数据库厂商标识
databaseIdProvider元素主要是为了支持不同厂商的数据库,比如有时候我们在公司内部开发使用的数据库都是PG(Postgresql),但是客户要求使用MySql,那就麻烦了是吧?其实在my ...
- PAT甲级——A1024 Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- Linux下ps -ef和ps aux的区别及格式详解-转
原文:https://www.linuxidc.com/Linux/2016-07/133515.htm Linux下显示系统进程的命令ps,最常用的有ps -ef 和ps aux.这两个到底有什么区 ...
- Google 和 微软,就不是一个重量级的
看微软的 WinCE 模拟器,做得带模带样 Google 也不是新来的,但是 Android 模拟器,配置一下都那么费事 这就是差距,Google 比 微软 差了好几级
- 2019-10-16-WPF-控件-Content-的内容不显示下划线字符串
title author date CreateTime categories WPF 控件 Content 的内容不显示下划线字符串 lindexi 2019-10-16 09:21:32 +080 ...
- ssh 连接vps
默认端口22 ssh root@194.10.10.10 特定端口xxx 上面命令后面加上 "-p xxx" 就可以了
- Http中GET和POST请求的区别
https://mp.weixin.qq.com/s/_oLnkDQn3TE7XdFWCT5Img GET请求 GET /books/?sex=man&name=Professional HT ...
- windows 下nginx配置ssl https支持
本文适合正式上线的配置,购买来的证书 私钥*.key文件需要先去掉密码 openssl rsa -in old.key -out new.key