Search in sources :

Example 1 with EventGroup

use of org.mule.runtime.core.internal.routing.EventGroup 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 EventGroup

use of org.mule.runtime.core.internal.routing.EventGroup 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 EventGroup

use of org.mule.runtime.core.internal.routing.EventGroup in project mule by mulesoft.

the class EventCorrelator method getEventGroup.

protected EventGroup getEventGroup(String groupId) throws ObjectStoreException {
    try {
        EventGroup eventGroup = (EventGroup) correlatorStore.retrieve(groupId, getEventGroupsPartitionKey());
        if (!eventGroup.isInitialised()) {
            try {
                DeserializationPostInitialisable.Implementation.init(eventGroup, muleContext);
            } catch (Exception e) {
                throw new ObjectStoreException(e);
            }
        }
        eventGroup.initEventsStore(correlatorStore);
        return eventGroup;
    } catch (ObjectDoesNotExistException e) {
        return null;
    }
}
Also used : ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) ObjectDoesNotExistException(org.mule.runtime.api.store.ObjectDoesNotExistException) EventGroup(org.mule.runtime.core.internal.routing.EventGroup) 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)

Aggregations

DefaultMuleException (org.mule.runtime.api.exception.DefaultMuleException)3 MuleException (org.mule.runtime.api.exception.MuleException)3 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)3 ObjectAlreadyExistsException (org.mule.runtime.api.store.ObjectAlreadyExistsException)3 ObjectDoesNotExistException (org.mule.runtime.api.store.ObjectDoesNotExistException)3 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)3 MessagingException (org.mule.runtime.core.internal.exception.MessagingException)3 EventGroup (org.mule.runtime.core.internal.routing.EventGroup)3 RegistrationException (org.mule.runtime.core.privileged.registry.RegistrationException)3 RoutingException (org.mule.runtime.core.privileged.routing.RoutingException)3 RoutingNotification (org.mule.runtime.api.notification.RoutingNotification)2 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)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