Search in sources :

Example 31 with MessageDeliveryException

use of org.springframework.messaging.MessageDeliveryException 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 32 with MessageDeliveryException

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

the class EnableIntegrationTests method testBridgeAnnotations.

@Test
public void testBridgeAnnotations() {
    GenericMessage<?> testMessage = new GenericMessage<Object>("foo");
    this.bridgeInput.send(testMessage);
    Message<?> receive = this.bridgeOutput.receive(2000);
    assertNotNull(receive);
    assertSame(receive, testMessage);
    assertNull(this.bridgeOutput.receive(10));
    this.pollableBridgeInput.send(testMessage);
    receive = this.pollableBridgeOutput.receive(2000);
    assertNotNull(receive);
    assertSame(receive, testMessage);
    assertNull(this.pollableBridgeOutput.receive(10));
    try {
        this.metaBridgeInput.send(testMessage);
        fail("MessageDeliveryException expected");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), Matchers.containsString("Dispatcher has no subscribers"));
    }
    this.context.getBean("enableIntegrationTests.ContextConfiguration.metaBridgeOutput.bridgeFrom", Lifecycle.class).start();
    this.metaBridgeInput.send(testMessage);
    receive = this.metaBridgeOutput.receive(2000);
    assertNotNull(receive);
    assertSame(receive, testMessage);
    assertNull(this.metaBridgeOutput.receive(10));
    this.bridgeToInput.send(testMessage);
    receive = this.bridgeToOutput.receive(2000);
    assertNotNull(receive);
    assertSame(receive, testMessage);
    assertNull(this.bridgeToOutput.receive(10));
    PollableChannel replyChannel = new QueueChannel();
    Message<?> bridgeMessage = MessageBuilder.fromMessage(testMessage).setReplyChannel(replyChannel).build();
    this.pollableBridgeToInput.send(bridgeMessage);
    receive = replyChannel.receive(2000);
    assertNotNull(receive);
    assertSame(receive, bridgeMessage);
    assertNull(replyChannel.receive(10));
    try {
        this.myBridgeToInput.send(testMessage);
        fail("MessageDeliveryException expected");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(MessageDeliveryException.class));
        assertThat(e.getMessage(), Matchers.containsString("Dispatcher has no subscribers"));
    }
    this.context.getBean("enableIntegrationTests.ContextConfiguration.myBridgeToInput.bridgeTo", Lifecycle.class).start();
    this.myBridgeToInput.send(bridgeMessage);
    receive = replyChannel.receive(2000);
    assertNotNull(receive);
    assertSame(receive, bridgeMessage);
    assertNull(replyChannel.receive(10));
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) Lifecycle(org.springframework.context.Lifecycle) SmartLifecycle(org.springframework.context.SmartLifecycle) PollableChannel(org.springframework.messaging.PollableChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test)

Example 33 with MessageDeliveryException

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

the class DelayHandlerTests method errorChannelHeaderAndHandlerThrowsExceptionWithDelay.

