Example usage for org.springframework.integration.handler AbstractReplyProducingMessageHandler setBeanName

List of usage examples for org.springframework.integration.handler AbstractReplyProducingMessageHandler setBeanName

Introduction

In this page you can find the example usage for org.springframework.integration.handler AbstractReplyProducingMessageHandler setBeanName.

Prototype

@Override
    public final void setBeanName(String beanName) 

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/*www .  ja va  2  s. com*/
        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());
    }
}