接着前面前面的camel web 章程,本章程主要介绍 Direct,seda,和 routes的xml导入(import)。
建议在阅读该文时参阅笔者前两篇博客
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 namedmyCoolRoutes.xml
which contains a couple of routes as shown:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
">
<!-- this is an included XML file where we only the the routeContext -->
<!-- we can have a route -->
<
route
id="cool">
<
from
uri="direct:start"/>
<
to
uri="mock:result"/>
</
route
>
<!-- and another route,you can have as many your like -->
<
route
id="bar">
<
from
uri="direct:bar"/>
<
to
uri="mock:bar"/>
</
route
>
</
routeContext
>
</
beans
>
|
Then in your XML file which contains the CamelContext you can use Spring to import themyCoolRoute.xml
file.
And then inside<camelContext/>
you can refer to the<routeContext/>
by its id as shown below:
<!-- import the routes from another XML file -->
<
import
resource="myCoolRoutes.xml"/>
<!-- refer to a given route to be used -->
<
routeContextRef
ref="myCoolRoutes"/>
<!-- we can of course still use routes inside camelContext -->
<
route
id="inside">
<
from
uri="direct:inside"/>
<
to
uri="mock:inside"/>
</
route
>
</
camelContext
>
|
Also notice that you can mix and match,having routes inside CamelContext and also externalized in RouteContext.
You can have as many<routeContextRef/>
as you like.
Reusable routes
官方介绍
component provides direct,synchronous invocation of any consumers when a producer sends a message exchange.
This endpoint can be used to connect existing routes in thesamecamel context.
我们就理解最后一句 ,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,
Name |
Default Value | Description | ||||||
---|---|---|---|---|---|---|---|---|
|
|
@deprecatedIf set to |
||||||
|
|
Camel 2.11.1:If sending a message to a direct endpoint which has no active consumer,then we can tell the producer to block and wait for the consumer to become active. |
|
|
Camel 2.11.1:The timeout value to use if block is enabled. |
|
true |
Camel 2.16.0: Indicates whether the producer should fail by throwing an exception when sending to a DIRECT endpoint with no active consumers. |
后续文件 披露 seda