Search in sources :

Example 1 with RoutingNotification

use of org.mule.runtime.api.notification.RoutingNotification in project mule by mulesoft.

the class EventCorrelator method process.

public CoreEvent process(CoreEvent event) throws RoutingException {
    // the correlationId of the event's message
    final String groupId = event.getCorrelationId();
    if (logger.isTraceEnabled()) {
        try {
            logger.trace(format("Received async reply message for correlationID: %s%n%s%n%s", groupId, truncate(StringMessageUtils.toString(event.getMessage().getPayload().getValue()), 200, false), event.getMessage().toString()));
        } catch (Exception e) {
        // ignore
        }
    }
    // spinloop for the EventGroup lookup
    while (true) {
        try {
            if (isGroupAlreadyProcessed(groupId)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("An event was received for an event group that has already been processed, " + "this is probably because the async-reply timed out. GroupCorrelation Id is: " + groupId + ". Dropping event");
                }
                // Fire a notification to say we received this message
                notificationFirer.dispatch(new RoutingNotification(event.getMessage(), event.getContext().getOriginatingLocation().getComponentIdentifier().getIdentifier().getNamespace(), MISSED_AGGREGATION_GROUP_EVENT));
                return null;
            }
        } catch (ObjectStoreException e) {
            throw new RoutingException(timeoutMessageProcessor, e);
        }
        // check for an existing group first
        EventGroup group;
        try {
            group = this.getEventGroup(groupId);
        } catch (ObjectStoreException e) {
            throw new RoutingException(timeoutMessageProcessor, e);
        }
        // does the group exist?
        if (group == null) {
            // ..apparently not, so create a new one & add it
            try {
                EventGroup eventGroup = callback.createEventGroup(event, groupId);
                eventGroup.initEventsStore(correlatorStore);
                group = this.addEventGroup(eventGroup);
            } catch (ObjectStoreException e) {
                throw new RoutingException(timeoutMessageProcessor, e);
            }
        }
        // ensure that only one thread at a time evaluates this EventGroup
        synchronized (groupsLock) {
            if (logger.isDebugEnabled()) {
                logger.debug("Adding event to aggregator group: " + groupId);
            }
            // add the incoming event to the group
            try {
                group.addEvent(event);
            } catch (ObjectStoreException e) {
                throw new RoutingException(timeoutMessageProcessor, e);
            }
            // check to see if the event group is ready to be aggregated
            if (callback.shouldAggregateEvents(group)) {
                // create the response event
                CoreEvent returnEvent = null;
                try {
                    returnEvent = callback.aggregateEvents(group);
                } catch (RoutingException routingException) {
                    try {
                        this.removeEventGroup(group);
                        group.clear();
                    } catch (ObjectStoreException objectStoreException) {
                        throw new RoutingException(timeoutMessageProcessor, objectStoreException);
                    }
                    throw routingException;
                }
                // for this group once we aggregate
                try {
                    this.removeEventGroup(group);
                    group.clear();
                } catch (ObjectStoreException e) {
                    throw new RoutingException(timeoutMessageProcessor, e);
                }
                return returnEvent;
            } else {
                return null;
            }
        }
    }
}
Also used : ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RoutingNotification(org.mule.runtime.api.notification.RoutingNotification) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) ObjectDoesNotExistException(org.mule.runtime.api.store.ObjectDoesNotExistException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) ObjectAlreadyExistsException(org.mule.runtime.api.store.ObjectAlreadyExistsException) EventGroup(org.mule.runtime.core.internal.routing.EventGroup)

Example 2 with RoutingNotification

use of org.mule.runtime.api.notification.RoutingNotification in project mule by mulesoft.

the class EventCorrelator method handleGroupExpiry.

