Search in sources :

Example 31 with MessagingException

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

the class RequestHandlerRetryAdvice method doInvoke.

@Override
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message) throws Exception {
    RetryState retryState = null;
    retryState = this.retryStateGenerator.determineRetryState(message);
    messageHolder.set(message);
    try {
        return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
    } catch (MessagingException e) {
        if (e.getFailedMessage() == null) {
            throw new MessagingException(message, "Failed to invoke handler", e);
        }
        throw e;
    } catch (Exception e) {
        throw new MessagingException(message, "Failed to invoke handler", unwrapExceptionIfNecessary(e));
    } finally {
        messageHolder.remove();
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) MessagingException(org.springframework.messaging.MessagingException) RetryState(org.springframework.retry.RetryState)

Example 32 with MessagingException

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

the class ScatterGatherHandler method doInit.

@Override
protected void doInit() {
    if (this.gatherChannel == null) {
        this.gatherChannel = new FixedSubscriberChannel(this.gatherer);
    } else {
        if (this.gatherChannel instanceof SubscribableChannel) {
            this.gatherEndpoint = new EventDrivenConsumer((SubscribableChannel) this.gatherChannel, this.gatherer);
        } else if (this.gatherChannel instanceof PollableChannel) {
            this.gatherEndpoint = new PollingConsumer((PollableChannel) this.gatherChannel, this.gatherer);
            ((PollingConsumer) this.gatherEndpoint).setReceiveTimeout(this.gatherTimeout);
        } else {
            throw new MessagingException("Unsupported 'replyChannel' type [" + this.gatherChannel.getClass() + "]." + "SubscribableChannel or PollableChannel type are supported.");
        }
        this.gatherEndpoint.setBeanFactory(this.getBeanFactory());
        this.gatherEndpoint.afterPropertiesSet();
    }
    ((MessageProducer) this.gatherer).setOutputChannel(new FixedSubscriberChannel(message -> {
        MessageHeaders headers = message.getHeaders();
        if (headers.containsKey(GATHER_RESULT_CHANNEL)) {
            Object gatherResultChannel = headers.get(GATHER_RESULT_CHANNEL);
            if (gatherResultChannel instanceof MessageChannel) {
                messagingTemplate.send((MessageChannel) gatherResultChannel, message);
            } else if (gatherResultChannel instanceof String) {
                messagingTemplate.send((String) gatherResultChannel, message);
            }
        } else {
            throw new MessageDeliveryException(message, "The 'gatherResultChannel' header is required to delivery gather result.");
        }
    }));
    this.replyChannelRegistry = getBeanFactory().getBean(IntegrationContextUtils.INTEGRATION_HEADER_CHANNEL_REGISTRY_BEAN_NAME, HeaderChannelRegistry.class);
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MessagingException(org.springframework.messaging.MessagingException) AbstractEndpoint(org.springframework.integration.endpoint.AbstractEndpoint) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) ClassUtils(org.springframework.util.ClassUtils) AopUtils(org.springframework.aop.support.AopUtils) AbstractReplyProducingMessageHandler(org.springframework.integration.handler.AbstractReplyProducingMessageHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessageChannel(org.springframework.messaging.MessageChannel) MessageHeaders(org.springframework.messaging.MessageHeaders) IntegrationContextUtils(org.springframework.integration.context.IntegrationContextUtils) Lifecycle(org.springframework.context.Lifecycle) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageHandler(org.springframework.messaging.MessageHandler) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel) Message(org.springframework.messaging.Message) PollableChannel(org.springframework.messaging.PollableChannel) Assert(org.springframework.util.Assert) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) PollingConsumer(org.springframework.integration.endpoint.PollingConsumer) MessagingException(org.springframework.messaging.MessagingException) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) HeaderChannelRegistry(org.springframework.integration.support.channel.HeaderChannelRegistry) MessageChannel(org.springframework.messaging.MessageChannel) PollableChannel(org.springframework.messaging.PollableChannel) MessageProducer(org.springframework.integration.core.MessageProducer) MessageHeaders(org.springframework.messaging.MessageHeaders) SubscribableChannel(org.springframework.messaging.SubscribableChannel) FixedSubscriberChannel(org.springframework.integration.channel.FixedSubscriberChannel)

Example 33 with MessagingException

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

the class SimpleMessageStore method removeMessagesFromGroup.

@Override
public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messages) {
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        try {
            MessageGroup group = this.groupIdToMessageGroup.get(groupId);
            Assert.notNull(group, "MessageGroup for groupId '" + groupId + "' " + "can not be located while attempting to remove Message(s) from the MessageGroup");
            UpperBound upperBound = this.groupToUpperBound.get(groupId);
            Assert.state(upperBound != null, "'upperBound' must not be null.");
            boolean modified = false;
            for (Message<?> messageToRemove : messages) {
                if (group.remove(messageToRemove)) {
                    upperBound.release();
                    modified = true;
                }
            }
            if (modified) {
                group.setLastModified(System.currentTimeMillis());
            }
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Example 34 with MessagingException

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

the class SimpleMessageStore method addMessagesToGroup.

@Override
public void addMessagesToGroup(Object groupId, Message<?>... messages) {
    Assert.notNull(groupId, "'groupId' must not be null");
    Assert.notNull(messages, "'messages' must not be null");
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        boolean unlocked = false;
        try {
            UpperBound upperBound;
            MessageGroup group = this.groupIdToMessageGroup.get(groupId);
            MessagingException outOfCapacityException = new MessagingException(getClass().getSimpleName() + " was out of capacity (" + this.groupCapacity + ") for group '" + groupId + "', try constructing it with a larger capacity.");
            if (group == null) {
                if (this.groupCapacity > 0 && messages.length > this.groupCapacity) {
                    throw outOfCapacityException;
                }
                group = getMessageGroupFactory().create(groupId);
                this.groupIdToMessageGroup.put(groupId, group);
                upperBound = new UpperBound(this.groupCapacity);
                for (Message<?> message : messages) {
                    upperBound.tryAcquire(-1);
                    group.add(message);
                }
                this.groupToUpperBound.put(groupId, upperBound);
            } else {
                upperBound = this.groupToUpperBound.get(groupId);
                Assert.state(upperBound != null, "'upperBound' must not be null.");
                for (Message<?> message : messages) {
                    lock.unlock();
                    if (!upperBound.tryAcquire(this.upperBoundTimeout)) {
                        unlocked = true;
                        throw outOfCapacityException;
                    }
                    lock.lockInterruptibly();
                    group.add(message);
                }
            }
            group.setLastModified(System.currentTimeMillis());
        } finally {
            if (!unlocked) {
                lock.unlock();
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Example 35 with MessagingException

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

the class SimpleMessageStore method removeMessageGroup.

@Override
public void removeMessageGroup(Object groupId) {
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        try {
            MessageGroup messageGroup = this.groupIdToMessageGroup.remove(groupId);
            if (messageGroup != null) {
                UpperBound upperBound = this.groupToUpperBound.remove(groupId);
                Assert.state(upperBound != null, "'upperBound' must not be null.");
                upperBound.release(this.groupCapacity);
            }
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

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