Apache Camel框架集成Spring

来源:互联网 发布:mind map软件下载 编辑:程序博客网 时间:2024/06/01 09:28

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,如下

[java] view plaincopyprint?
  1. public class FileProcessWithCamelSpringextends RouteBuilder {
  2. @Override
  3. public void configure()throws Exception {
  4. FileConvertProcessor processor = new FileConvertProcessor();
  5. from("file:d:/temp/inbox?delay=30000").process(processor).to("file:d:/temp/outbox");
  6. }
  7. }

3,Processor仍然和和入门示例的代码相同.

[java] view plaincopyprint?
  1. public class FileConvertProcessorimplements 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. }

4,创建一个Spring的配置文件如下:注意要将camel的xmlns加入文件中

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:camel="http://camel.apache.org/schema/spring"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
  7. default-autowire="byName"default-init-method="init">
  8. <camelContextid="testCamelContext"xmlns="http://camel.apache.org/schema/spring">
  9. <package>com.test.camel</package>
  10. </camelContext>
  11. </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.

[html] view plaincopyprint?
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:camel="http://camel.apache.org/schema/spring"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
  7. default-autowire="byName"default-init-method="init">
  8. <beanid="fileConverter"class="com.test.camel.FileConvertProcessor"/>
  9. <camelContextid="testCamelContext"xmlns="http://camel.apache.org/schema/spring">
  10. <route>
  11. <fromuri="file:d:/temp/inbox?delay=30000"/>
  12. <processref="fileConverter"/>
  13. <touri="file:d:/temp/outbox"/>
  14. </route>
  15. </camelContext>
  16. </beans>

与第五步一样启动Spring容器,Camel会每隔30秒轮询一下看d:/temp/inbox是否有文件,有的话则进行处理.

原创粉丝点击