camel Direct and import the routes from another XML file
接着前面前面的camel web 章程,本章程主要介绍 Direct,seda,和 routes的xml导入(import)。 建议在阅读该文时参阅笔者前两篇博客 apache camel jetty to http using Camel in a Web Application 先来简单的,direct (点击进入官网)和 how do import routes from other xml file (点击进入官网) 比如我们需要大量的 xml配置route,但是同时出现在applicationContext文件中有非常庞大,臃肿,多人维护有容易冲突。那么解决该问题的一个办法就是根据业务,或者其他类别,把各个route配置到别的xml文件,在applicationContext中import,然后使用。(如果你使用的不是 xml 配置路由,选择通过代码编码route,camel 有一个org.apache.camel.spring.config.scan.route的包可以自动扫描代码中 继承RouteBuilder 自动加载路由,不过个人觉得那样路由规则都在代码中编码,如路由还需要重新编译极为不便,所以我选择 xml配置的方式) routes import 十分简单,笔者就直接贴官网原文 For example we could have a file named
myCoolRoutes.xml
Then in your XML file which contains the CamelContext you can use Spring to import the
Also notice that you can mix and match,having routes inside CamelContext and also externalized in RouteContext. You can have as many Reusable routes The routes defined in 官方介绍 component provides direct,synchronous invocation of any consumers when a producer sends a message exchange. 我们就理解最后一句 ,endpoint(当前配置的route) 可以连接(调用)camel上已经配置好的routes。 有种组合拳的感觉。不过既然可以调用已经存在的,那再配置的时候就要避免 相互调用,route1 to:/direct:route2 . route2 to:/diect:route1.(这是一个错误的示范) 理解了上面这点。那么在使用的时候就会顺手很多 我就直接贴入我在实践是用的 camelRoute1.xml(other route) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd "> <!--<bean id="myReviveProcess" class="com.smart.fulfilcamel.reviveProcess"/>--> <bean id="coolReviveProcess" class="com.smart.fulfilcamel.reviveProcess"/> <!-- this is an included XML file where we only the the routeContext --> <routeContext id="myCoolRoutes" xmlns="http://camel.apache.org/schema/spring"> <!-- we can have a route --> <route id="cool"> <from uri="jetty:http://localhost:8777/FulfilCamel/cool?sessionSupport=true"/> <to uri="direct:bar"/> </route> <!-- and another route,you can have as many your like --> <route id="bar"> <from uri="direct:bar"/> <removeHeaders pattern="CamelHttp*"/> <process ref="coolReviveProcess"/> <to uri="http://localhost:8007/FulfilCamel/fulfil"/> </route> </routeContext> </beans>我在 applicationContext中import 上面 camelRote1.xml <?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd"> <bean id="myReviveProcess" class="com.smart.fulfilcamel.reviveProcess"/> <!-- import the routes from another XML file --> <import resource="camelRoute1.xml"/> <camelContext id="testOne" xmlns="http://camel.apache.org/schema/spring"> <!-- refer to a given route to be used --> <routeContextRef ref="myCoolRoutes"/> <route> <from uri="jetty:http://localhost:8777/FulfilCamel/revive?sessionSupport=true"/> <removeHeaders pattern="CamelHttp*" /> <process ref="myReviveProcess"/> <to uri="http://localhost:8007/FulfilCamel/fulfil"/> </route> </camelContext> </beans> enjoy。 上面哪里有什么不明白的,请参阅笔者前两篇博客 using Camel in a Web Application 下面 direct 的options,
后续文件 披露 seda (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |