【问题标题】:How to combine retry and circuit breaker in Apache Camel?如何在 Apache Camel 中结合重试和断路器?
【发布时间】:2016-06-05 03:17:53
【问题描述】:

我想重试3次,然后将失败阈值增加到1。当这个阈值达到5时,我想在Apache Camel中打开电路。

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

非常感谢任何帮助。

提前致谢。

拉梅什。

【问题讨论】:

    标签: apache-camel circuit-breaker


    【解决方案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 类

    package com.camel.examples;
    
    public class MyException extends Exception{
    
        public MyException() {
        super();
    
        }
    
        public MyException(String message){
            super("My Exception : "+message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-01-07
      • 1970-01-01
      • 2017-07-05
      • 2017-12-26
      • 1970-01-01
      • 2023-03-08
      相关资源
      最近更新 更多