1. 程式人生 > >Apache Camel框架整合Spring

Apache Camel框架整合Spring

Apache Camel提供了和Spring的整合,通過Spring容器(ApplicationContext)來管理Camel的CamelContext,這樣的話,就不需要寫程式碼來控制CamelContext的初始化,啟動和停止了.Camel會隨著Spring的啟動而啟動起來.

本文將Apache Camel框架入門示例中的例子整合到Spring中,下面簡單介紹一下整合的基本步驟.

1,新建一個Eclipse工程,將Spring3的jar包,和Camel的jar包配置到工程的classpath.

2,Route類要繼承RouteBuilder,如下

public class FileProcessWithCamelSpring extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        FileConvertProcessor processor = new FileConvertProcessor(); 
        from("file:d:/temp/inbox?delay=30000").process(processor).to("file:d:/temp/outbox");        
    }
}

3,Processor仍然和和入門示例的程式碼相同.
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();
        }
    }
}

4,建立一個Spring的配置檔案如下:注意要將camel的xmlns加入檔案中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
    default-autowire="byName"  default-init-method="init">
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <package>com.test.camel</package>
    </camelContext>    
</beans>

5,啟動Spring容器,Camel會自動啟動,不用像入門示例那樣CamelContext context = new DefaultCamelContext(), context.addRoutes(..); context.start();

        ApplicationContext ac = new ClassPathXmlApplicationContext("config/cameltest.xml");
        while (true) {
            Thread.sleep(2000);
        }

可見,Camel可以很容易的和Spring整合.

Camel還提供了"Spring DSL"來在XML中配置Route規則,不需要用JAVA類(如上面的FileProcessWithCamelSpring )來實現route.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:camel="http://camel.apache.org/schema/spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
    default-autowire="byName"  default-init-method="init">
    <bean id="fileConverter" class="com.test.camel.FileConvertProcessor"/>
    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
        <route>
            <from uri="file:d:/temp/inbox?delay=30000"/>
            <process ref="fileConverter"/>
            <to uri="file:d:/temp/outbox"/>
        </route>
    </camelContext>
</beans>

與第五步一樣啟動Spring容器,Camel會每隔30秒輪詢一下看d:/temp/inbox是否有檔案,有的話則進行處理.