Search in sources :

Example 31 with MessageHandlingException

use of org.springframework.messaging.MessageHandlingException in project spring-cloud-stream by spring-cloud.

the class DefaultPollableMessageSource method poll.

@Override
public boolean poll(MessageHandler handler, ParameterizedTypeReference<?> type) {
    Message<?> message = this.receive(type);
    if (message == null) {
        return false;
    }
    AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
    try {
        if (this.retryTemplate == null) {
            if (this.errorChannel == null) {
                this.handle(message, handler);
            } else {
                try {
                    this.handle(message, handler);
                } catch (Exception e) {
                    this.messagingTemplate.send(this.errorChannel, this.errorMessageStrategy.buildErrorMessage(e, attributesHolder.get()));
                }
            }
        } else {
            this.retryTemplate.execute(context -> {
                this.handle(message, handler);
                return null;
            }, this.recoveryCallback);
        }
        return true;
    } catch (Exception e) {
        AckUtils.autoNack(ackCallback);
        if (e instanceof MessageHandlingException && ((MessageHandlingException) e).getFailedMessage().equals(message)) {
            throw (MessageHandlingException) e;
        }
        throw new MessageHandlingException(message, e);
    } finally {
        AckUtils.autoAck(ackCallback);
    }
}
Also used : AcknowledgmentCallback(org.springframework.integration.support.AcknowledgmentCallback) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 32 with MessageHandlingException

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

the class EnricherParserTests3 method testTargetBeanResolver.

@Test
public void testTargetBeanResolver() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(this.getClass().getSimpleName() + "-fail-context.xml", this.getClass());
    MessageChannel beanResolveIn = context.getBean("beanResolveIn", MessageChannel.class);
    SomeBean payload = new SomeBean("foo");
    assertEquals("foo", payload.getNested().getValue());
    try {
        beanResolveIn.send(new GenericMessage<SomeBean>(payload));
        fail("Expected SpEL Exception");
    } catch (MessageHandlingException e) {
        assertTrue(e.getCause() instanceof SpelEvaluationException);
    }
    context.close();
}
Also used : SpelEvaluationException(org.springframework.expression.spel.SpelEvaluationException) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Test(org.junit.Test)

Example 33 with MessageHandlingException

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

the class MessageSourcePollingTemplate method poll.

@Override
public boolean poll(MessageHandler handler) {
    Assert.notNull(handler, "'handler' cannot be null");
    Message<?> message = this.source.receive();
    if (message != null) {
        AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
        try {
            handler.handleMessage(message);
            AckUtils.autoAck(ackCallback);
        } catch (Exception e) {
            AckUtils.autoNack(ackCallback);
            throw new MessageHandlingException(message, e);
        }
        return true;
    }
    return false;
}
Also used : AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 34 with MessageHandlingException

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

the class AbstractMessageProducingHandler method produceOutput.

