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

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

Introduction

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

Prototype

public void setPropagateEvaluationFailures(boolean propagateOnSuccessEvaluationFailures) 

Source Link

Document

If true and an onSuccess expression evaluation fails with an exception, the exception will be thrown to the caller.

Usage

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

@Test
public void propagateOnSuccessExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean();
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//  www.j  a  va  2  s .  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!");

    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("1/0");
    advice.setOnFailureExpression("1/0");

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

    // failing advice with success
    handler.handleMessage(message);
    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(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());

    // propagate failing advice with success
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("/ by zero", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);

    success = successChannel.receive(1000);
    assertNotNull(success);
    assertEquals("Hello, world!", ((AdviceMessage) success).getInputMessage().getPayload());
    assertEquals(ArithmeticException.class, success.getPayload().getClass());
    assertEquals("/ by zero", ((Exception) success.getPayload()).getMessage());

}

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

@Test
public void propagateOnFailureExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean(true);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {
        @Override//from  w w  w  . ja  v  a 2  s  .c o m
        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!");

    PollableChannel successChannel = new QueueChannel();
    PollableChannel failureChannel = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setSuccessChannel(successChannel);
    advice.setFailureChannel(failureChannel);
    advice.setOnSuccessExpression("1/0");
    advice.setOnFailureExpression("1/0");

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

    // failing advice with failure
    try {
        handler.handleMessage(message);
        fail("Expected exception");
    } catch (Exception e) {
        assertEquals("qux", e.getCause().getMessage());
    }
    Message<?> reply = replies.receive(1);
    assertNull(reply);

    Message<?> failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());

    // propagate failing advice with failure; expect original exception
    advice.setPropagateEvaluationFailures(true);
    try {
        handler.handleMessage(message);
        fail("Expected Exception");
    } catch (MessageHandlingException e) {
        assertEquals("qux", e.getCause().getMessage());
    }
    reply = replies.receive(1);
    assertNull(reply);

    failure = failureChannel.receive(1000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());

}