Search in sources :

Example 1 with MessageRejectedException

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

the class TransformerTests method transformWithHeader.

@Test
public void transformWithHeader() {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message = MessageBuilder.withPayload("Foo").setReplyChannel(replyChannel).build();
    this.pojoTransformFlowInput.send(message);
    Message<?> receive = replyChannel.receive(10000);
    assertNotNull(receive);
    assertEquals("FooBar", receive.getPayload());
    try {
        this.pojoTransformFlowInput.send(message);
        fail("MessageRejectedException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageRejectedException.class));
        assertThat(e.getMessage(), containsString("IdempotentReceiver"));
        assertThat(e.getMessage(), containsString("rejected duplicate Message"));
    }
    assertNotNull(this.idempotentDiscardChannel.receive(10000));
    assertNotNull(this.adviceChannel.receive(10000));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) IOException(java.io.IOException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 2 with MessageRejectedException

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

the class RoundRobinDispatcherConcurrentTests method noHandlerSkipUnderConcurrentFailureWithFailover.

@Test
public void noHandlerSkipUnderConcurrentFailureWithFailover() throws Exception {
    dispatcher.addHandler(handler1);
    dispatcher.addHandler(handler2);
    doThrow(new MessageRejectedException(message, null)).when(handler1).handleMessage(message);
    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 = new Runnable() {

        @Override
        public void run() {
            try {
                start.await();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            if (!dispatcher.dispatch(message)) {
                failed.set(true);
            } else {
                allDone.countDown();
            }
        }
    };
    for (int i = 0; i < TOTAL_EXECUTIONS; i++) {
        executor.execute(messageSenderTask);
    }
    start.countDown();
    allDone.await(5000, TimeUnit.MILLISECONDS);
    assertFalse("not all messages were accepted", failed.get());
    verify(handler1, times(TOTAL_EXECUTIONS / 2)).handleMessage(message);
    verify(handler2, times(TOTAL_EXECUTIONS)).handleMessage(message);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 3 with MessageRejectedException

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

the class IdempotentReceiverInterceptor method doInvoke.

@Override
protected Object doInvoke(MethodInvocation invocation, Message<?> message) throws Throwable {
    boolean accept = this.messageSelector.accept(message);
    if (!accept) {
        boolean discarded = false;
        MessageChannel theDiscardChannel = obtainDiscardChannel();
        if (theDiscardChannel != null) {
            this.messagingTemplate.send(theDiscardChannel, message);
            discarded = true;
        }
        if (this.throwExceptionOnRejection) {
            throw new MessageRejectedException(message, "IdempotentReceiver '" + this + "' rejected duplicate Message: " + message);
        }
        if (!discarded) {
            invocation.getArguments()[0] = getMessageBuilderFactory().fromMessage(message).setHeader(IntegrationMessageHeaderAccessor.DUPLICATE_MESSAGE, true).build();
        } else {
            return null;
        }
    }
    return invocation.proceed();
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MessageRejectedException(org.springframework.integration.MessageRejectedException)

Example 4 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method noFailoverLoadBalancing.

@Test
public void noFailoverLoadBalancing() {
    DirectChannel channel = (DirectChannel) ac.getBean("loadBalancerNoFailover");
    doThrow(new MessageRejectedException(message, null)).when(handlerA).handleMessage(message);
    UnicastingDispatcher dispatcher = channel.getDispatcher();
    dispatcher.setLoadBalancingStrategy(new RoundRobinLoadBalancingStrategy());
    dispatcher.addHandler(handlerA);
    dispatcher.addHandler(handlerB);
    dispatcher.addHandler(handlerC);
    InOrder inOrder = inOrder(handlerA, handlerB, handlerC);
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    inOrder.verify(handlerA).handleMessage(message);
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    inOrder.verify(handlerB).handleMessage(message);
    try {
        channel.send(message);
    } catch (Exception e) {
    /* ignore */
    }
    inOrder.verify(handlerC).handleMessage(message);
    verify(handlerA, times(1)).handleMessage(message);
    verify(handlerB, times(1)).handleMessage(message);
    verify(handlerC, times(1)).handleMessage(message);
}
Also used : RoundRobinLoadBalancingStrategy(org.springframework.integration.dispatcher.RoundRobinLoadBalancingStrategy) InOrder(org.mockito.InOrder) UnicastingDispatcher(org.springframework.integration.dispatcher.UnicastingDispatcher) MessageRejectedException(org.springframework.integration.MessageRejectedException) MessageRejectedException(org.springframework.integration.MessageRejectedException) Test(org.junit.Test)

Example 5 with MessageRejectedException

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

the class MixedDispatcherConfigurationScenarioTests method failoverNoLoadBalancing.

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