Search in sources :

Example 26 with MessagingException

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

the class PollingTransactionTests method propagationMandatory.

@Test
public void propagationMandatory() throws Throwable {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("propagationMandatoryTests.xml", this.getClass());
    TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
    PollableChannel input = (PollableChannel) context.getBean("input");
    PollableChannel output = (PollableChannel) context.getBean("output");
    PollableChannel errorChannel = (PollableChannel) context.getBean("errorChannel");
    assertEquals(0, txManager.getCommitCount());
    input.send(new GenericMessage<String>("test"));
    Message<?> errorMessage = errorChannel.receive(3000);
    assertNotNull(errorMessage);
    Object payload = errorMessage.getPayload();
    assertEquals(MessagingException.class, payload.getClass());
    MessagingException messagingException = (MessagingException) payload;
    assertEquals(IllegalTransactionStateException.class, messagingException.getCause().getClass());
    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)

Example 27 with MessagingException

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

the class RoundRobinDispatcherTests 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.addHandler(handler);
    doThrow(new MessagingException("Mock Exception")).when(handler).handleMessage(message);
    try {
        dispatcher.dispatch(message);
        fail("Expected Exception");
    } catch (MessagingException e) {
        assertEquals(message, e.getFailedMessage());
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) Test(org.junit.Test)

Example 28 with MessagingException

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

the class SourcePollingChannelAdapterFactoryBeanTests method testInterrupted.

@Test
public void testInterrupted() throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    MessageSource<Object> ms = () -> {
        startLatch.countDown();
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new MessagingException("Interrupted awaiting stopLatch", e);
        }
        return null;
    };
    SourcePollingChannelAdapter pollingChannelAdapter = new SourcePollingChannelAdapter();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
    taskScheduler.setAwaitTerminationSeconds(1);
    taskScheduler.afterPropertiesSet();
    pollingChannelAdapter.setTaskScheduler(taskScheduler);
    MessagePublishingErrorHandler errorHandler = new MessagePublishingErrorHandler();
    Log errorHandlerLogger = TestUtils.getPropertyValue(errorHandler, "logger", Log.class);
    errorHandlerLogger = spy(errorHandlerLogger);
    DirectFieldAccessor dfa = new DirectFieldAccessor(errorHandler);
    dfa.setPropertyValue("logger", errorHandlerLogger);
    pollingChannelAdapter.setErrorHandler(errorHandler);
    pollingChannelAdapter.setSource(ms);
    pollingChannelAdapter.setOutputChannel(new NullChannel());
    pollingChannelAdapter.setBeanFactory(mock(BeanFactory.class));
    pollingChannelAdapter.afterPropertiesSet();
    Log adapterLogger = TestUtils.getPropertyValue(pollingChannelAdapter, "logger", Log.class);
    adapterLogger = spy(adapterLogger);
    when(adapterLogger.isDebugEnabled()).thenReturn(true);
    dfa = new DirectFieldAccessor(pollingChannelAdapter);
    dfa.setPropertyValue("logger", adapterLogger);
    pollingChannelAdapter.start();
    assertTrue(startLatch.await(10, TimeUnit.SECONDS));
    pollingChannelAdapter.stop();
    taskScheduler.shutdown();
    verifyZeroInteractions(errorHandlerLogger);
    verify(adapterLogger).debug(contains("Poll interrupted - during stop()?"));
}
Also used : MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) MessagingException(org.springframework.messaging.MessagingException) Log(org.apache.commons.logging.Log) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) NullChannel(org.springframework.integration.channel.NullChannel) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Test(org.junit.Test)

Example 29 with MessagingException

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

the class DefaultOutboundChannelAdapterParserTests method checkConfigWithInnerBeanAndPoller.

@Test
public void checkConfigWithInnerBeanAndPoller() {
    Object adapter = context.getBean("adapterB");
    assertEquals(Boolean.FALSE, TestUtils.getPropertyValue(adapter, "autoStartup"));
    MessageHandler handler = TestUtils.getPropertyValue(adapter, "handler", MessageHandler.class);
    assertTrue(AopUtils.isAopProxy(handler));
    assertThat(TestUtils.getPropertyValue(handler, "h.advised.advisors.first.item.advice"), Matchers.instanceOf(RequestHandlerRetryAdvice.class));
    handler.handleMessage(new GenericMessage<>("foo"));
    QueueChannel recovery = context.getBean("recovery", QueueChannel.class);
    Message<?> received = recovery.receive(10000);
    assertNotNull(received);
    assertThat(received, instanceOf(ErrorMessage.class));
    assertThat(received.getPayload(), instanceOf(MessagingException.class));
    assertEquals("foo", ((MessagingException) received.getPayload()).getFailedMessage().getPayload());
}
Also used : RequestHandlerRetryAdvice(org.springframework.integration.handler.advice.RequestHandlerRetryAdvice) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 30 with MessagingException

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

the class ErrorMessagePublisher method publish.

/**
 * Publish an error message for the supplied throwable and context.
 * The {@link #errorMessageStrategy} is used to build a {@link ErrorMessage}
 * to publish.
 * @param throwable the throwable. May be null.
 * @param context the context for {@link ErrorMessage} properties.
 */
public void publish(Throwable throwable, AttributeAccessor context) {
    populateChannel();
    Throwable payload = determinePayload(throwable, context);
    ErrorMessage errorMessage = this.errorMessageStrategy.buildErrorMessage(payload, context);
    if (this.logger.isDebugEnabled() && payload instanceof MessagingException) {
        MessagingException exception = (MessagingException) errorMessage.getPayload();
        this.logger.debug("Sending ErrorMessage: failedMessage: " + exception.getFailedMessage(), exception);
    }
    this.messagingTemplate.send(errorMessage);
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

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