Search in sources :

Example 96 with MessagingException

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

the class NioFileLocker method lock.

@Override
public boolean lock(File fileToLock) {
    FileLock lock = this.lockCache.get(fileToLock);
    if (lock == null) {
        FileLock newLock = null;
        try {
            newLock = FileChannelCache.tryLockFor(fileToLock);
        } catch (IOException e) {
            throw new MessagingException("Failed to lock file: " + fileToLock, e);
        }
        if (newLock != null) {
            FileLock original = this.lockCache.putIfAbsent(fileToLock, newLock);
            lock = original != null ? original : newLock;
        }
    }
    return lock != null;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) FileLock(java.nio.channels.FileLock) IOException(java.io.IOException)

Example 97 with MessagingException

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

the class ExpressionEvaluatingRequestHandlerAdvice method evaluateFailureExpression.

private Object evaluateFailureExpression(Message<?> message, Exception exception) throws Exception {
    Object evalResult;
    try {
        evalResult = this.onFailureExpression.getValue(this.prepareEvaluationContextToUse(exception), message);
    } catch (Exception e) {
        evalResult = e;
        logger.error("Failure expression evaluation failed for " + message + ": " + e.getMessage());
    }
    if (this.failureChannel == null && this.failureChannelName != null && getChannelResolver() != null) {
        this.failureChannel = getChannelResolver().resolveDestination(this.failureChannelName);
    }
    if (evalResult != null && this.failureChannel != null) {
        MessagingException messagingException = new MessageHandlingExpressionEvaluatingAdviceException(message, "Handler Failed", this.unwrapThrowableIfNecessary(exception), evalResult);
        ErrorMessage resultMessage = new ErrorMessage(messagingException);
        this.messagingTemplate.send(this.failureChannel, resultMessage);
    }
    return evalResult;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException)

Example 98 with MessagingException

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

the class MessagingGatewaySupport method sendAndReceiveMessageReactive.

protected Mono<Message<?>> sendAndReceiveMessageReactive(Object object) {
    initializeIfNecessary();
    Assert.notNull(object, "request must not be null");
    MessageChannel requestChannel = getRequestChannel();
    if (requestChannel == null) {
        throw new MessagingException("No request channel available. Cannot send request message.");
    }
    registerReplyMessageCorrelatorIfNecessary();
    return doSendAndReceiveMessageReactive(requestChannel, object, false);
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) MessagingException(org.springframework.messaging.MessagingException)

Example 99 with MessagingException

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

the class AbstractMessageSource method buildMessage.

@SuppressWarnings("unchecked")
protected Message<T> buildMessage(Object result) {
    Message<T> message = null;
    Map<String, Object> headers = evaluateHeaders();
    if (result instanceof AbstractIntegrationMessageBuilder) {
        if (!CollectionUtils.isEmpty(headers)) {
            ((AbstractIntegrationMessageBuilder<T>) result).copyHeaders(headers);
        }
        message = ((AbstractIntegrationMessageBuilder<T>) result).build();
    } else if (result instanceof Message<?>) {
        try {
            message = (Message<T>) result;
        } catch (Exception e) {
            throw new MessagingException("MessageSource returned unexpected type.", e);
        }
        if (!CollectionUtils.isEmpty(headers)) {
            // create a new Message from this one in order to apply headers
            message = getMessageBuilderFactory().fromMessage(message).copyHeaders(headers).build();
        }
    } else if (result != null) {
        T payload;
        try {
            payload = (T) result;
        } catch (Exception e) {
            throw new MessagingException("MessageSource returned unexpected type.", e);
        }
        message = getMessageBuilderFactory().withPayload(payload).copyHeaders(headers).build();
    }
    if (this.countsEnabled && message != null) {
        if (this.metricsCaptor != null) {
            incrementReceiveCounter();
        }
        this.messageCount.incrementAndGet();
    }
    return message;
}
Also used : Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) AbstractIntegrationMessageBuilder(org.springframework.integration.support.AbstractIntegrationMessageBuilder) MessagingException(org.springframework.messaging.MessagingException)

Example 100 with MessagingException

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

Aggregations

MessagingException (org.springframework.messaging.MessagingException)143 Test (org.junit.Test)60 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 BeanFactory (org.springframework.beans.factory.BeanFactory)15 MessageChannel (org.springframework.messaging.MessageChannel)15 MessageHandlingException (org.springframework.messaging.MessageHandlingException)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