Search in sources :

Example 6 with MessagingException

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

the class AbstractPollingEndpoint method doPoll.

private boolean doPoll() {
    IntegrationResourceHolder holder = this.bindResourceHolderIfNecessary(this.getResourceKey(), this.getResourceToBind());
    Message<?> message = null;
    try {
        message = this.receiveMessage();
    } catch (Exception e) {
        if (Thread.interrupted()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Poll interrupted - during stop()? : " + e.getMessage());
            }
            return false;
        } else {
            throw (RuntimeException) e;
        }
    }
    boolean result;
    if (message == null) {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Received no Message during the poll, returning 'false'");
        }
        result = false;
    } else {
        if (this.logger.isDebugEnabled()) {
            this.logger.debug("Poll resulted in Message: " + message);
        }
        if (holder != null) {
            holder.setMessage(message);
        }
        try {
            this.handleMessage(message);
        } catch (Exception e) {
            if (e instanceof MessagingException) {
                throw new MessagingExceptionWrapper(message, (MessagingException) e);
            } else {
                throw new MessagingException(message, e);
            }
        }
        result = true;
    }
    return result;
}
Also used : MessagingException(org.springframework.messaging.MessagingException) IntegrationResourceHolder(org.springframework.integration.transaction.IntegrationResourceHolder) MessagingExceptionWrapper(org.springframework.integration.support.MessagingExceptionWrapper) MessagingException(org.springframework.messaging.MessagingException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

Example 7 with MessagingException

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

the class MessagingGatewaySupport method registerReplyMessageCorrelatorIfNecessary.

protected void registerReplyMessageCorrelatorIfNecessary() {
    MessageChannel replyChannel = getReplyChannel();
    if (replyChannel != null && this.replyMessageCorrelator == null) {
        boolean shouldStartCorrelator;
        synchronized (this.replyMessageCorrelatorMonitor) {
            if (this.replyMessageCorrelator != null) {
                return;
            }
            AbstractEndpoint correlator;
            BridgeHandler handler = new BridgeHandler();
            if (getBeanFactory() != null) {
                handler.setBeanFactory(getBeanFactory());
            }
            handler.afterPropertiesSet();
            if (replyChannel instanceof SubscribableChannel) {
                correlator = new EventDrivenConsumer((SubscribableChannel) replyChannel, handler);
            } else if (replyChannel instanceof PollableChannel) {
                PollingConsumer endpoint = new PollingConsumer((PollableChannel) replyChannel, handler);
                endpoint.setBeanFactory(getBeanFactory());
                endpoint.setReceiveTimeout(this.replyTimeout);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else if (replyChannel instanceof ReactiveStreamsSubscribableChannel) {
                ReactiveStreamsConsumer endpoint = new ReactiveStreamsConsumer(replyChannel, (Subscriber<Message<?>>) handler);
                endpoint.afterPropertiesSet();
                correlator = endpoint;
            } else {
                throw new MessagingException("Unsupported 'replyChannel' type [" + replyChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
            }
            this.replyMessageCorrelator = correlator;
            shouldStartCorrelator = true;
        }
        if (shouldStartCorrelator && isRunning()) {
            if (isRunning()) {
                this.replyMessageCorrelator.start();
            }
        }
    }
}
Also used : AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) BridgeHandler(org.springframework.integration.handler.BridgeHandler) MessagingException(org.springframework.messaging.MessagingException) PollableChannel(org.springframework.messaging.PollableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ReactiveStreamsSubscribableChannel(org.springframework.integration.channel.ReactiveStreamsSubscribableChannel)

Example 8 with MessagingException

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

the class MessagingGatewaySupport method doSendAndReceive.

@SuppressWarnings("unchecked")
private Object doSendAndReceive(Object object, boolean shouldConvert) {
    this.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();
    Object reply = null;
    Throwable error = null;
    Message<?> requestMessage = null;
    try {
        if (this.countsEnabled) {
            this.messageCount.incrementAndGet();
        }
        if (shouldConvert) {
            reply = this.messagingTemplate.convertSendAndReceive(requestChannel, object, Object.class, this.historyWritingPostProcessor);
            if (reply instanceof Throwable) {
                error = (Throwable) reply;
            }
        } else {
            requestMessage = (object instanceof Message<?>) ? (Message<?>) object : this.requestMapper.toMessage(object);
            requestMessage = this.historyWritingPostProcessor.postProcessMessage(requestMessage);
            reply = this.messagingTemplate.sendAndReceive(requestChannel, requestMessage);
            if (reply instanceof ErrorMessage) {
                error = ((ErrorMessage) reply).getPayload();
            }
        }
        if (reply == null && this.errorOnTimeout) {
            if (object instanceof Message) {
                error = new MessageTimeoutException((Message<?>) object, "No reply received within timeout");
            } else {
                error = new MessageTimeoutException("No reply received within timeout");
            }
        }
    } catch (Exception e) {
        if (logger.isDebugEnabled()) {
            logger.debug("failure occurred in gateway sendAndReceive: " + e.getMessage());
        }
        error = e;
    }
    if (error != null) {
        MessageChannel errorChannel = getErrorChannel();
        if (errorChannel != null) {
            ErrorMessage errorMessage = buildErrorMessage(requestMessage, error);
            Message<?> errorFlowReply = null;
            try {
                errorFlowReply = this.messagingTemplate.sendAndReceive(errorChannel, errorMessage);
            } catch (Exception errorFlowFailure) {
                throw new MessagingException(errorMessage, "failure occurred in error-handling flow", errorFlowFailure);
            }
            if (shouldConvert) {
                Object result = (errorFlowReply != null) ? errorFlowReply.getPayload() : null;
                if (result instanceof Throwable) {
                    this.rethrow((Throwable) result, "error flow returned Exception");
                }
                return result;
            }
            if (errorFlowReply != null && errorFlowReply.getPayload() instanceof Throwable) {
                this.rethrow((Throwable) errorFlowReply.getPayload(), "error flow returned an Error Message");
            }
            if (errorFlowReply == null && this.errorOnTimeout) {
                if (object instanceof Message) {
                    throw new MessageTimeoutException((Message<?>) object, "No reply received from error channel within timeout");
                } else {
                    throw new MessageTimeoutException("No reply received from error channel within timeout");
                }
            }
            return errorFlowReply;
        } else {
            // no errorChannel so we'll propagate
            this.rethrow(error, "gateway received checked Exception");
        }
    }
    return reply;
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) ErrorMessage(org.springframework.messaging.support.ErrorMessage) Message(org.springframework.messaging.Message) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MessagingException(org.springframework.messaging.MessagingException) MessageTimeoutException(org.springframework.integration.MessageTimeoutException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageMappingException(org.springframework.integration.mapping.MessageMappingException)

Example 9 with MessagingException

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

the class SourcePollingChannelAdapter method handleMessage.

@Override
protected void handleMessage(Message<?> message) {
    if (this.shouldTrack) {
        message = MessageHistory.write(message, this, getMessageBuilderFactory());
    }
    AcknowledgmentCallback ackCallback = StaticMessageHeaderAccessor.getAcknowledgmentCallback(message);
    try {
        this.messagingTemplate.send(getOutputChannel(), message);
        AckUtils.autoAck(ackCallback);
    } catch (Exception e) {
        AckUtils.autoNack(ackCallback);
        if (e instanceof MessagingException) {
            throw (MessagingException) e;
        } else {
            throw new MessagingException(message, "Failed to send Message", e);
        }
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessagingException(org.springframework.messaging.MessagingException) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 10 with MessagingException

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

the class HeaderEnricher method transform.

@Override
public Message<?> transform(Message<?> message) {
    try {
        Map<String, Object> headerMap = new HashMap<String, Object>(message.getHeaders());
        this.addHeadersFromMessageProcessor(message, headerMap);
        for (Map.Entry<String, ? extends HeaderValueMessageProcessor<?>> entry : this.headersToAdd.entrySet()) {
            String key = entry.getKey();
            HeaderValueMessageProcessor<?> valueProcessor = entry.getValue();
            Boolean shouldOverwrite = valueProcessor.isOverwrite();
            if (shouldOverwrite == null) {
                shouldOverwrite = this.defaultOverwrite;
            }
            boolean headerDoesNotExist = headerMap.get(key) == null;
            /*
				 * Only evaluate value expression if necessary
				 */
            if (headerDoesNotExist || shouldOverwrite) {
                Object value = valueProcessor.processMessage(message);
                if (value != null || !this.shouldSkipNulls) {
                    headerMap.put(key, value);
                }
            }
        }
        return this.getMessageBuilderFactory().withPayload(message.getPayload()).copyHeaders(headerMap).build();
    } catch (Exception e) {
        throw new MessagingException(message, "failed to transform message headers", e);
    }
}
Also used : HashMap(java.util.HashMap) MessagingException(org.springframework.messaging.MessagingException) HashMap(java.util.HashMap) Map(java.util.Map) MessagingException(org.springframework.messaging.MessagingException) BeanInitializationException(org.springframework.beans.factory.BeanInitializationException)

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