Search in sources :

Example 11 with ErrorMessage

use of org.springframework.messaging.support.ErrorMessage 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)

Example 12 with ErrorMessage

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

the class ApplicationContextMessageBusTests method consumerSubscribedToErrorChannel.

@Test
public void consumerSubscribedToErrorChannel() throws InterruptedException {
    TestApplicationContext context = TestUtils.createTestApplicationContext();
    QueueChannel errorChannel = new QueueChannel();
    context.registerChannel(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, errorChannel);
    final CountDownLatch latch = new CountDownLatch(1);
    AbstractReplyProducingMessageHandler handler = new AbstractReplyProducingMessageHandler() {

        @Override
        public Object handleRequestMessage(Message<?> message) {
            latch.countDown();
            return null;
        }
    };
    PollingConsumer endpoint = new PollingConsumer(errorChannel, handler);
    endpoint.setBeanFactory(mock(BeanFactory.class));
    context.registerEndpoint("testEndpoint", endpoint);
    context.refresh();
    errorChannel.send(new ErrorMessage(new RuntimeException("test-exception")));
    latch.await(1000, TimeUnit.MILLISECONDS);
    assertEquals("handler should have received error message", 0, latch.getCount());
    context.stop();
}
Also used : PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) QueueChannel(org.springframework.integration.channel.QueueChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorMessage(org.springframework.messaging.support.ErrorMessage) TestApplicationContext(org.springframework.integration.test.util.TestUtils.TestApplicationContext) Test(org.junit.Test)

Example 13 with ErrorMessage

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

the class AggregatorIntegrationTests method testZeroGroupTimeoutExpressionScheduling.

@Test
public void testZeroGroupTimeoutExpressionScheduling() throws Exception {
    try {
        this.output.purge(null);
        this.errors.purge(null);
        GenericMessage<String> message = new GenericMessage<String>("foo");
        this.output.send(message);
        this.output.send(message);
        this.output.send(message);
        this.output.send(message);
        this.output.send(message);
        this.zeroGroupTimeoutExpressionAggregatorInput.send(new GenericMessage<Integer>(1, stubHeaders(1, 2, 1)));
        ErrorMessage em = (ErrorMessage) this.errors.receive(10000);
        assertNotNull(em);
        assertThat(em.getPayload().getMessage().toLowerCase(), containsString("failed to send message to channel 'output' within timeout: 10"));
    } finally {
        this.output.purge(null);
        this.errors.purge(null);
    }
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Test(org.junit.Test)

Example 14 with ErrorMessage

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

the class TcpConnectionSupport method sendExceptionToListener.

protected final void sendExceptionToListener(Exception e) {
    if (!this.exceptionSent.getAndSet(true) && this.getListener() != null) {
        Map<String, Object> headers = Collections.singletonMap(IpHeaders.CONNECTION_ID, (Object) this.getConnectionId());
        ErrorMessage errorMessage = new ErrorMessage(e, headers);
        this.getListener().onMessage(errorMessage);
    }
}
Also used : ErrorMessage(org.springframework.messaging.support.ErrorMessage)

Example 15 with ErrorMessage

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

the class WebFluxRequestExecutingMessageHandlerTests method testReactiveConnectErrorOneWay.

@Test
public void testReactiveConnectErrorOneWay() {
    ClientHttpConnector httpConnector = new HttpHandlerConnector((request, response) -> {
        throw new RuntimeException("Intentional connection error");
    });
    WebClient webClient = WebClient.builder().clientConnector(httpConnector).build();
    String destinationUri = "http://www.springsource.org/spring-integration";
    WebFluxRequestExecutingMessageHandler reactiveHandler = new WebFluxRequestExecutingMessageHandler(destinationUri, webClient);
    reactiveHandler.setExpectReply(false);
    QueueChannel errorChannel = new QueueChannel();
    reactiveHandler.handleMessage(MessageBuilder.withPayload("hello, world").setErrorChannel(errorChannel).build());
    Message<?> errorMessage = errorChannel.receive(10000);
    assertNotNull(errorMessage);
    assertThat(errorMessage, instanceOf(ErrorMessage.class));
    Throwable throwable = (Throwable) errorMessage.getPayload();
    assertThat(throwable.getMessage(), containsString("Intentional connection error"));
}
Also used : ClientHttpConnector(org.springframework.http.client.reactive.ClientHttpConnector) QueueChannel(org.springframework.integration.channel.QueueChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) ErrorMessage(org.springframework.messaging.support.ErrorMessage) WebClient(org.springframework.web.reactive.function.client.WebClient) HttpHandlerConnector(org.springframework.test.web.reactive.server.HttpHandlerConnector) Test(org.junit.Test)

Aggregations

ErrorMessage (org.springframework.messaging.support.ErrorMessage)59 Test (org.junit.Test)47 CountDownLatch (java.util.concurrent.CountDownLatch)17 GenericMessage (org.springframework.messaging.support.GenericMessage)17 MessagingException (org.springframework.messaging.MessagingException)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)13 Message (org.springframework.messaging.Message)12 QueueChannel (org.springframework.integration.channel.QueueChannel)11 MessageHandlingException (org.springframework.messaging.MessageHandlingException)11 Socket (java.net.Socket)9 ArrayList (java.util.ArrayList)7 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)7 Semaphore (java.util.concurrent.Semaphore)6 Log (org.apache.commons.logging.Log)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 ServerSocket (java.net.ServerSocket)3