Search in sources :

Example 6 with MessageRejectedException

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

the class XmlValidatingMessageSelector method accept.

@Override
public boolean accept(Message<?> message) {
    SAXParseException[] validationExceptions = null;
    try {
        validationExceptions = this.xmlValidator.validate(this.converter.convertToSource(message.getPayload()));
    } catch (Exception e) {
        throw new MessageHandlingException(message, e);
    }
    boolean validationSuccess = ObjectUtils.isEmpty(validationExceptions);
    if (!validationSuccess) {
        if (this.throwExceptionOnRejection) {
            throw new MessageRejectedException(message, "Message was rejected due to XML Validation errors", new AggregatedXmlMessageValidationException(Arrays.<Throwable>asList(validationExceptions)));
        }
        this.logger.debug("Message was rejected due to XML Validation errors");
    }
    return validationSuccess;
}
Also used : AggregatedXmlMessageValidationException(org.springframework.integration.xml.AggregatedXmlMessageValidationException) SAXParseException(org.xml.sax.SAXParseException) MessageRejectedException(org.springframework.integration.MessageRejectedException) IOException(java.io.IOException) AggregatedXmlMessageValidationException(org.springframework.integration.xml.AggregatedXmlMessageValidationException) SAXParseException(org.xml.sax.SAXParseException) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 7 with MessageRejectedException

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

the class MessagingAnnotationsWithBeanAnnotationTests method testMessagingAnnotationsFlow.

@Test
public void testMessagingAnnotationsFlow() {
    Stream.of(this.sourcePollingChannelAdapters).forEach(a -> a.start());
    // this.sourcePollingChannelAdapter.start();
    for (int i = 0; i < 10; i++) {
        Message<?> receive = this.discardChannel.receive(10000);
        assertNotNull(receive);
        assertTrue(((Integer) receive.getPayload()) % 2 == 0);
        receive = this.counterErrorChannel.receive(10000);
        assertNotNull(receive);
        assertThat(receive, instanceOf(ErrorMessage.class));
        assertThat(receive.getPayload(), instanceOf(MessageRejectedException.class));
        MessageRejectedException exception = (MessageRejectedException) receive.getPayload();
        assertThat(exception.getMessage(), containsString("MessageFilter " + "'messagingAnnotationsWithBeanAnnotationTests.ContextConfiguration.filter.filter.handler'" + " rejected Message"));
    }
    for (Message<?> message : collector) {
        assertFalse(((Integer) message.getPayload()) % 2 == 0);
        MessageHistory messageHistory = MessageHistory.read(message);
        assertNotNull(messageHistory);
        String messageHistoryString = messageHistory.toString();
        assertThat(messageHistoryString, Matchers.containsString("routerChannel"));
        assertThat(messageHistoryString, Matchers.containsString("filterChannel"));
        assertThat(messageHistoryString, Matchers.containsString("aggregatorChannel"));
        assertThat(messageHistoryString, Matchers.containsString("splitterChannel"));
        assertThat(messageHistoryString, Matchers.containsString("serviceChannel"));
        assertThat(messageHistoryString, Matchers.not(Matchers.containsString("discardChannel")));
    }
    assertNull(this.skippedServiceActivator);
    assertNull(this.skippedMessageHandler);
    assertNull(this.skippedChannel);
    assertNull(this.skippedChannel2);
    assertNull(this.skippedMessageSource);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHistory(org.springframework.integration.history.MessageHistory) EnableMessageHistory(org.springframework.integration.config.EnableMessageHistory) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 8 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverNoLoadBalancingConcurrent.

@Test
public void noFailoverNoLoadBalancingConcurrent() throws Exception {
    final DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerNoFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        boolean sent = false;
        try {
            sent = channel.send(message);
        } catch (Exception e2) {
            exceptionRegistry.add(e2);
        }
        if (!sent) {
            failed.set(true);
        }
        allDone.countDown();
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    assertTrue(allDone.await(10, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertTrue("not all messages were accepted", failed.get());
    verify(handlerA, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerB, times(0)).handleMessage(message);
    verify(exceptionRegistry, times(TOTAL_EXECUTIONS)).add(any(Exception.class));
}
Also used : UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 9 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method failoverNoLoadBalancingConcurrent.

@Test
public void failoverNoLoadBalancingConcurrent() throws Exception {
    final DirectChannel channel = (DirectChannel) ac.getBean("noLoadBalancerFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch allDone = new CountDownLatch(TOTAL_EXECUTIONS);
    final Message<?> message = this.message;
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        boolean sent = false;
        try {
            sent = channel.send(message);
        } catch (Exception e2) {
            exceptionRegistry.add(e2);
        }
        if (!sent) {
            failed.set(true);
        }
        allDone.countDown();
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    assertTrue(allDone.await(10, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertFalse("not all messages were accepted", failed.get());
    verify(handlerA, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerB, times(TOTAL_EXECUTIONS)).handleMessage(message);
    verify(handlerC, never()).handleMessage(message);
    verify(exceptionRegistry, never()).add(any(Exception.class));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 10 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverLoadBalancingConcurrent.

@Test
public void noFailoverLoadBalancingConcurrent() throws Exception {
    final DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    final CountDownLatch start = new CountDownLatch(1);
    final CountDownLatch allDone = new CountDownLatch(TOTAL_EXECUTIONS);
    final Message<?> message = this.message;
    final AtomicBoolean failed = new AtomicBoolean(false);
    Runnable messageSenderTask = () -> {
        try {
            start.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
        }
        boolean sent = false;
        try {
            sent = channel.send(message);
        } catch (Exception e2) {
            exceptionRegistry.add(e2);
        }
        if (!sent) {
            failed.set(true);
        }
        allDone.countDown();
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    assertTrue(allDone.await(10, TimeUnit.SECONDS));
    executor.shutdown();
    executor.awaitTermination(10, TimeUnit.SECONDS);
    assertTrue("not all messages were accepted", failed.get());
    verify(handlerA, times(14)).handleMessage(message);
    verify(handlerB, times(13)).handleMessage(message);
    verify(handlerC, times(13)).handleMessage(message);
    verify(exceptionRegistry, times(14)).add(any(Exception.class));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) CountDownLatch(java.util.concurrent.CountDownLatch) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Aggregations

MessageRejectedException (org.springframework.integration.MessageRejectedException)15 Test (org.junit.Test)13 UnicastingDispatcher (org.springframework.integration.dispatcher.UnicastingDispatcher)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 GenericMessage (org.springframework.messaging.support.GenericMessage)3 IOException (java.io.IOException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 InOrder (org.mockito.InOrder)2 AggregatedXmlMessageValidationException (org.springframework.integration.xml.AggregatedXmlMessageValidationException)2 MessageChannel (org.springframework.messaging.MessageChannel)2 ErrorMessage (org.springframework.messaging.support.ErrorMessage)2 Map (java.util.Map)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1