Example usage for org.springframework.integration.handler.advice ExpressionEvaluatingRequestHandlerAdvice setTrapException

List of usage examples for org.springframework.integration.handler.advice ExpressionEvaluatingRequestHandlerAdvice setTrapException

Introduction

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

Prototype

public void setTrapException(boolean trapException) 

Source Link

Document

If true, any exception will be caught and null returned.

Usage

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

@Test
public void successFailureAdvice() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override/*  ww  w .  java2s  .c om*/
        protected Object handleRequestMessage(Message<?> requestMessage) {
            if (doFail.get()) {
                throw new RuntimeException("qux");
            }
            return "baz";
        }
    };
    QueueChannel replies = new QueueChannel();
    handler.setOutputChannel(replies);
    Message<String> message = new GenericMessage<String>("Hello, world!");

    // no advice
    handler.handleMessage(message);
    Message<?> reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());

    PollableChannel successChannel = new QueueChannel();
    PollableChannel failureChannel = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setBeanFactory(mock(BeanFactory.class));
    advice.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpression("'foo'");
    advice.setOnFailureExpression("'bar:' + #exception.message");

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

    // advice with success
    handler.handleMessage(message);
    reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("baz", reply.getPayload());

    Message<?> success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload());
    assertEquals("foo", success.getPayload());

    // advice with failure, not trapped
    doFail.set(true);
    try {
        handler.handleMessage(message);
        fail("Expected exception");
    } catch (Exception e) {
        assertEquals("qux", e.getCause().getMessage());
    }

    Message<?> failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux",
            ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());

    // advice with failure, trapped
    advice.setTrapException(true);
    handler.handleMessage(message);
    failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux",
            ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());
    assertNull(replies.receive(1));

    // advice with failure, eval is result
    advice.setReturnFailureExpressionResult(true);
    handler.handleMessage(message);
    failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals("bar:qux",
            ((MessageHandlingExpressionEvaluatingAdviceException) failure.getPayload()).getEvaluationResult());

    reply = replies.receive(1000);
    assertNotNull(reply);
    assertEquals("bar:qux", reply.getPayload());

}