Search in sources :

Example 31 with ErrorMessage

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

the class DatatypeChannelTests method subclassOfAcceptedType.

@Test
public void subclassOfAcceptedType() {
    MessageChannel channel = createChannel(RuntimeException.class);
    assertTrue(channel.send(new ErrorMessage(new MessagingException("test"))));
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 32 with ErrorMessage

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

the class HeaderChannelRegistryTests method testReplaceError.

/**
 * MessagingTemplate sets the errorChannel to the replyChannel so it gets any async
 * exceptions via the default {@link MessagePublishingErrorHandler}.
 */
@Test
public void testReplaceError() {
    MessagingTemplate template = new MessagingTemplate();
    template.setDefaultDestination(this.inputPolled);
    Message<?> reply = template.sendAndReceive(new GenericMessage<String>("bar"));
    assertNotNull(reply);
    assertTrue(reply instanceof ErrorMessage);
    assertNotNull(((ErrorMessage) reply).getOriginalMessage());
    assertThat(reply.getPayload(), not(instanceOf(MessagingExceptionWrapper.class)));
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 33 with ErrorMessage

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

the class MessageHistory method write.

@SuppressWarnings("unchecked")
public static <T> Message<T> write(Message<T> message, NamedComponent component, MessageBuilderFactory messageBuilderFactory) {
    Assert.notNull(message, "Message must not be null");
    Assert.notNull(component, "Component must not be null");
    Properties metadata = extractMetadata(component);
    if (!metadata.isEmpty()) {
        MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
        List<Properties> components = (previousHistory != null) ? new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
        components.add(metadata);
        MessageHistory history = new MessageHistory(components);
        if (message instanceof MutableMessage) {
            message.getHeaders().put(HEADER_NAME, history);
        } else if (message instanceof ErrorMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            Throwable payload = ((ErrorMessage) message).getPayload();
            ErrorMessage errorMessage = new ErrorMessage(payload, headerAccessor.toMessageHeaders());
            message = (Message<T>) errorMessage;
        } else if (message instanceof AdviceMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            message = new AdviceMessage<T>(message.getPayload(), headerAccessor.toMessageHeaders(), ((AdviceMessage<?>) message).getInputMessage());
        } else {
            if (!(message instanceof GenericMessage) && (messageBuilderFactory instanceof DefaultMessageBuilderFactory || messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
                if (logger.isWarnEnabled()) {
                    logger.warn("MessageHistory rebuilds the message and produces the result of the [" + messageBuilderFactory + "], not an instance of the provided type [" + message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " + "to retain custom messages during MessageHistory tracking.");
                }
            }
            message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build();
        }
    }
    return message;
}
Also used : AdviceMessage(org.springframework.integration.message.AdviceMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Properties(java.util.Properties) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

Example 34 with ErrorMessage

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

the class ExpressionEvaluatingRequestHandlerAdvice method evaluateFailureExpression.

private Object evaluateFailureExpression(Message<?> message, Exception exception) throws Exception {
    Object evalResult;
    try {
        evalResult = this.onFailureExpression.getValue(this.prepareEvaluationContextToUse(exception), message);
    } catch (Exception e) {
        evalResult = e;
        logger.error("Failure expression evaluation failed for " + message + ": " + e.getMessage());
    }
    if (this.failureChannel == null && this.failureChannelName != null && getChannelResolver() != null) {
        this.failureChannel = getChannelResolver().resolveDestination(this.failureChannelName);
    }
    if (evalResult != null && this.failureChannel != null) {
        MessagingException messagingException = new MessageHandlingExpressionEvaluatingAdviceException(message, "Handler Failed", this.unwrapThrowableIfNecessary(exception), evalResult);
        ErrorMessage resultMessage = new ErrorMessage(messagingException);
        this.messagingTemplate.send(this.failureChannel, resultMessage);
    }
    return evalResult;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException)

Example 35 with ErrorMessage

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

the class MessagingGatewaySupport method send.

protected void send(Object object) {
    this.initializeIfNecessary();
    Assert.notNull(object, "request must not be null");
    MessageChannel requestChannel = getRequestChannel();
    Assert.state(requestChannel != null, "send is not supported, because no request channel has been configured");
    try {
        if (this.countsEnabled) {
            this.messageCount.incrementAndGet();
        }
        this.messagingTemplate.convertAndSend(requestChannel, object, this.historyWritingPostProcessor);
    } catch (Exception e) {
        MessageChannel errorChannel = getErrorChannel();
        if (errorChannel != null) {
            this.messagingTemplate.send(errorChannel, new ErrorMessage(e));
        } else {
            this.rethrow(e, "failed to send message");
        }
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageMappingException(org.springframework.integration.mapping.MessageMappingException)

Aggregations

ErrorMessage (org.springframework.messaging.support.ErrorMessage)59 Test (org.junit.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 GenericMessage (org.springframework.messaging.support.GenericMessage)17 MessagingException (org.springframework.messaging.MessagingException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Message (org.springframework.messaging.Message)12 QueueChannel (org.springframework.integration.channel.QueueChannel)11 MessageHandlingException (org.springframework.messaging.MessageHandlingException)11 Socket (java.net.Socket)9 ArrayList (java.util.ArrayList)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 Semaphore (java.util.concurrent.Semaphore)6 Log (org.apache.commons.logging.Log)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ServerSocket (java.net.ServerSocket)3