Search in sources :

Example 21 with MessagingException

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

the class InboundEndpointTests method testRetryWithinOnMessageAdapter.

@Test
public void testRetryWithinOnMessageAdapter() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundChannelAdapter adapter = new AmqpInboundChannelAdapter(container);
    adapter.setOutputChannel(new DirectChannel());
    adapter.setRetryTemplate(new RetryTemplate());
    QueueChannel errors = new QueueChannel();
    ErrorMessageSendingRecoverer recoveryCallback = new ErrorMessageSendingRecoverer(errors);
    recoveryCallback.setErrorMessageStrategy(new AmqpMessageHeaderErrorMessageStrategy());
    adapter.setRecoveryCallback(recoveryCallback);
    adapter.afterPropertiesSet();
    ChannelAwareMessageListener listener = (ChannelAwareMessageListener) container.getMessageListener();
    listener.onMessage(org.springframework.amqp.core.MessageBuilder.withBody("foo".getBytes()).andProperties(new MessageProperties()).build(), null);
    Message<?> errorMessage = errors.receive(0);
    assertNotNull(errorMessage);
    assertThat(errorMessage.getPayload(), instanceOf(MessagingException.class));
    MessagingException payload = (MessagingException) errorMessage.getPayload();
    assertThat(payload.getMessage(), containsString("Dispatcher has no"));
    assertThat(StaticMessageHeaderAccessor.getDeliveryAttempt(payload.getFailedMessage()).get(), equalTo(3));
    org.springframework.amqp.core.Message amqpMessage = errorMessage.getHeaders().get(AmqpMessageHeaderErrorMessageStrategy.AMQP_RAW_MESSAGE, org.springframework.amqp.core.Message.class);
    assertThat(amqpMessage, notNullValue());
    assertNull(errors.receive(0));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) MessagingException(org.springframework.messaging.MessagingException) SimpleMessageListenerContainer(org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer) ChannelAwareMessageListener(org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener) ConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory) RetryTemplate(org.springframework.retry.support.RetryTemplate) MessageProperties(org.springframework.amqp.core.MessageProperties) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) AmqpMessageHeaderErrorMessageStrategy(org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy) AbstractMessageListenerContainer(org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer) Test(org.junit.Test)

Example 22 with MessagingException

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

the class BarrierMessageHandler method handleRequestMessage.

@Override
protected Object handleRequestMessage(Message<?> requestMessage) {
    Object key = this.correlationStrategy.getCorrelationKey(requestMessage);
    if (key == null) {
        throw new MessagingException(requestMessage, "Correlation Strategy returned null");
    }
    Thread existing = this.inProcess.putIfAbsent(key, Thread.currentThread());
    if (existing != null) {
        throw new MessagingException(requestMessage, "Correlation key (" + key + ") is already in use by " + existing.getName());
    }
    SynchronousQueue<Message<?>> syncQueue = createOrObtainQueue(key);
    try {
        Message<?> releaseMessage = syncQueue.poll(this.timeout, TimeUnit.MILLISECONDS);
        if (releaseMessage != null) {
            return processRelease(key, requestMessage, releaseMessage);
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessageHandlingException(requestMessage, "Interrupted while waiting for release", e);
    } finally {
        this.inProcess.remove(key);
        this.suspensions.remove(key);
    }
    return null;
}
Also used : Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 23 with MessagingException

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

the class PollingLifecycleTests method ensurePollerTaskStops.

@Test
public void ensurePollerTaskStops() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    QueueChannel channel = new QueueChannel();
    channel.send(new GenericMessage<String>("foo"));
    MessageHandler handler = Mockito.spy(new MessageHandler() {

        @Override
        public void handleMessage(Message<?> message) throws MessagingException {
            latch.countDown();
        }
    });
    PollingConsumer consumer = new PollingConsumer(channel, handler);
    consumer.setTrigger(new PeriodicTrigger(0));
    consumer.setErrorHandler(errorHandler);
    consumer.setTaskScheduler(taskScheduler);
    consumer.setBeanFactory(mock(BeanFactory.class));
    consumer.afterPropertiesSet();
    consumer.start();
    assertTrue(latch.await(2, TimeUnit.SECONDS));
    Mockito.verify(handler, times(1)).handleMessage(Mockito.any(Message.class));
    consumer.stop();
    for (int i = 0; i < 10; i++) {
        channel.send(new GenericMessage<String>("foo"));
    }
    // give enough time for poller to kick in if it didn't stop properly
    Thread.sleep(2000);
    // we'll still have a natural race condition between call to stop() and poller polling
    // so what we really have to assert is that it doesn't poll for more then once after stop() was called
    Mockito.reset(handler);
    Mockito.verify(handler, atMost(1)).handleMessage(Mockito.any(Message.class));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessageHandler(org.springframework.messaging.MessageHandler) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessagingException(org.springframework.messaging.MessagingException) CountDownLatch(java.util.concurrent.CountDownLatch) PeriodicTrigger(org.springframework.scheduling.support.PeriodicTrigger) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) Test(org.junit.Test)

