Search in sources :

Example 36 with ErrorMessage

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

the class AbstractMessageProducingHandler method sendErrorMessage.

protected void sendErrorMessage(final Message<?> requestMessage, Throwable ex) {
    Object errorChannel = resolveErrorChannel(requestMessage.getHeaders());
    Throwable result = ex;
    if (!(ex instanceof MessagingException)) {
        result = new MessageHandlingException(requestMessage, ex);
    }
    if (errorChannel == null) {
        logger.error("Async exception received and no 'errorChannel' header exists and no default " + "'errorChannel' found", result);
    } else {
        try {
            sendOutput(new ErrorMessage(result), errorChannel, true);
        } catch (Exception e) {
            Exception exceptionToLog = e;
            if (!(e instanceof MessagingException)) {
                exceptionToLog = new MessageHandlingException(requestMessage, e);
            }
            logger.error("Failed to send async reply", exceptionToLog);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException)

Example 37 with ErrorMessage

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

the class MessageProducerSupportTests method validateSuccessfulErrorFlowDoesNotThrowErrors.

@Test
public void validateSuccessfulErrorFlowDoesNotThrowErrors() {
    TestApplicationContext testApplicationContext = TestUtils.createTestApplicationContext();
    testApplicationContext.refresh();
    DirectChannel outChannel = new DirectChannel();
    outChannel.subscribe(message -> {
        throw new RuntimeException("problems");
    });
    PublishSubscribeChannel errorChannel = new PublishSubscribeChannel();
    SuccessfulErrorService errorService = new SuccessfulErrorService();
    ServiceActivatingHandler handler = new ServiceActivatingHandler(errorService);
    handler.setBeanFactory(testApplicationContext);
    handler.afterPropertiesSet();
    errorChannel.subscribe(handler);
    MessageProducerSupport mps = new MessageProducerSupport() {
    };
    mps.setOutputChannel(outChannel);
    mps.setErrorChannel(errorChannel);
    mps.setBeanFactory(testApplicationContext);
    mps.afterPropertiesSet();
    mps.start();
    Message<?> message = new GenericMessage<String>("hello");
    mps.sendMessage(message);
    assertThat(errorService.lastMessage, instanceOf(ErrorMessage.class));
    ErrorMessage errorMessage = (ErrorMessage) errorService.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exception = (MessageDeliveryException) errorMessage.getPayload();
    assertEquals(message, exception.getFailedMessage());
    testApplicationContext.close();
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) ServiceActivatingHandler(org.springframework.integration.handler.ServiceActivatingHandler) Test(org.junit.Test)

Example 38 with ErrorMessage

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

the class AdvisedMessageHandlerTests method throwableProperlyPropagatedAndReported.

/**
 * Verify that Errors such as OOM are properly propagated and we suppress the
 * ThrowableHolderException from the output message.
 */
@Test
public void throwableProperlyPropagatedAndReported() throws Exception {
    QueueChannel errors = new QueueChannel();
    ExpressionEvaluatingRequestHandlerAdvice expressionAdvice = new ExpressionEvaluatingRequestHandlerAdvice();
    expressionAdvice.setBeanFactory(mock(BeanFactory.class));
    expressionAdvice.setOnFailureExpressionString("'foo'");
    expressionAdvice.setFailureChannel(errors);
    Throwable theThrowable = new Throwable("foo");
    ProxyFactory proxyFactory = new ProxyFactory(new Foo(theThrowable));
    proxyFactory.addAdvice(expressionAdvice);
    Bar fooHandler = (Bar) proxyFactory.getProxy();
    try {
        fooHandler.handleRequestMessage(new GenericMessage<String>("foo"));
        fail("Expected throwable");
    } catch (Throwable t) {
        assertSame(theThrowable, t);
        ErrorMessage error = (ErrorMessage) errors.receive(10000);
        assertNotNull(error);
        assertSame(theThrowable, error.getPayload().getCause());
    }
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) ProxyFactory(org.springframework.aop.framework.ProxyFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 39 with ErrorMessage

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

the class PayloadTypeSelectorTests method testSubclassOfAcceptedTypeIsSelected.

@Test
public void testSubclassOfAcceptedTypeIsSelected() {
    PayloadTypeSelector selector = new PayloadTypeSelector(RuntimeException.class);
    assertTrue(selector.accept(new ErrorMessage(new MessagingException("test"))));
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PayloadTypeSelector(org.springframework.integration.selector.PayloadTypeSelector) Test(org.junit.Test)

Example 40 with ErrorMessage

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

the class PayloadTypeSelectorTests method testMultipleAcceptedTypes.

@Test
public void testMultipleAcceptedTypes() {
    PayloadTypeSelector selector = new PayloadTypeSelector(String.class, Integer.class);
    assertTrue(selector.accept(new GenericMessage<String>("test1")));
    assertTrue(selector.accept(new GenericMessage<Integer>(2)));
    assertFalse(selector.accept(new ErrorMessage(new RuntimeException())));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) PayloadTypeSelector(org.springframework.integration.selector.PayloadTypeSelector) Test(org.junit.Test)

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