Apache Camel 框架集成Spring

fmms 12年前發布 | 46K 次閱讀 Apache Camel

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

本文將Apache Camel框架入門示例(http://blog.csdn.net/kkdelta/article/details/7231640)中的例子集成到Spring中,下面簡單介紹一下集成的基本步驟.

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

2,Route類要繼承RouteBuilde,如下

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(); } }

}</pre>

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&quot;
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot;
    xmlns:camel="http://camel.apache.org/schema/spring&quot;
    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&quot;
    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"&gt;
        <route>
            <from uri="file:d:/temp/inbox?delay=30000"/>
            <process ref="fileConverter"/>
            <to uri="file:d:/temp/outbox"/>
        </route>
    </camelContext>

</beans></pre>

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

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!