如何在Apache Camel中组合重试和断路器?

时间:2016-02-23 10:29:35

标签: apache-camel circuit-breaker

我想重试3次,然后将故障阈值增加到1.当此阈值达到5时,我想在Apache Camel中打开电路。

我知道Camel中有断路器支持,但是我找不到可以将重试与其结合的示例。

非常感谢任何帮助。

提前致谢。

拉​​梅什。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码

<!--  Just Throws an exception of type MyException which is a custom Exception -->
<bean id="myBean" class="com.camel.examples.MyExceptionClass" />
<camel:errorHandler id="defaultErrorHandler" type="DefaultErrorHandler">
    <camel:redeliveryPolicy maximumRedeliveries="3"
        redeliveryDelay="1000" logStackTrace="false" />
</camel:errorHandler>
<camel:camelContext>
    <camel:route>
        <camel:from uri="timer:foo?repeatCount=8&amp;period=10000" />
        <camel:setBody>
            <camel:constant>Sundar</camel:constant>
        </camel:setBody>
        <camel:loadBalance>
            <camel:circuitBreaker threshold="5" halfOpenAfter="1000">
                <camel:exception>java.lang.Exception</camel:exception>
            </camel:circuitBreaker>
            <camel:to uri="direct:a" />
        </camel:loadBalance>
    </camel:route>
    <camel:route id="myroute" errorHandlerRef="defaultErrorHandler">
        <camel:from uri="direct:a" />
        <camel:process ref="myBean"></camel:process>
        <camel:log message="${exception}]" />
    </camel:route>
</camel:camelContext>

类MyExceptionClass.java

package com.camel.examples;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyExceptionClass implements Processor{
    Logger Logger = LoggerFactory.getLogger(MyExceptionClass.class);
    @Override
    public void process(Exchange exchange) throws Exception {   
        throw new MyException("Other Exceptions");
    }
}

MyException Class

package com.camel.examples;

public class MyException extends Exception{

    public MyException() {
    super();

    }

    public MyException(String message){
        super("My Exception : "+message);
    }
}