基本框架
Apache Camel
Spring Boot
Maven
开发过程
1.新建一个POM(quickstart)项目,在POM文件中添加Camel和Spring Boot的依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.18.1</version>
</dependency>
</dependencies>
2.新建Application.java 启动类
@SpringBootApplication
public class Application{
public static void main(String[] args) {
final ApplicationContext context = new SpringApplication(Application.class).run(args);
final CamelSpringBootApplicationController controller = context.getBean(CamelSpringBootApplicationController.class);
controller.run();
}
}
3.新建RouteBuilder.java 消息路由
@Component
public class RouteBuilder extends SpringRouteBuilder {
@Override
public void configure() throws Exception {
from("file:C:/Temp/inbox").to("file:C:/Temp/outbox");
}
}
4.启动Application的main函数,C:/Temp/inbox下的文件将会被自动拷贝到C:/Temp/outbox,camel会一直监控inbox文件夹,一旦有新文件出现,就会被立即拷贝到outbox。
5.更多高级应用的参数配置
- 处理完的文件移动到C:/Temp/inbox/.done 目录中,进行备份,不指定的话,会默认备份至C:/Temp/inbox/.camel
from("file:C:/Temp/inbox?move=.done").to("file:C:/Temp/outbox");
- 删除处理完成的文件
from("file:C:/Temp/inbox?delete=true").to("file:C:/Temp/outbox");
- 处理中的文件单独放在一个文件夹,文件第一步从inbox移动到inprogress,处理完成之后,再移动到.done
from("file:C:/Temp/inbox?preMove=inprogress&move=.done").to("file:C:/Temp/outbox");
- 按日期备份处理完的文件
from("file:C:/Temp/inbox?move=backup/${date:now:yyyyMMdd}/${file:name}").to("file:C:/Temp/outbox");
- 记录处理失败的文件
moveFailed=/error/${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}