Search in sources :

Example 1 with MessageRedeliveredException

use of org.mule.runtime.core.privileged.exception.MessageRedeliveredException in project mule by mulesoft.

the class IdempotentRedeliveryPolicy method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    Optional<Exception> exceptionSeen = empty();
    String messageId = null;
    try {
        messageId = getIdForEvent(event);
    } catch (ExpressionRuntimeException e) {
        logger.warn("The message cannot be processed because the digest could not be generated. Either make the payload serializable or use an expression.");
        return null;
    } catch (Exception ex) {
        exceptionSeen = of(ex);
    }
    Lock lock = lockFactory.createLock(idrId + "-" + messageId);
    lock.lock();
    try {
        RedeliveryCounter counter = findCounter(messageId);
        if (exceptionSeen.isPresent()) {
            throw new MessageRedeliveredException(messageId, counter.counter.get(), maxRedeliveryCount, exceptionSeen.get());
        } else if (counter != null && counter.counter.get() > maxRedeliveryCount) {
            throw new MessageRedeliveredException(messageId, counter.errors, counter.counter.get(), maxRedeliveryCount);
        }
        try {
            CoreEvent returnEvent = processNext(CoreEvent.builder(DefaultEventContext.child((BaseEventContext) event.getContext(), empty()), event).build());
            counter = findCounter(messageId);
            if (counter != null) {
                resetCounter(messageId);
            }
            return returnEvent;
        } catch (Exception ex) {
            if (ex instanceof MessagingException) {
                incrementCounter(messageId, (MessagingException) ex);
                throw ex;
            } else {
                MessagingException me = createMessagingException(event, ex, this);
                incrementCounter(messageId, me);
                throw ex;
            }
        }
    } finally {
        lock.unlock();
    }
}
Also used : MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) ExpressionRuntimeException(org.mule.runtime.core.api.expression.ExpressionRuntimeException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) MessageRedeliveredException(org.mule.runtime.core.privileged.exception.MessageRedeliveredException) Lock(java.util.concurrent.locks.Lock)

Aggregations

Lock (java.util.concurrent.locks.Lock)1 MuleException (org.mule.runtime.api.exception.MuleException)1 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)1 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 ExpressionRuntimeException (org.mule.runtime.core.api.expression.ExpressionRuntimeException)1 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)1 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)1 MessageRedeliveredException (org.mule.runtime.core.privileged.exception.MessageRedeliveredException)1