Apache Camel框架入门示例

24 篇文章 0 订阅
17 篇文章 0 订阅
Apache Camel是Apache基金会下的一个开源项目,它是一个基于规则路由和处理的引擎,提供企业集成模式的Java对象的实现,通过应用程序接口 或称为陈述式的Java领域特定语言(DSL)来配置路由和处理的规则。其核心的思想就是从一个from源头得到数据,通过processor处理,再发到一个to目的的.
这个from和to可以是我们在项目集成中经常碰到的类型:一个FTP文件夹中的文件,一个MQ的queue,一个HTTP request/response,一个webservice等等.
Camel可以很容易集成到standa lone的应用,在容器中运行的Web应用,以及和Spring一起集成.
下面用一个示例,介绍怎么开发一个最简单的Camel应用.
1,从http://camel.apache.org/download.html下载Jar包.在本文写作的时候最新版本是2.9. 本文用的是2.7,从2.7开始要求需要JRE1.6的环境.
下载的zip包含了Camel各种特性要用到的jar包.
在本文入门示例用到的Jar包只需要:camel-core-2.7.5.jar,commons-management-1.0.jar,slf4j-api-1.6.1.jar.
2,新建一个Eclipse工程,将上面列出的jar包设定到工程的Classpath.
新建一个如下的类:运行后完成的工作是将d:/temp/inbox/下的所有文件移到d:/temp/outbox
  1. public class FileMoveWithCamel {
  2. public static void main(String args[]) throws Exception {
  3. CamelContext context = new DefaultCamelContext();
  4. context.addRoutes(new RouteBuilder() {
  5. public void configure() {
  6. //from("file:d:/temp/inbox?noop=true").to("file:d:/temp/outbox");
  7. from("file:d:/temp/inbox/?delay=30000").to("file:d:/temp/outbox");
  8. }
  9. });
  10. context.start();
  11. boolean loop =true;
  12. while(loop){
  13. Thread.sleep(25000);
  14. }
  15. context.stop();
  16. }
  17. }
public class FileMoveWithCamel {
    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();
        context.addRoutes(new RouteBuilder() {
        public void configure() {
        //from("file:d:/temp/inbox?noop=true").to("file:d:/temp/outbox"); 
        from("file:d:/temp/inbox/?delay=30000").to("file:d:/temp/outbox");
        }
        });
        context.start();
        boolean loop =true;
        while(loop){
            Thread.sleep(25000);
        }        
        context.stop();
        }
}
上面的例子体现了一个最简单的路由功能,比如d:/temp/inbox/是某一个系统FTP到Camel所在的系统的一个接收目录.
d:/temp/outbox为Camel要发送的另一个系统的接收目录.
from/to可以是如下别的形式,读者是否可以看出Camel是可以用于系统集成中做路由,流程控制一个非常好的框架了呢?
from("file:d:/temp/inbox/?delay=30000").to("jms:queue:order");//delay=30000是每隔30秒轮询一次文件夹中是否有文件.
3,再给出一个从from到to有中间流程process处理的例子:
  1. public class FileProcessWithCamel {
  2. public static void main(String args[]) throws Exception {
  3. CamelContext context = new DefaultCamelContext();
  4. context.addRoutes(new RouteBuilder() {
  5. public void configure() {
  6. FileConvertProcessor processor = new FileConvertProcessor();
  7. from("file:d:/temp/inbox?noop=true").process(processor).to("file:d:/temp/outbox");
  8. }
  9. });
  10. context.start();
  11. boolean loop =true;
  12. while(loop){
  13. Thread.sleep(25000);
  14. }
  15. context.stop();
  16. }
  17. }
public class FileProcessWithCamel {
    public static void main(String args[]) throws Exception {
        CamelContext context = new DefaultCamelContext();    
        context.addRoutes(new RouteBuilder() {
            
        public void configure() {
        FileConvertProcessor processor = new FileConvertProcessor();
        from("file:d:/temp/inbox?noop=true").process(processor).to("file:d:/temp/outbox");
        }
        });
        
        context.start();
        boolean loop =true;
        while(loop){
            Thread.sleep(25000);
        }
        context.stop();
        }
}
这里的处理只是简单的把接收到的文件多行转成一行
  1. public class FileConvertProcessor implements Processor{
  2. @Override
  3. public void process(Exchange exchange) throws Exception {
  4. try {
  5. InputStream body = exchange.getIn().getBody(InputStream.class);
  6. BufferedReader in = new BufferedReader(new InputStreamReader(body));
  7. StringBuffer strbf = new StringBuffer("");
  8. String str = null;
  9. str = in.readLine();
  10. while (str != null) {
  11. System.out.println(str);
  12. strbf.append(str + " ");
  13. str = in.readLine();
  14. }
  15. exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");
  16. // set the output to the file
  17. exchange.getOut().setBody(strbf.toString());
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22. }
public class FileConvertProcessor implements Processor{
    @Override
    public void process(Exchange exchange) throws Exception {    
        try {
            InputStream body = exchange.getIn().getBody(InputStream.class);
            BufferedReader in = new BufferedReader(new InputStreamReader(body));
            StringBuffer strbf = new StringBuffer("");
            String str = null;
            str = in.readLine();
            while (str != null) {                
                System.out.println(str);
                strbf.append(str + " ");
                str = in.readLine();                
            }
            exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");
            // set the output to the file
            exchange.getOut().setBody(strbf.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
在Eclipse里运行的时候,Camel默认不会把log信息打印到控制台,这样出错的话,异常是看不到的,需要把 log4j配置到项目中.
  1. log4j.appender.stdout = org.apache.log4j.ConsoleAppender
  2. log4j.appender.stdout.Target = System.out
  3. log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
  4. log4j.appender.stdout.layout.ConversionPattern = %-5p %d [%t] %c: %m%n
  5. log4j.rootLogger = debug,stdout  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值