Camel框架的使用


Camel流程框架是Apache下的一个开源项目,是较为成熟的流程框架。在web项目中也可以无缝地集成于Spring当中。

一、简单使用

引入camel相关的jar包:camel-core-2.10.4.jar。

1、经典的入门示例——文件移动

public class FileMoveWithCamel {
    public static void main(String[] args) {
        try{
            CamelContext camelCtx = new DefaultCamelContext();
            camelCtx.addRoutes(new RouteBuilder() {
                
                //此示例中,只能转移文件,而无法转移目录
                @Override
                public void configure() throws Exception {
                    from("file:f:/tmp/inbox?delay=30000").to("file:f:/tmp/outbox");
                }
                
            });
            
            camelCtx.start();
            boolean loop = true;
            while(loop) {
                Thread.sleep(25000);
            }
            System.out.println("循环完毕");
            camelCtx.stop();
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }

其中file:类似于http://,是camel的协议组件。camel支持的组件还包括:bean browse dataset direct file log mock properties seda test timer stub validator vm xlst等。其中常用的有bean和direct以及file

运行上述程序,会发现file:f:/tmp/inbox下的文件被转移到file:f:/tmp/outbox了,并且在file:f:/tmp/inbox会生成一个.camel文件夹存放刚才被转移的文件。

2、入门示例二——带Processor处理

先定义一个处理器,实现org.apache.camel.Processor接口

public class FileConvertProcessor implements Processor {
 
    @Override
    public void process(Exchange exchange) throws Exception {
//        Object obj = exchange.getIn().getBody(); //如果是getBody()则返回一个Object
        //如果是getBody(Class)则返回T类型的实例
        InputStream body = exchange.getIn().getBody(InputStream.class);
//        System.out.println("进入:" + body);
        BufferedReader br = new BufferedReader(new InputStreamReader(body, "UTF-8"));
        
        StringBuilder sb = new StringBuilder("");
        String str;
        while((str = br.readLine()) != null) {
            System.out.println(str);
            sb.append(str + " ");
        }
        exchange.getOut().setHeader(Exchange.FILE_NAME, "converted.txt");
        
        exchange.getOut().setBody(sb.toString());
        System.out.println("body:" + exchange.getOut().getBody());
        
    }


加上处理器后处理文件的程序

public class FileProcessWithCamel {
    public static void main(String[] args) {
        try{
            CamelContext camelCtx = new DefaultCamelContext();
            camelCtx.addRoutes(new RouteBuilder() {
                
                @Override
                public void configure() throws Exception {
                    FileConvertProcessor processor = new FileConvertProcessor();
                    //noop表示等待、无操作
                    from("file:f:/tmp/inbox?noop=true").process(processor).to("file:f:/tmp/outbox");
                }
            });
            
            camelCtx.start();
            boolean loop = true;
//死循环表示挂起camel上下文,以便持续监听
            while(loop) {
                Thread.sleep(25000);
            }
            camelCtx.stop();
        } catch(Exception ex) {
            ex.printStackTrace();}
    }
}


运行程序后将用一行打印file:f:/tmp/inbox下的文件的多行内容,并在file:f:/tmp/outbox下生成名为converted.txt的文件。该文件的内容即为file:f:/tmp/inbox下的文件的多行内容显示成的一行。

这里要特别注意getIn(setIn),getOut(setOut)怎么用:先看下面一张图

这张图表明了:假设有流程A->B->C->D->……则A在处理完毕之后给B的话,A必须setOut结果,然后B要取流程中的“上一个”节点(即A)的结果则必须getIn取结果再处理,以此类推……A不能setIn结果,否则B getIn的话会取不到A set的结果。

二、集成在Spring当中

需引入camel-core-2.10.4.jar camel-spring-2.10.4.jar

对已第一部分的第一示例,若在Spring中配置,并设置它随着web项目的启动而启动,则可以这样写:

<?xml version="1.0" encoding="UTF-8"?>

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:drools="http://drools.org/schema/drools-spring"
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://drools.org/schema/drools-spring http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-container/drools-spring/src/main/resources/org/drools/container/spring/drools-spring-1.0.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    
    class="com.xxxx.FileConvertProcessor" />
    
         
          
              
              
             
         
    

除此之外,其实更为常见的是一个处理流程往往需要经过很多个bean类。而查看camel direct组件的用法:

In the route below we use the direct component to link the two routes together:

可知bean与bean之间的流程连接用的是direct,这里不妨用fileConverter和fileConverter2两个Processor测试(它们的具体定义省略,都得实现Processor接口),于是:

        class="com.xxx.FileConvertProcessor" />
    class="com.xxx.FileConvertProcessor2" />