Search in sources :

Example 1 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class DelayHandler method determineDelayForMessage.

private long determineDelayForMessage(Message<?> message) {
    DelayedMessageWrapper delayedMessageWrapper = null;
    if (message.getPayload() instanceof DelayedMessageWrapper) {
        delayedMessageWrapper = (DelayedMessageWrapper) message.getPayload();
    }
    long delay = this.defaultDelay;
    if (this.delayExpression != null) {
        Exception delayValueException = null;
        Object delayValue = null;
        try {
            delayValue = this.delayExpression.getValue(this.evaluationContext, delayedMessageWrapper != null ? delayedMessageWrapper.getOriginal() : message);
        } catch (EvaluationException e) {
            delayValueException = e;
        }
        if (delayValue instanceof Date) {
            long current = delayedMessageWrapper != null ? delayedMessageWrapper.getRequestDate() : System.currentTimeMillis();
            delay = ((Date) delayValue).getTime() - current;
        } else if (delayValue != null) {
            try {
                delay = Long.valueOf(delayValue.toString());
            } catch (NumberFormatException e) {
                delayValueException = e;
            }
        }
        if (delayValueException != null) {
            if (this.ignoreExpressionFailures) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to get delay value from 'delayExpression': " + delayValueException.getMessage() + ". Will fall back to default delay: " + this.defaultDelay);
                }
            } else {
                throw new MessageHandlingException(message, "Error occurred during 'delay' value determination", delayValueException);
            }
        }
    }
    return delay;
}
Also used : EvaluationException(org.springframework.expression.EvaluationException) MessagingException(org.springframework.messaging.MessagingException) EvaluationException(org.springframework.expression.EvaluationException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Date(java.util.Date) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 2 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class ContentEnricher method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    final Object requestPayload = requestMessage.getPayload();
    final Object targetPayload;
    if (requestPayload instanceof Cloneable && this.shouldClonePayload) {
        try {
            Method cloneMethod = requestPayload.getClass().getMethod("clone");
            targetPayload = ReflectionUtils.invokeMethod(cloneMethod, requestPayload);
        } catch (Exception e) {
            throw new MessageHandlingException(requestMessage, "Failed to clone payload object", e);
        }
    } else {
        targetPayload = requestPayload;
    }
    final Message<?> actualRequestMessage;
    if (this.requestPayloadExpression == null) {
        actualRequestMessage = requestMessage;
    } else {
        final Object requestMessagePayload = this.requestPayloadExpression.getValue(this.sourceEvaluationContext, requestMessage);
        actualRequestMessage = this.getMessageBuilderFactory().withPayload(requestMessagePayload).copyHeaders(requestMessage.getHeaders()).build();
    }
    final Message<?> replyMessage;
    if (this.gateway == null) {
        replyMessage = actualRequestMessage;
    } else {
        replyMessage = this.gateway.sendAndReceiveMessage(actualRequestMessage);
        if (replyMessage == null) {
            if (this.nullResultPropertyExpressions.isEmpty() && this.nullResultHeaderExpressions.isEmpty()) {
                return null;
            }
            for (Map.Entry<Expression, Expression> entry : this.nullResultPropertyExpressions.entrySet()) {
                Expression propertyExpression = entry.getKey();
                Expression valueExpression = entry.getValue();
                Object value = valueExpression.getValue(this.sourceEvaluationContext, requestMessage);
                propertyExpression.setValue(this.targetEvaluationContext, targetPayload, value);
            }
            if (this.nullResultHeaderExpressions.isEmpty()) {
                return targetPayload;
            } else {
                Map<String, Object> targetHeaders = new HashMap<String, Object>(this.nullResultHeaderExpressions.size());
                for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.nullResultHeaderExpressions.entrySet()) {
                    String header = entry.getKey();
                    HeaderValueMessageProcessor<?> valueProcessor = entry.getValue();
                    Boolean overwrite = valueProcessor.isOverwrite();
                    overwrite = overwrite != null ? overwrite : true;
                    if (overwrite || !requestMessage.getHeaders().containsKey(header)) {
                        Object value = valueProcessor.processMessage(requestMessage);
                        targetHeaders.put(header, value);
                    }
                }
                return this.getMessageBuilderFactory().withPayload(targetPayload).copyHeaders(targetHeaders).build();
            }
        }
    }
    for (Map.Entry<Expression, Expression> entry : this.propertyExpressions.entrySet()) {
        Expression propertyExpression = entry.getKey();
        Expression valueExpression = entry.getValue();
        Object value = valueExpression.getValue(this.sourceEvaluationContext, replyMessage);
        propertyExpression.setValue(this.targetEvaluationContext, targetPayload, value);
    }
    if (this.headerExpressions.isEmpty()) {
        return targetPayload;
    } else {
        Map<String, Object> targetHeaders = new HashMap<String, Object>(this.headerExpressions.size());
        for (Map.Entry<String, HeaderValueMessageProcessor<?>> entry : this.headerExpressions.entrySet()) {
            String header = entry.getKey();
            HeaderValueMessageProcessor<?> valueProcessor = entry.getValue();
            Boolean overwrite = valueProcessor.isOverwrite();
            overwrite = overwrite != null ? overwrite : true;
            if (overwrite || !requestMessage.getHeaders().containsKey(header)) {
                Object value = valueProcessor.processMessage(replyMessage);
                targetHeaders.put(header, value);
            }
        }
        return getMessageBuilderFactory().withPayload(targetPayload).copyHeaders(targetHeaders);
    }
}
Also used : HashMap(java.util.HashMap) HeaderValueMessageProcessor(org.springframework.integration.transformer.support.HeaderValueMessageProcessor) Method(java.lang.reflect.Method) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Expression(org.springframework.expression.Expression) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class ErrorMessageExceptionTypeRouterTests method noMatchAndNoDefaultChannel.

