Skip to main content

Apache Camel 框架集成Spring


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,如下
1public class FileProcessWithCamelSpring extends 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仍然和和入门示例的代码相同.
01public class FileConvertProcessor implements Processor{
02    @Override
03    public void process(Exchange exchange) throws Exception {   
04        try {
05            InputStream body = exchange.getIn().getBody(InputStream.class);
06            BufferedReader in = new BufferedReader(new InputStreamReader(body));
07            StringBuffer strbf = new StringBuffer("");
08            String str = null;
09            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 
23}
4,创建一个Spring的配置文件如下:注意要将camel的xmlns加入文件中
01xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04    xmlns:camel="http://camel.apache.org/schema/spring"
05    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
06    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
07    default-autowire="byName"  default-init-method="init">
08    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
09        <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.
01xml version="1.0" encoding="UTF-8"?>
02<beans xmlns="http://www.springframework.org/schema/beans"
03    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
04    xmlns:camel="http://camel.apache.org/schema/spring"
05    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
06    http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"
07    default-autowire="byName"  default-init-method="init">
08    <bean id="fileConverter" class="com.test.camel.FileConvertProcessor"/>
09    <camelContext id="testCamelContext" xmlns="http://camel.apache.org/schema/spring">
10        <route>
11            <from uri="file:d:/temp/inbox?delay=30000"/>
12            <process ref="fileConverter"/>
13            <to uri="file:d:/temp/outbox"/>
14        </route>
15    </camelContext>
16 
17</beans>
与第五步一样启动Spring容器,Camel会每隔30秒轮询一下看d:/temp/inbox是否有文件,有的话则进行处理.

Comments

Popular posts from this blog

Regression Testing

From  http://www.softwaretestinghelp.com/regression-testing-tools-and-methods/ The selective retesting of a  software  system that has been modified to ensure that any  bugs  have been fixed and that no other previously working functions have failed as a result of the reparations and that newly added features have not created problems with previous versions of the  software . Also referred to as  verification testing , regression testing is initiated after a  programmer  has attempted to fix a recognized problem or has added  source code  to a program that may have inadvertently introduced errors. It is a  quality  control measure to ensure that the newly modified code still complies with its specified requirements and that unmodified code has not been affected by the maintenance activity. What is Regression Software Testing? Regression means retesting the unchanged parts of the application. Test cases are re-execu...

ThreadPoolExecutor使用

From 洞玄的博客 http://dongxuan.iteye.com/blog/901689 jdk官方文档(javadoc)是学习的最好,最权威的参考。 文章分上中下。上篇中主要介绍ThreadPoolExecutor接受任务相关的两方面入参的意义和区别,池大小参数 corePoolSize和 maximumPoolSize,BlockingQueue选型( SynchronousQueue, LinkedBlockingQueue, ArrayBlockingQueue );中篇中主要聊聊与 keepAliveTime这个参数相关的话题;下片中介绍一下一些比较少用的该类的API,及他的近亲: ScheduledThreadPoolExecutor 。 如果理解错误,请直接指出。 查看JDK帮助文档,可以发现该类比较简单,继承自AbstractExecutorService,而AbstractExecutorService实现了ExecutorService接口。 ThreadPoolExecutor的完整构造方法的签名是: ThreadPoolExecutor (int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit  unit, BlockingQueue < Runnable > workQueue, ThreadFactory  threadFactory, RejectedExecutionHandler  handler)   先记着,后面慢慢解释。 ===============================神奇分割线================================== 其实对于ThreadPoolExecutor的构造函数网上有N多的解释的,大多讲得都很好,不过我想先换个方式, 从Executors这个类入手 。因为他的几个构造工厂构造方法名字取得令人很容易了解有什么特点。但是其实Executors类的底层实现便是ThreadPoolExecutor! ThreadPoolEx...

Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

Continuing the Spring 3.0 "simplification series" started by Keith and Chris , I would like to provide a quick overview of simplifications in scheduling and task execution enabled by Spring 3.0. I will be walking through a basic sample application that you can checkout from the spring-samples Subversion repository. It has been designed to be as simple as possible while showcasing both annotation-driven and XML-based approaches to scheduling tasks in Spring 3.0. Let's begin with the annotation-driven approach. You can run it directly via the main() method in AnnotationDemo. If you take a look, you'll see that it's nothing more than a bootstrap for a Spring ApplicationContext: public static void main(String[] args) {      new ClassPathXmlApplicationContext( "config.xml" , AnnotationDemo. class ); } The reason nothing else is necessary is that the ApplicationContext contains an "active" component, which we will...