protected void handleGroupExpiry(EventGroup group) throws MuleException {
    try {
        removeEventGroup(group);
    } catch (ObjectStoreException e) {
        throw new DefaultMuleException(e);
    }
    if (isFailOnTimeout()) {
        CoreEvent messageCollectionEvent = group.getMessageCollectionEvent();
        notificationFirer.dispatch(new RoutingNotification(messageCollectionEvent.getMessage(), null, CORRELATION_TIMEOUT));
        try {
            group.clear();
        } catch (ObjectStoreException e) {
            logger.warn("Failed to clear group with id " + group.getGroupId() + " since underlying ObjectStore threw Exception:" + e.getMessage());
        }
        throw new CorrelationTimeoutException(correlationTimedOut(group.getGroupId()));
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug(MessageFormat.format("Aggregator expired, but ''failOnTimeOut'' is false. Forwarding {0} events out of {1} " + "total for group ID: {2}", group.size(), group.expectedSize().map(v -> v.toString()).orElse(NOT_SET), group.getGroupId()));
        }
        try {
            if (!(group.getCreated() + DAYS.toMillis(1) < currentTimeMillis())) {
                CoreEvent newEvent = CoreEvent.builder(callback.aggregateEvents(group)).build();
                group.clear();
                if (!correlatorStore.contains((String) group.getGroupId(), getExpiredAndDispatchedPartitionKey())) {
                    // returned?
                    if (timeoutMessageProcessor != null) {
                        processToApply(newEvent, timeoutMessageProcessor, false, empty());
                    } else {
                        throw new MessagingException(createStaticMessage(MessageFormat.format("Group {0} timed out, but no timeout message processor was " + "configured.", group.getGroupId())), newEvent);
                    }
                    correlatorStore.store((String) group.getGroupId(), group.getCreated(), getExpiredAndDispatchedPartitionKey());
                } else {
                    logger.warn(MessageFormat.format("Discarding group {0}", group.getGroupId()));
                }
            }
        } catch (MessagingException me) {
            throw me;
        } catch (Exception e) {
            throw new MessagingException(group.getMessageCollectionEvent(), e);
        }
    }
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) NOT_SET(org.mule.runtime.core.api.message.GroupCorrelation.NOT_SET) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) System.currentTimeMillis(java.lang.System.currentTimeMillis) DeserializationPostInitialisable(org.mule.runtime.core.privileged.store.DeserializationPostInitialisable) LoggerFactory(org.slf4j.LoggerFactory) MINUTES(java.util.concurrent.TimeUnit.MINUTES) PartitionableObjectStore(org.mule.runtime.api.store.PartitionableObjectStore) Expirable(org.mule.runtime.core.privileged.util.monitor.Expirable) Processor(org.mule.runtime.core.api.processor.Processor) ObjectDoesNotExistException(org.mule.runtime.api.store.ObjectDoesNotExistException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) Scheduler(org.mule.runtime.api.scheduler.Scheduler) DAYS(java.util.concurrent.TimeUnit.DAYS) CORRELATION_TIMEOUT(org.mule.runtime.api.notification.RoutingNotification.CORRELATION_TIMEOUT) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) Mono.empty(reactor.core.publisher.Mono.empty) ExpiryMonitor(org.mule.runtime.core.privileged.util.monitor.ExpiryMonitor) RoutingNotification(org.mule.runtime.api.notification.RoutingNotification) Disposable(org.mule.runtime.api.lifecycle.Disposable) Startable(org.mule.runtime.api.lifecycle.Startable) MISSED_AGGREGATION_GROUP_EVENT(org.mule.runtime.api.notification.RoutingNotification.MISSED_AGGREGATION_GROUP_EVENT) Logger(org.slf4j.Logger) MessageProcessors.processToApply(org.mule.runtime.core.privileged.processor.MessageProcessors.processToApply) ObjectStore(org.mule.runtime.api.store.ObjectStore) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) StringMessageUtils.truncate(org.mule.runtime.core.api.util.StringMessageUtils.truncate) StringMessageUtils(org.mule.runtime.core.api.util.StringMessageUtils) String.format(java.lang.String.format) EventGroup(org.mule.runtime.core.internal.routing.EventGroup) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) List(java.util.List) Stoppable(org.mule.runtime.api.lifecycle.Stoppable) CoreMessages.objectIsNull(org.mule.runtime.core.api.config.i18n.CoreMessages.objectIsNull) CoreMessages.correlationTimedOut(org.mule.runtime.core.api.config.i18n.CoreMessages.correlationTimedOut) FlowConstruct(org.mule.runtime.core.api.construct.FlowConstruct) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) ObjectAlreadyExistsException(org.mule.runtime.api.store.ObjectAlreadyExistsException) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) RoutingNotification(org.mule.runtime.api.notification.RoutingNotification) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) ObjectDoesNotExistException(org.mule.runtime.api.store.ObjectDoesNotExistException) MuleException(org.mule.runtime.api.exception.MuleException) MessagingException(org.mule.runtime.core.internal.exception.MessagingException) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) RoutingException(org.mule.runtime.core.privileged.routing.RoutingException) ObjectAlreadyExistsException(org.mule.runtime.api.store.ObjectAlreadyExistsException)

