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,如下
- public class FileProcessWithCamelSpringextends 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");
- }
- }
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 FileConvertProcessorimplements 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();
- }
- }
- }
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加入文件中
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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">
- <camelContextid="testCamelContext"xmlns="http://camel.apache.org/schema/spring">
- <package>com.test.camel</package>
- </camelContext>
- </beans>
<?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.
- <?xmlversion="1.0"encoding="UTF-8"?>
- <beansxmlns="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">
- <beanid="fileConverter"class="com.test.camel.FileConvertProcessor"/>
- <camelContextid="testCamelContext"xmlns="http://camel.apache.org/schema/spring">
- <route>
- <fromuri="file:d:/temp/inbox?delay=30000"/>
- <processref="fileConverter"/>
- <touri="file:d:/temp/outbox"/>
- </route>
- </camelContext>
- </beans>
<?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是否有文件,有的话则进行处理.
- Apache Camel框架集成Spring
- Apache Camel框架集成Spring
- Apache Camel框架集成Spring
- Apache Camel框架集成Spring
- Apache Camel框架集成Spring
- Spring集成 Apache Camel: AWS-SQS
- Spring集成Camel
- camel-file 集成spring使用
- Apache Camel框架入门示例
- Apache Camel框架入门示例
- Apache Camel框架入门示例
- Apache Camel框架入门示例
- Apache Camel框架入门示例
- Apache Camel框架入门示例
- 基于Apache Camel的EIP集成
- Spring集成Apache Shiro安全框架
- Apache Camel框架之FTP路由
- Apache Camel框架之JMS路由
- Super Jumper:一个2DOpenGL ES游戏
- 2013.4.14视频整理
- vmware下安装64 位solaris10
- TimeSpan
- prob7
- Apache Camel框架集成Spring
- wince 1.窗体不显示在任务栏 2.最小化窗体
- Apache Camel框架之FTP路由
- hdu 2955 01 背包小变形
- Ubuntu 回收站 位置(处理回收站不能打开问题)
- 成员变量的简单例题
- Apache Camel框架之JMS路由
- ubuntu12.04 下安装 Y480 网卡驱动
- 寻找发帖水王