@Test
public void errorChannelHeaderAndHandlerThrowsExceptionWithDelay() throws Exception {
    DirectChannel errorChannel = new DirectChannel();
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    errorHandler.setDefaultErrorChannel(errorChannel);
    taskScheduler.setErrorHandler(errorHandler);
    this.setDelayExpression();
    this.startDelayerHandler();
    output.unsubscribe(resultHandler);
    errorChannel.subscribe(resultHandler);
    output.subscribe(message -> {
        throw new UnsupportedOperationException("intentional test failure");
    });
    Message<?> message = MessageBuilder.withPayload("test").setHeader("delay", "10").setErrorChannel(errorChannel).build();
    input.send(message);
    waitForLatch(10000);
    Message<?> errorMessage = resultHandler.lastMessage;
    assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
    MessageDeliveryException exceptionPayload = (MessageDeliveryException) errorMessage.getPayload();
    assertSame(message.getPayload(), exceptionPayload.getFailedMessage().getPayload());
    assertEquals(UnsupportedOperationException.class, exceptionPayload.getCause().getClass());
    assertNotSame(Thread.currentThread(), resultHandler.lastThread);
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) DirectChannel(org.springframework.integration.channel.DirectChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 34 with MessageDeliveryException

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

the class MethodInvokingRouterTests method doTestMultiChannelListResolutionByPayload.

private void doTestMultiChannelListResolutionByPayload(MethodInvokingRouter router, TestChannelResolver channelResolver) {
    QueueChannel fooChannel = new QueueChannel();
    QueueChannel barChannel = new QueueChannel();
    channelResolver.addChannel("foo-channel", fooChannel);
    channelResolver.addChannel("bar-channel", barChannel);
    router.setChannelResolver(channelResolver);
    Message<String> fooMessage = new GenericMessage<String>("foo");
    Message<String> barMessage = new GenericMessage<String>("bar");
    Message<String> badMessage = new GenericMessage<String>("bad");
    router.handleMessage(fooMessage);
    Message<?> result1a = fooChannel.receive(0);
    Message<?> result1b = barChannel.receive(0);
    assertNotNull(result1a);
    assertEquals("foo", result1a.getPayload());
    assertNotNull(result1b);
    assertEquals("foo", result1b.getPayload());
    router.handleMessage(barMessage);
    Message<?> result2a = fooChannel.receive(0);
    Message<?> result2b = barChannel.receive(0);
    assertNotNull(result2a);
    assertEquals("bar", result2a.getPayload());
    assertNotNull(result2b);
    assertEquals("bar", result2b.getPayload());
    try {
        router.handleMessage(badMessage);
        fail();
    } catch (MessageDeliveryException e) {
    /* Success */
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Example 35 with MessageDeliveryException

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

the class MethodInvokingRouterTests method doTestChannelInstanceResolutionByMessage.

private void doTestChannelInstanceResolutionByMessage(MethodInvokingRouter router, TestChannelResolver channelResolver) {
    QueueChannel fooChannel = new QueueChannel();
    QueueChannel barChannel = new QueueChannel();
    channelResolver.addChannel("foo-channel", fooChannel);
    channelResolver.addChannel("bar-channel", barChannel);
    router.setChannelResolver(channelResolver);
    Message<String> fooMessage = new GenericMessage<String>("foo");
    Message<String> barMessage = new GenericMessage<String>("bar");
    Message<String> badMessage = new GenericMessage<String>("bad");
    router.handleMessage(fooMessage);
    Message<?> result1 = fooChannel.receive(0);
    assertNotNull(result1);
    assertEquals("foo", result1.getPayload());
    router.handleMessage(barMessage);
    Message<?> result2 = barChannel.receive(0);
    assertNotNull(result2);
    assertEquals("bar", result2.getPayload());
    try {
        router.handleMessage(badMessage);
        fail();
    } catch (MessageDeliveryException e) {
    /* Success */
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) QueueChannel(org.springframework.integration.channel.QueueChannel) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException)

Aggregations

MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)47 Test (org.junit.Test)19 GenericMessage (org.springframework.messaging.support.GenericMessage)17 QueueChannel (org.springframework.integration.channel.QueueChannel)13 MessagingException (org.springframework.messaging.MessagingException)10 MessageChannel (org.springframework.messaging.MessageChannel)8 Message (org.springframework.messaging.Message)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageHandler (org.springframework.messaging.MessageHandler)5 MessageHeaders (org.springframework.messaging.MessageHeaders)4 PollableChannel (org.springframework.messaging.PollableChannel)4 IOException (java.io.IOException)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 SubscribableChannel (org.springframework.messaging.SubscribableChannel)3 MessageHeaderAccessor (org.springframework.messaging.support.MessageHeaderAccessor)3 FileNotFoundException (java.io.FileNotFoundException)2 Date (java.util.Date)2 List (java.util.List)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2