Example 3 with RoutingNotification

use of org.mule.runtime.api.notification.RoutingNotification in project mule by mulesoft.

the class AbstractAsyncRequestReplyRequester method receiveAsyncReply.

private PrivilegedEvent receiveAsyncReply(CoreEvent event) throws MuleException {
    String asyncReplyCorrelationId = getAsyncReplyCorrelationId(event);
    System.out.println("receiveAsyncReply: " + asyncReplyCorrelationId);
    Latch asyncReplyLatch = getLatch(asyncReplyCorrelationId);
    // flag for catching the interrupted status of the Thread waiting for a
    // result
    boolean interruptedWhileWaiting = false;
    boolean resultAvailable = false;
    PrivilegedEvent result;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Waiting for async reply message with id: " + asyncReplyCorrelationId);
        }
        // how long should we wait for the lock?
        if (timeout <= 0) {
            asyncReplyLatch.await();
            resultAvailable = true;
        } else {
            resultAvailable = asyncReplyLatch.await(timeout, MILLISECONDS);
        }
        if (!resultAvailable) {
            asyncReplyLatch.await(1000, MILLISECONDS);
            resultAvailable = asyncReplyLatch.getCount() == 0;
        }
    } catch (InterruptedException e) {
        interruptedWhileWaiting = true;
    } finally {
        locks.remove(asyncReplyCorrelationId);
        result = responseEvents.remove(asyncReplyCorrelationId);
        if (interruptedWhileWaiting) {
            Thread.currentThread().interrupt();
            return null;
        }
    }
    if (resultAvailable) {
        if (result == null) {
            // this should never happen, just using it as a safe guard for now
            throw new IllegalStateException("Response MuleEvent is null");
        }
        // Copy event because the async-reply message was received by a different
        // receiver thread (or the senders dispatcher thread in case of vm
        // with queueEvents="false") and the current thread may need to mutate
        // the even. See MULE-4370
        setCurrentEvent(result);
        return result;
    } else {
        addProcessed(new ProcessedEvents(asyncReplyCorrelationId, EndReason.FINISHED_BY_TIMEOUT));
        if (failOnTimeout) {
            notificationFirer.dispatch(new RoutingNotification(event.getMessage(), null, ASYNC_REPLY_TIMEOUT));
            throw new ResponseTimeoutException(responseTimedOutWaitingForId((int) timeout, asyncReplyCorrelationId), null);
        } else {
            return null;
        }
    }
}
Also used : ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Latch(org.mule.runtime.api.util.concurrent.Latch) RoutingNotification(org.mule.runtime.api.notification.RoutingNotification)

Aggregations

RoutingNotification (org.mule.runtime.api.notification.RoutingNotification)3 DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)2 MuleException (org.mule.runtime.api.exception.MuleException)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 ObjectAlreadyExistsException (org.mule.runtime.api.store.ObjectAlreadyExistsException)2 ObjectDoesNotExistException (org.mule.runtime.api.store.ObjectDoesNotExistException)2 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)2 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)2 EventGroup (org.mule.runtime.core.internal.routing.EventGroup)2 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)2 RoutingException (org.mule.runtime.core.privileged.routing.RoutingException)2 String.format (java.lang.String.format)1 System.currentTimeMillis (java.lang.System.currentTimeMillis)1 MessageFormat (java.text.MessageFormat)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DAYS (java.util.concurrent.TimeUnit.DAYS)1 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)1 MINUTES (java.util.concurrent.TimeUnit.MINUTES)1