Search in sources :

Example 1 with ErrorMessageSendingRecoverer

use of org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer in project spring-integration by spring-projects.

the class InboundEndpointTests method testRetryWithinOnMessageGateway.

@Test
public void testRetryWithinOnMessageGateway() throws Exception {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    AbstractMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
    AmqpInboundGateway adapter = new AmqpInboundGateway(container);
    adapter.setRequestChannel(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 2 with ErrorMessageSendingRecoverer

use of org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer 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 3 with ErrorMessageSendingRecoverer

use of org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer in project spring-cloud-stream by spring-cloud.

the class AbstractMessageChannelBinder method registerErrorInfrastructure.

/**
 * Build an errorChannelRecoverer that writes to a pub/sub channel for the destination
 * when an exception is thrown to a consumer.
 * @param destination the destination.
 * @param group the group.
 * @param consumerProperties the properties.
 * @param polled true if this is for a polled consumer.
 * @return the ErrorInfrastructure which is a holder for the error channel, the recoverer and the
 * message handler that is subscribed to the channel.
 */
protected final ErrorInfrastructure registerErrorInfrastructure(ConsumerDestination destination, String group, C consumerProperties, boolean polled) {
    ErrorMessageStrategy errorMessageStrategy = getErrorMessageStrategy();
    ConfigurableListableBeanFactory beanFactory = getApplicationContext().getBeanFactory();
    String errorChannelName = errorsBaseName(destination, group, consumerProperties);
    SubscribableChannel errorChannel = null;
    if (getApplicationContext().containsBean(errorChannelName)) {
        Object errorChannelObject = getApplicationContext().getBean(errorChannelName);
        if (!(errorChannelObject instanceof SubscribableChannel)) {
            throw new IllegalStateException("Error channel '" + errorChannelName + "' must be a SubscribableChannel");
        }
        errorChannel = (SubscribableChannel) errorChannelObject;
    } else {
        errorChannel = new BinderErrorChannel();
        beanFactory.registerSingleton(errorChannelName, errorChannel);
        errorChannel = (LastSubscriberAwareChannel) beanFactory.initializeBean(errorChannel, errorChannelName);
    }
    ErrorMessageSendingRecoverer recoverer;
    if (errorMessageStrategy == null) {
        recoverer = new ErrorMessageSendingRecoverer(errorChannel);
    } else {
        recoverer = new ErrorMessageSendingRecoverer(errorChannel, errorMessageStrategy);
    }
    String recovererBeanName = getErrorRecovererName(destination, group, consumerProperties);
    beanFactory.registerSingleton(recovererBeanName, recoverer);
    beanFactory.initializeBean(recoverer, recovererBeanName);
    MessageHandler handler;
    if (polled) {
        handler = getPolledConsumerErrorMessageHandler(destination, group, consumerProperties);
    } else {
        handler = getErrorMessageHandler(destination, group, consumerProperties);
    }
    MessageChannel defaultErrorChannel = null;
    if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
        defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, MessageChannel.class);
    }
    if (handler == null && errorChannel instanceof LastSubscriberAwareChannel) {
        handler = getDefaultErrorMessageHandler((LastSubscriberAwareChannel) errorChannel, defaultErrorChannel != null);
    }
    String errorMessageHandlerName = getErrorMessageHandlerName(destination, group, consumerProperties);
    if (handler != null) {
        beanFactory.registerSingleton(errorMessageHandlerName, handler);
        beanFactory.initializeBean(handler, errorMessageHandlerName);
        errorChannel.subscribe(handler);
    }
    if (defaultErrorChannel != null) {
        BridgeHandler errorBridge = new BridgeHandler();
        errorBridge.setOutputChannel(defaultErrorChannel);
        errorChannel.subscribe(errorBridge);
        String errorBridgeHandlerName = getErrorBridgeName(destination, group, consumerProperties);
        beanFactory.registerSingleton(errorBridgeHandlerName, errorBridge);
        beanFactory.initializeBean(errorBridge, errorBridgeHandlerName);
    }
    return new ErrorInfrastructure(errorChannel, recoverer, handler);
}
Also used : AbstractMessageHandler(org.springframework.integration.handler.AbstractMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ErrorMessageStrategy(org.springframework.integration.support.ErrorMessageStrategy) BridgeHandler(org.springframework.integration.handler.BridgeHandler) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessageSendingRecoverer(org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory)

Aggregations

ErrorMessageSendingRecoverer (org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer)3 Test (org.junit.Test)2 MessageProperties (org.springframework.amqp.core.MessageProperties)2 ConnectionFactory (org.springframework.amqp.rabbit.connection.ConnectionFactory)2 AbstractMessageListenerContainer (org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer)2 SimpleMessageListenerContainer (org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer)2 ChannelAwareMessageListener (org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener)2 AmqpMessageHeaderErrorMessageStrategy (org.springframework.integration.amqp.support.AmqpMessageHeaderErrorMessageStrategy)2 DirectChannel (org.springframework.integration.channel.DirectChannel)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 MessagingException (org.springframework.messaging.MessagingException)2 RetryTemplate (org.springframework.retry.support.RetryTemplate)2 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 AbstractMessageChannel (org.springframework.integration.channel.AbstractMessageChannel)1 AbstractMessageHandler (org.springframework.integration.handler.AbstractMessageHandler)1 BridgeHandler (org.springframework.integration.handler.BridgeHandler)1 ErrorMessageStrategy (org.springframework.integration.support.ErrorMessageStrategy)1 MessageChannel (org.springframework.messaging.MessageChannel)1 MessageHandler (org.springframework.messaging.MessageHandler)1 SubscribableChannel (org.springframework.messaging.SubscribableChannel)1