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

myCoolRoutes.xml
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 themyCoolRoute.xmlfile.
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 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

allowMultipleConsumers

true

@deprecatedIf set tofalse,then when a second consumer is started on the endpoint,anIllegalStateExceptionis 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