protected void produceOutput(Object reply, final Message<?> requestMessage) {
    final MessageHeaders requestHeaders = requestMessage.getHeaders();
    Object replyChannel = null;
    if (getOutputChannel() == null) {
        Map<?, ?> routingSlipHeader = requestHeaders.get(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Map.class);
        if (routingSlipHeader != null) {
            Assert.isTrue(routingSlipHeader.size() == 1, "The RoutingSlip header value must be a SingletonMap");
            Object key = routingSlipHeader.keySet().iterator().next();
            Object value = routingSlipHeader.values().iterator().next();
            Assert.isInstanceOf(List.class, key, "The RoutingSlip key must be List");
            Assert.isInstanceOf(Integer.class, value, "The RoutingSlip value must be Integer");
            List<?> routingSlip = (List<?>) key;
            AtomicInteger routingSlipIndex = new AtomicInteger((Integer) value);
            replyChannel = getOutputChannelFromRoutingSlip(reply, requestMessage, routingSlip, routingSlipIndex);
            if (replyChannel != null) {
                // TODO Migrate to the SF MessageBuilder
                AbstractIntegrationMessageBuilder<?> builder = null;
                if (reply instanceof Message) {
                    builder = this.getMessageBuilderFactory().fromMessage((Message<?>) reply);
                } else if (reply instanceof AbstractIntegrationMessageBuilder) {
                    builder = (AbstractIntegrationMessageBuilder<?>) reply;
                } else {
                    builder = this.getMessageBuilderFactory().withPayload(reply);
                }
                builder.setHeader(IntegrationMessageHeaderAccessor.ROUTING_SLIP, Collections.singletonMap(routingSlip, routingSlipIndex.get()));
                reply = builder;
            }
        }
        if (replyChannel == null) {
            replyChannel = requestHeaders.getReplyChannel();
            if (replyChannel == null && reply instanceof Message) {
                replyChannel = ((Message<?>) reply).getHeaders().getReplyChannel();
            }
        }
    }
    if (this.async && (reply instanceof ListenableFuture<?> || reply instanceof Publisher<?>)) {
        if (reply instanceof ListenableFuture<?> || !(getOutputChannel() instanceof ReactiveStreamsSubscribableChannel)) {
            ListenableFuture<?> future;
            if (reply instanceof ListenableFuture<?>) {
                future = (ListenableFuture<?>) reply;
            } else {
                SettableListenableFuture<Object> settableListenableFuture = new SettableListenableFuture<>();
                Mono.from((Publisher<?>) reply).subscribe(settableListenableFuture::set, settableListenableFuture::setException);
                future = settableListenableFuture;
            }
            Object theReplyChannel = replyChannel;
            future.addCallback(new ListenableFutureCallback<Object>() {

                @Override
                public void onSuccess(Object result) {
                    Message<?> replyMessage = null;
                    try {
                        replyMessage = createOutputMessage(result, requestHeaders);
                        sendOutput(replyMessage, theReplyChannel, false);
                    } catch (Exception e) {
                        Exception exceptionToLogAndSend = e;
                        if (!(e instanceof MessagingException)) {
                            exceptionToLogAndSend = new MessageHandlingException(requestMessage, e);
                            if (replyMessage != null) {
                                exceptionToLogAndSend = new MessagingException(replyMessage, exceptionToLogAndSend);
                            }
                        }
                        logger.error("Failed to send async reply: " + result.toString(), exceptionToLogAndSend);
                        onFailure(exceptionToLogAndSend);
                    }
                }

                @Override
                public void onFailure(Throwable ex) {
                    sendErrorMessage(requestMessage, ex);
                }
            });
        } else {
            ((ReactiveStreamsSubscribableChannel) getOutputChannel()).subscribeTo(Flux.from((Publisher<?>) reply).map(result -> createOutputMessage(result, requestHeaders)));
        }
    } else {
        sendOutput(createOutputMessage(reply, requestHeaders), replyChannel, false);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) Arrays(java.util.Arrays) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) HashMap(java.util.HashMap) ErrorMessage(org.springframework.messaging.support.ErrorMessage) ListenableFutureCallback(org.springframework.util.concurrent.ListenableFutureCallback) MessagingTemplate(org.springframework.integration.core.MessagingTemplate) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageHandlingException(org.springframework.messaging.MessageHandlingException) Map(java.util.Map) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) RoutingSlipRouteStrategy(org.springframework.integration.routingslip.RoutingSlipRouteStrategy) Message(org.springframework.messaging.Message) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) Collection(java.util.Collection) Publisher(org.reactivestreams.Publisher) ObjectUtils(org.springframework.util.ObjectUtils) PatternMatchUtils(org.springframework.util.PatternMatchUtils) Set(java.util.Set) Mono(reactor.core.publisher.Mono) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageProducer(org.springframework.integration.core.MessageProducer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) Flux(reactor.core.publisher.Flux) List(java.util.List) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) Publisher(org.reactivestreams.Publisher) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) ListenableFuture(org.springframework.util.concurrent.ListenableFuture) SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) List(java.util.List) MessageHeaders(org.springframework.messaging.MessageHeaders)

Example 35 with MessageHandlingException

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

the class AbstractMessageProducingHandler method sendErrorMessage.

protected void sendErrorMessage(final Message<?> requestMessage, Throwable ex) {
    Object errorChannel = resolveErrorChannel(requestMessage.getHeaders());
    Throwable result = ex;
    if (!(ex instanceof MessagingException)) {
        result = new MessageHandlingException(requestMessage, ex);
    }
    if (errorChannel == null) {
        logger.error("Async exception received and no 'errorChannel' header exists and no default " + "'errorChannel' found", result);
    } else {
        try {
            sendOutput(new ErrorMessage(result), errorChannel, true);
        } catch (Exception e) {
            Exception exceptionToLog = e;
            if (!(e instanceof MessagingException)) {
                exceptionToLog = new MessageHandlingException(requestMessage, e);
            }
            logger.error("Failed to send async reply", exceptionToLog);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessagingException(org.springframework.messaging.MessagingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException)

Aggregations

MessageHandlingException (org.springframework.messaging.MessageHandlingException)66 Test (org.junit.Test)34 ErrorMessage (org.springframework.messaging.support.ErrorMessage)14 IOException (java.io.IOException)12 GenericMessage (org.springframework.messaging.support.GenericMessage)12 Message (org.springframework.messaging.Message)10 MessagingException (org.springframework.messaging.MessagingException)10 File (java.io.File)9 Matchers.containsString (org.hamcrest.Matchers.containsString)6 BeanFactory (org.springframework.beans.factory.BeanFactory)6 FileNotFoundException (java.io.FileNotFoundException)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 MessageRejectedException (org.springframework.integration.MessageRejectedException)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)4 FileOutputStream (java.io.FileOutputStream)3 OutputStream (java.io.OutputStream)3 IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)3