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

How to install JSVC on Linux WidenHome Log | WidenHome Log

How to install JSVC on Linux WidenHome Log | WidenHome Log In our team, we have a lot of Java standalone applications which should be run as daemon on Unix/Linux system, and we found JSVC is the best choice for us to wrap Java programs to daemons. This article records the steps on how to install JSVC executable on Linux, which is our stage/prod environment. Download JSVC source package First of all, we need to download JSVC source package from this URL: http://commons.apache.org/daemon/download_daemon.cgi , for example, I downloaded commons-daemon-1.0.5-src.tar.gz file. Or, download it via wget: wget -c http://apache.etoak.com/commons/daemon/source/commons-daemon-1.0.5-src.tar.gz Build JSVC executable Unzip the source package and build JSVC executable. chmod 755 commons-daemon-1.0.5-src.tar.gz tar zxvf commons-daemon-1.0.5-src.tar.gz cd commons-daemon-1.0.5-src/src/native/unix Before building the JSVC executable, please make sure you have set JAVA_HOME variable correctly. And make sur...

Java中的Serializable浅谈

from  http://www.cnblogs.com/vicenteforever/articles/1471775.html 对象的串行化(Serialization) 一、串行化的概念和目的 1.什么是串行化             对象的寿命通常随着生成该对象的程序的终止而终止。有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复。我们把对象的这种能记录自己的状态以便将来再生的能力。叫作对象的持续性(persistence)。对象通过写出描述自己状态的数值来记录自己 ,这个过程叫对象的串行化(Serialization) 。串行化的主要任务是写出对象实例变量的数值。如果交量是另一对象的引用,则引用的对象也要串行化。这个过程是递归的,串行化可能要涉及一个复杂树结构的单行化,包括原有对象、对象的对象、对象的对象的对象等等。对象所有权的层次结构称为图表(graph)。 2.串行化的目的             Java对象的单行化的目标是为Java的运行环境提供一组特性,如下所示: 1)       尽量保持对象串行化的简单扼要 ,但要提供一种途径使其可根据开发者的要求进行扩展或定制。 2)       串行化机制应严格遵守Java的对象模型 。对象的串行化状态中应该存有所有的关于种类的安全特性的信息。 3)       对象的串行化机制应支持Java的对象持续性。 4)       对象的串行化机制应有足够的 可扩展能力以支持对象的远程方法调用(RMI)。 5)       对象串行化应允许对象定义自身 的格式即其自身的数据流表示形式,可外部化接口来完成这项功能。 什么情况下需要序列化 a)当...

Log4j Configuration

First, include  Log4j   jar file in your project (e.g. log4j-1.2.8.jar) From  http://www.javabeat.net/tips/82-baisc-steps-to-configure-log4j-using-xml-and.html Configure Log4j This example demonstrated how to configure  Log4j  setup using the Proerties file and  XML file . These are the two most widely used techniques for configuring the  Log4j  for your application. But, in the recent days configuring with  properties files  are considered to be old technique and recommended using  XML . This example program uses simple satndalone java program for running the example. But, in most of the  project  scenarios it will be used in the web application. However the configuration file will be the same. log4j.properties # Set root logger level to DEBUG and its only appender to Appender1. log4j.rootLogger=INFO, Appender1,Appender2 # Appender1 is set to be a ConsoleAppender. log4j.appender.Appender1=org.apache...