Example 24 with MessagingException

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

the class BroadcastingDispatcherTests method testExceptionEnhancement.

/**
 * Verifies that the dispatcher adds the message to the exception if it
 * was not attached by the handler.
 */
@Test
public void testExceptionEnhancement() {
    dispatcher = new BroadcastingDispatcher();
    dispatcher.addHandler(targetMock1);
    doThrow(new MessagingException("Mock Exception")).when(targetMock1).handleMessage(eq(messageMock));
    try {
        dispatcher.dispatch(messageMock);
        fail("Expected Exception");
    } catch (MessagingException e) {
        assertEquals(messageMock, e.getFailedMessage());
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test)

Example 25 with MessagingException

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

the class PollingTransactionTests method commitFailureAndHandlerFailureTest.

@Test
public void commitFailureAndHandlerFailureTest() throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("transactionFailureTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManagerBad");
    PollableChannel inputTxFail = (PollableChannel) context.getBean("inputTxFailure");
    PollableChannel inputHandlerFail = (PollableChannel) context.getBean("inputHandlerFailure");
    PollableChannel output = (PollableChannel) context.getBean("output");
    PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
    assertEquals(0, txManager.getCommitCount());
    inputTxFail.send(new GenericMessage<>("commitFailureTest"));
    Message<?> errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertEquals(MessagingException.class, payload.getClass());
    MessagingException messagingException = (MessagingException) payload;
    assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
    assertNotNull(messagingException.getFailedMessage());
    assertNotNull(output.receive(0));
    assertEquals(0, txManager.getCommitCount());
    inputHandlerFail.send(new GenericMessage<>("handlerFalilureTest"));
    errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    payload = errorMessage.getPayload();
    assertEquals(MessageHandlingException.class, payload.getClass());
    messagingException = (MessageHandlingException) payload;
    assertEquals(RuntimeException.class, messagingException.getCause().getClass());
    assertNotNull(messagingException.getFailedMessage());
    assertNull(output.receive(0));
    assertEquals(0, txManager.getCommitCount());
    context.close();
}
Also used : ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) TestTransactionManager(org.springframework.integration.util.TestTransactionManager) Test(org.junit.Test)

Aggregations

MessagingException (org.springframework.messaging.MessagingException)145 Test (org.junit.Test)61 IOException (java.io.IOException)25 Message (org.springframework.messaging.Message)23 QueueChannel (org.springframework.integration.channel.QueueChannel)22 ErrorMessage (org.springframework.messaging.support.ErrorMessage)21 CountDownLatch (java.util.concurrent.CountDownLatch)17 MessageHandlingException (org.springframework.messaging.MessageHandlingException)16 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 GenericMessage (org.springframework.messaging.support.GenericMessage)15 MessageHandler (org.springframework.messaging.MessageHandler)14 File (java.io.File)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 DirectChannel (org.springframework.integration.channel.DirectChannel)12 ArrayList (java.util.ArrayList)11 PollableChannel (org.springframework.messaging.PollableChannel)11 Lock (java.util.concurrent.locks.Lock)8 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)8