Example usage for org.springframework.integration.handler.advice RequestHandlerCircuitBreakerAdvice setThreshold

List of usage examples for org.springframework.integration.handler.advice RequestHandlerCircuitBreakerAdvice setThreshold

Introduction

In this page you can find the example usage for org.springframework.integration.handler.advice RequestHandlerCircuitBreakerAdvice setThreshold.

Prototype

public void setThreshold(int threshold) 

Source Link

Usage

From source file:org.springframework.integration.handler.advice.AdvisedMessageHandlerTests.java

@Test
public void circuitBreakerTests() throws Exception {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override// w ww  .  j ava2  s.c  o  m
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("foo");
            }
            return "bar";
        }

    };
    handler.setBeanName("baz");
    handler.setOutputChannel(new QueueChannel());
    RequestHandlerCircuitBreakerAdvice advice = new RequestHandlerCircuitBreakerAdvice();
    /*
     * Circuit breaker opens after 2 failures; allows a new attempt after 100ms and
     * immediately opens again if that attempt fails. After a successful attempt,
     * we reset the failure counter.
     */
    advice.setThreshold(2);
    advice.setHalfOpenAfter(100);

    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.afterPropertiesSet();

    doFail.set(true);
    Message<String> message = new GenericMessage<String>("Hello, world!");
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
    Thread.sleep(100);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
    Thread.sleep(100);
    doFail.set(false);
    handler.handleMessage(message);
    doFail.set(true);
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("foo", e.getCause().getMessage());
    }
    try {
        handler.handleMessage(message);
        fail("Expected failure");
    } catch (Exception e) {
        assertEquals("Circuit Breaker is Open for baz", e.getCause().getMessage());
    }
}