骆驼的重新交付政策

时间:2015-07-15 14:27:36

标签: java xml apache-camel

我是一条小路,我想使用自定义重新传递策略重复向端点发送消息,但行为很奇怪。它看起来像一个redeliveryPolicy只重复抛出错误。我试图将所有交易所发送到路线的开头,但随后政策不起作用原因每次都在创造:

        <route id="sampleRoute">
            <from uri="direct:anotheRoute" />
            <to uri="nmr:kaboom" />
            <choice>
                <when>
                    <xpath>//result = 'true'</xpath>
                    <to uri="direct:anotherAnotherRoute" />
                </when>
                <otherwise>
                    <throwException ref="redeliveryException" />
                </otherwise>
            </choice>
            <onException>
                <exception>java.net.SocketException</exception>
                <exception>java.net.ConnectException</exception>
                <exception>my.custom.error.RedeliveryException</exception>
                <redeliveryPolicy ref="customRedeliveryPolicy" />
                <to uri="direct:anotheRoute" />
            </onException>
        </route>

我做错了什么?我想在发生错误时间隔重复我的请求。我的骆驼版本是2.6

日志:

16:29:43,780 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:43,780 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 0 caught: my.custom.error.RedeliveryException
16:29:43,780 | DEBUG | cc5bd91-thread-1 | RedeliveryPolicy                 |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Sleeping for: 60 millis until attempting redelivery
16:29:43,840 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:43,840 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 1 caught: my.custom.error.RedeliveryException
16:29:43,840 | DEBUG | cc5bd91-thread-1 | RedeliveryPolicy                 |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Sleeping for: 60 millis until attempting redelivery
16:29:43,900 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:43,900 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 2 caught: my.custom.error.RedeliveryException
16:29:43,900 | DEBUG | cc5bd91-thread-1 | RedeliveryPolicy                 |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Sleeping for: 60 millis until attempting redelivery
16:29:43,960 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:43,960 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 3 caught: my.custom.error.RedeliveryException
16:29:43,960 | DEBUG | cc5bd91-thread-1 | RedeliveryPolicy                 |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Sleeping for: 60 millis until attempting redelivery
16:29:44,020 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:44,020 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 4 caught: my.custom.error.RedeliveryException
16:29:44,020 | DEBUG | cc5bd91-thread-1 | RedeliveryPolicy                 |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Sleeping for: 60 millis until attempting redelivery
16:29:44,080 | DEBUG | cc5bd91-thread-1 | DefaultListableBeanFactory       |  | 59 - org.springframework.beans - 3.0.5.RELEASE | Returning cached instance of singleton bean 'RedeliveryPolicy'
16:29:44,080 | DEBUG | cc5bd91-thread-1 | DefaultErrorHandler              |  | 68 - org.apache.camel.camel-core - 2.6.0.fuse-03-01 | Failed delivery for exchangeId: ID-1NVC-52527-1436958153799-314-4. On delivery attempt: 5 caught: my.custom.error.RedeliveryException
16:29:44,081 | DEBUG | cc5bd91-thread-1 | NMR                              |  | 81 - org.apache.servicemix.nmr.core - 1.4.0.fuse-03-01 | Channel org.apache.servicemix.nmr.core.ChannelImpl@7dc124e5 dispatching exchange: [

2 个答案:

答案 0 :(得分:3)

它并不奇怪,Camel的错误处理程序和重新传递从失败的步骤执行,而不是从路径的开始执行。

你可以不再将错误调用direct:anotherRoute再次抛出,唯一的问题是,如果这样做太频繁,你的调用堆栈可能会变得太深。

您可以做的是将nmr:kaboom放入单独的路线,并将其配置为noErrorHandler,然后从第一条路线调用direct:,然后错误处理程序可以从头开始重新传递整个路径。

答案 1 :(得分:1)

感谢您的解释。我会发表正确答案:

        <route id="sampleRoute">
            <from uri="direct:anotheRoute" />
            <to uri="direct:kaboom" />
            <onException>
                <exception>java.net.SocketException</exception>
                <exception>java.net.ConnectException</exception>
                <exception>my.custom.error.RedeliveryException</exception>
                <redeliveryPolicy ref="customRedeliveryPolicy" />
            </onException>
        </route>
        <route errorHandlerRef="noErrorHandler">
            <from uri="direct:kaboom />
            <to uri="nmr:kaboom/>
            <choice>
                <when>
                    <xpath>//result = 'true'</xpath>
                    <to uri="direct:anotherAnotherRoute" />
                </when>
                <otherwise>
                    <throwException ref="redeliveryException" />
                </otherwise>
            </choice>
        </route>

和bean内部的声明:

<bean id="noErrorHandler" class="org.apache.camel.builder.NoErrorHandlerBuilder"/>