Search in sources :

Example 36 with MessageHandlingException

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

the class MessageSourcePollingTemplateTests method testAckNack.

@Test
public void testAckNack() {
    AcknowledgmentCallback callback = mock(AcknowledgmentCallback.class);
    given(callback.isAutoAck()).willReturn(true);
    MessageSource<?> source = () -> new GenericMessage<>("foo", Collections.singletonMap(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback));
    MessageSourcePollingTemplate template = new MessageSourcePollingTemplate(source);
    template.poll(h -> {
    });
    verify(callback).acknowledge(Status.ACCEPT);
    try {
        template.poll(h -> {
            throw new RuntimeException("expected");
        });
        fail("expected exception");
    } catch (MessageHandlingException e) {
        assertThat(e.getCause().getMessage(), equalTo("expected"));
    }
    verify(callback).acknowledge(Status.REJECT);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 37 with MessageHandlingException

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

the class CorrelatingMessageHandlerTests method bufferCompletesWithException.

@Test
public void bufferCompletesWithException() throws Exception {
    doAnswer(new ThrowsException(new RuntimeException("Planned test exception"))).when(processor).processMessageGroup(isA(SimpleMessageGroup.class));
    String correlationKey = "key";
    Message<?> message1 = testMessage(correlationKey, 1, 2);
    Message<?> message2 = testMessage(correlationKey, 2, 2);
    when(correlationStrategy.getCorrelationKey(isA(Message.class))).thenReturn(correlationKey);
    handler.setExpireGroupsUponCompletion(true);
    handler.handleMessage(message1);
    try {
        handler.handleMessage(message2);
        fail("Expected MessageHandlingException");
    } catch (MessageHandlingException e) {
        assertEquals(0, store.getMessageGroup(correlationKey).size());
    }
    verify(correlationStrategy).getCorrelationKey(message1);
    verify(correlationStrategy).getCorrelationKey(message2);
    verify(processor).processMessageGroup(isA(SimpleMessageGroup.class));
}
Also used : Message(org.springframework.messaging.Message) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) Test(org.junit.Test)

Example 38 with MessageHandlingException

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

the class ErrorMessageExceptionTypeRouterTests method fallbackToErrorMessageType.

@Test
public void fallbackToErrorMessageType() {
    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(MessageHandlingException.class.getName(), "messageHandlingExceptionChannel");
    router.setDefaultOutputChannel(defaultChannel);
    router.handleMessage(message);
    assertNotNull(messageHandlingExceptionChannel.receive(1000));
    assertNull(runtimeExceptionChannel.receive(0));
    assertNull(illegalArgumentChannel.receive(0));
    assertNull(defaultChannel.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 39 with MessageHandlingException

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

the class ErrorMessageExceptionTypeRouterTests method exceptionPayloadButNotErrorMessage.

@Test
public void exceptionPayloadButNotErrorMessage() {
    Message<?> failedMessage = new GenericMessage<String>("foo");
    IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
    RuntimeException middleCause = new RuntimeException(rootCause);
    MessageHandlingException error = new MessageHandlingException(failedMessage, "failed", middleCause);
    Message<?> message = new GenericMessage<Exception>(error);
    ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
    router.setBeanFactory(beanFactory);
    router.setApplicationContext(TestUtils.createTestApplicationContext());
    router.setChannelMapping(IllegalArgumentException.class.getName(), "illegalArgumentChannel");
    router.setChannelMapping(RuntimeException.class.getName(), "runtimeExceptionChannel");
    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) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 40 with MessageHandlingException

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

the class ErrorMessageExceptionTypeRouterTests method mostSpecificCause.

@Test
public void mostSpecificCause() {
    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(RuntimeException.class.getName(), "runtimeExceptionChannel");
    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)

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