@Test
public void noMatchAndNoDefaultChannel() {
    Message<?> failedMessage = new GenericMessage<String>("foo");
    IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
    RuntimeException middleCause = new RuntimeException(rootCause);
    MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
    ErrorMessage message = new ErrorMessage(error);
    ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
    router.setBeanFactory(beanFactory);
    router.setApplicationContext(TestUtils.createTestApplicationContext());
    router.setChannelMapping(MessageDeliveryException.class.getName(), "messageDeliveryExceptionChannel");
    router.setResolutionRequired(true);
    router.setBeanName("fooRouter");
    try {
        router.handleMessage(message);
        fail("MessageDeliveryException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), containsString("'fooRouter'"));
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 4 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class ErrorMessageExceptionTypeRouterTests method intermediateCauseHasNoMappingButMostSpecificCauseDoes.

@Test
public void intermediateCauseHasNoMappingButMostSpecificCauseDoes() {
    Message<?> failedMessage = new GenericMessage<String>("foo");
    IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
    RuntimeException middleCause = new RuntimeException(rootCause);
    MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
    ErrorMessage message = new ErrorMessage(error);
    ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
    router.setBeanFactory(beanFactory);
    router.setApplicationContext(TestUtils.createTestApplicationContext());
    router.setChannelMapping(IllegalArgumentException.class.getName(), "illegalArgumentChannel");
    router.setChannelMapping(MessageHandlingException.class.getName(), "messageHandlingExceptionChannel");
    router.setDefaultOutputChannel(defaultChannel);
    router.handleMessage(message);
    assertNotNull(illegalArgumentChannel.receive(1000));
    assertNull(defaultChannel.receive(0));
    assertNull(runtimeExceptionChannel.receive(0));
    assertNull(messageHandlingExceptionChannel.receive(0));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 5 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-integration by spring-projects.

the class AdvisedMessageHandlerTests method propagateOnFailureExpressionFailures.

@Test
public void propagateOnFailureExpressionFailures() {
    final AtomicBoolean doFail = new AtomicBoolean(true);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        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.setOnSuccessExpressionString("1/0");
    advice.setOnFailureExpressionString("1/0");
    List<Advice> adviceChain = new ArrayList<Advice>();
    adviceChain.add(advice);
    handler.setAdviceChain(adviceChain);
    handler.setBeanFactory(mock(BeanFactory.class));
    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(10000);
    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(10000);
    assertNotNull(failure);
    assertEquals("Hello, world!", ((MessagingException) failure.getPayload()).getFailedMessage().getPayload());
    assertEquals(MessageHandlingExpressionEvaluatingAdviceException.class, failure.getPayload().getClass());
    assertEquals("qux", ((Exception) failure.getPayload()).getCause().getMessage());
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingExpressionEvaluatingAdviceException(org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice.MessageHandlingExpressionEvaluatingAdviceException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GenericMessage(org.springframework.messaging.support.GenericMessage) PollableChannel(org.springframework.messaging.PollableChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) Advice(org.aopalliance.aop.Advice) Test(org.junit.Test)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)66 Test (org.junit.Test)34 ErrorMessage (org.springframework.messaging.support.ErrorMessage)14 IOException (java.io.IOException)12 GenericMessage (org.springframework.messaging.support.GenericMessage)12 Message (org.springframework.messaging.Message)10 MessagingException (org.springframework.messaging.MessagingException)10 File (java.io.File)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 MessageRejectedException (org.springframework.integration.MessageRejectedException)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)3