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 which contains a couple of routes as shown:

myCoolRoutes.xml
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
     ">
 
     <!-- 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="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 the myCoolRoute.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

The routes defined in <routeContext/> can be reused by multiple <camelContext/>. However its only the definition which is reused. At runtime each CamelContext will create its own instance of the route based on the definition.

下面介绍Direct

官方介绍

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 the same camel 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。

上面哪里有什么不明白的,请参阅笔者前两篇博客

apache camel jetty to http

using Camel in a Web Application

下面 direct 的options,

Name

Default Value

Description

 

allowMultipleConsumers

true

@deprecated If set to false, then when a second consumer is started on the endpoint, an IllegalStateException is thrown. Will be removed in Camel 2.1: Direct endpoint does not support multiple consumers.

 

block

false

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.

 

timeout

30000

Camel 2.11.1: The timeout value to use if block is enabled.

 

failIfNoConsumers

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


转载于:https://www.cnblogs.com/mendeliang/p/5070833.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值