Search in sources :

Example 11 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverNoLoadBalancing.

@Test
public void noFailoverNoLoadBalancing() {
    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);
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    verify(handlerA, times(2)).handleMessage(message);
    verify(handlerB, times(0)).handleMessage(message);
}
Also used : UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 12 with MessageRejectedException

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

the class IdempotentReceiverTests method testIdempotentReceiverInterceptor.

@Test
public void testIdempotentReceiverInterceptor() {
    ConcurrentMetadataStore store = new SimpleMetadataStore();
    ExpressionEvaluatingMessageProcessor<String> idempotentKeyStrategy = new ExpressionEvaluatingMessageProcessor<>(new SpelExpressionParser().parseExpression("payload"));
    BeanFactory beanFactory = Mockito.mock(BeanFactory.class);
    idempotentKeyStrategy.setBeanFactory(beanFactory);
    IdempotentReceiverInterceptor idempotentReceiverInterceptor = new IdempotentReceiverInterceptor(new MetadataStoreSelector(idempotentKeyStrategy, store));
    idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
    AtomicReference<Message<?>> handled = new AtomicReference<>();
    MessageHandler idempotentReceiver = handled::set;
    ProxyFactory proxyFactory = new ProxyFactory(idempotentReceiver);
    proxyFactory.addAdvice(idempotentReceiverInterceptor);
    idempotentReceiver = (MessageHandler) proxyFactory.getProxy();
    idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
    assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
    assertNotNull(store.get("foo"));
    try {
        idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageRejectedException.class));
    }
    idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
    idempotentReceiver.handleMessage(new GenericMessage<>("foo"));
    assertTrue(handled.get().getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
    assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
}
Also used : ExpressionEvaluatingMessageProcessor(org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageHandler(org.springframework.messaging.MessageHandler) ProxyFactory(org.springframework.aop.framework.ProxyFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) SimpleMetadataStore(org.springframework.integration.metadata.SimpleMetadataStore) ConcurrentMetadataStore(org.springframework.integration.metadata.ConcurrentMetadataStore) MessageRejectedException(org.springframework.integration.MessageRejectedException) MetadataStoreSelector(org.springframework.integration.selector.MetadataStoreSelector) SpelExpressionParser(org.springframework.expression.spel.standard.SpelExpressionParser) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 13 with MessageRejectedException

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

the class ErrorMessageExceptionTypeRouterTests method testHierarchicalMapping.

@Test
public void testHierarchicalMapping() {
    IllegalArgumentException rootCause = new IllegalArgumentException("bad argument");
    MessageHandlingException error = new MessageRejectedException(new GenericMessage<Object>("foo"), "failed", rootCause);
    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(defaultChannel.receive(0));
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 14 with MessageRejectedException

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

the class IdempotentReceiverIntegrationTests method testIdempotentReceiver.

@Test
public void testIdempotentReceiver() {
    this.idempotentReceiverInterceptor.setThrowExceptionOnRejection(true);
    TestUtils.getPropertyValue(this.store, "metadata", Map.class).clear();
    Message<String> message = new GenericMessage<>("foo");
    this.input.send(message);
    Message<?> receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals(1, this.adviceCalled.get());
    assertEquals(1, TestUtils.getPropertyValue(this.store, "metadata", Map.class).size());
    String foo = this.store.get("foo");
    assertEquals("FOO", foo);
    try {
        this.input.send(message);
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageRejectedException.class));
    }
    this.idempotentReceiverInterceptor.setThrowExceptionOnRejection(false);
    this.input.send(message);
    receive = this.output.receive(10000);
    assertNotNull(receive);
    assertEquals(2, this.adviceCalled.get());
    assertTrue(receive.getHeaders().get(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, Boolean.class));
    assertEquals(1, TestUtils.getPropertyValue(store, "metadata", Map.class).size());
    assertTrue(this.txSupplied.get());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Map(java.util.Map) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 15 with MessageRejectedException

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

the class XmlPayloadValidatingFilterParserTests method testInvalidMessageWithThrowException.

@Test
public void testInvalidMessageWithThrowException() throws Exception {
    Document doc = XmlTestUtil.getDocumentForString("<greeting ping=\"pong\"><other/></greeting>");
    GenericMessage<Document> docMessage = new GenericMessage<Document>(doc);
    MessageChannel inputChannel = ac.getBean("inputChannelB", MessageChannel.class);
    try {
        inputChannel.send(docMessage);
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, Matchers.instanceOf(MessageRejectedException.class));
        Throwable cause = e.getCause();
        assertThat(cause, Matchers.instanceOf(AggregatedXmlMessageValidationException.class));
        assertThat(cause.getMessage(), Matchers.containsString("Element 'greeting' is a simple type, so it must have no element information item [children]."));
        assertThat(cause.getMessage(), Matchers.containsString("Element 'greeting' is a simple type, so it cannot have attributes,"));
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) Document(org.w3c.dom.Document) AggregatedXmlMessageValidationException(org.springframework.integration.xml.AggregatedXmlMessageValidationException) MessageRejectedException(org.springframework.integration.MessageRejectedException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) 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