Search in sources :

Example 46 with PrivilegedEvent

use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.

the class MessageChunkAggregator method getCorrelatorCallback.

@Override
protected EventCorrelatorCallback getCorrelatorCallback(MuleContext muleContext) {
    return new CollectionCorrelatorCallback(muleContext, storePrefix) {

        /**
         * This method is invoked if the shouldAggregate method is called and returns true. Once this method returns an aggregated
         * message the event group is removed from the router
         *
         * @param events the event group for this request
         * @return an aggregated message
         * @throws AggregationException if the aggregation fails. in this scenario the whole event
         *         group is removed and passed to the exception handler for this componenet
         */
        @Override
        public CoreEvent aggregateEvents(EventGroup events) throws AggregationException {
            PrivilegedEvent[] collectedEvents;
            try {
                collectedEvents = events.toArray(false);
            } catch (ObjectStoreException e) {
                throw new AggregationException(events, MessageChunkAggregator.this, e);
            }
            CoreEvent firstEvent = collectedEvents[0];
            Arrays.sort(collectedEvents, eventComparator);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);
            try {
                for (PrivilegedEvent event : collectedEvents) {
                    baos.write(event.getMessageAsBytes(muleContext));
                }
                final Message.Builder builder = Message.builder(firstEvent.getMessage());
                // try to deserialize message, since ChunkingRouter might have serialized the object...
                try {
                    builder.value(muleContext.getObjectSerializer().getInternalProtocol().deserialize(baos.toByteArray()));
                } catch (SerializationException e) {
                    builder.value(baos.toByteArray());
                }
                // Use last event, that hasn't been completed yet, for continued processing.
                return PrivilegedEvent.builder(collectedEvents[collectedEvents.length - 1]).message(builder.build()).session(getMergedSession(events.toArray())).build();
            } catch (Exception e) {
                throw new AggregationException(events, MessageChunkAggregator.this, e);
            } finally {
                closeQuietly(baos);
            }
        }
    };
}
Also used : CollectionCorrelatorCallback(org.mule.runtime.core.internal.routing.correlation.CollectionCorrelatorCallback) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) SerializationException(org.mule.runtime.api.serialization.SerializationException) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) SerializationException(org.mule.runtime.api.serialization.SerializationException)

Example 47 with PrivilegedEvent

use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.

the class EventGroup method getMessageCollectionEvent.

public CoreEvent getMessageCollectionEvent() {
    try {
        if (size() > 0) {
            PrivilegedEvent[] muleEvents = toArray(true);
            CoreEvent lastEvent = muleEvents[muleEvents.length - 1];
            List<Message> messageList = Arrays.stream(muleEvents).map(event -> event.getMessage()).collect(toList());
            final Message.Builder builder = Message.builder().collectionValue(messageList, Message.class);
            PrivilegedEvent muleEvent = PrivilegedEvent.builder(lastEvent).message(builder.build()).session(getMergedSession(muleEvents)).build();
            return muleEvent;
        } else {
            return null;
        }
    } catch (ObjectStoreException e) {
        // Nothing to do...
        return null;
    }
}
Also used : NOT_SET(org.mule.runtime.core.api.message.GroupCorrelation.NOT_SET) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) Arrays(java.util.Arrays) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Iterator(java.util.Iterator) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) DeserializationPostInitialisable(org.mule.runtime.core.privileged.store.DeserializationPostInitialisable) PartitionableObjectStore(org.mule.runtime.api.store.PartitionableObjectStore) MuleSession(org.mule.runtime.core.privileged.event.MuleSession) Serializable(java.io.Serializable) ClassUtils(org.mule.runtime.core.api.util.ClassUtils) DefaultMuleSession(org.mule.runtime.core.privileged.event.DefaultMuleSession) DASH(org.mule.runtime.core.api.util.StringUtils.DASH) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) MuleContext(org.mule.runtime.core.api.MuleContext) MuleException(org.mule.runtime.api.exception.MuleException) MuleProperties(org.mule.runtime.core.api.config.MuleProperties) IteratorUtils(org.apache.commons.collections.IteratorUtils) Optional(java.util.Optional) Comparator(java.util.Comparator) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) Message(org.mule.runtime.api.message.Message) CoreEvent(org.mule.runtime.core.api.event.CoreEvent)

Example 48 with PrivilegedEvent

use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.

the class AbstractAsyncRequestReplyRequester method retrieveEvent.

private PrivilegedEvent retrieveEvent(String correlationId) throws MuleException {
    MultipleRequestReplierEvent multipleEvent = (MultipleRequestReplierEvent) store.retrieve(correlationId);
    PrivilegedEvent event = multipleEvent.getEvent();
    // TODO MULE-10302 remove this.
    if (currentMuleContext.get() == null) {
        try {
            DeserializationPostInitialisable.Implementation.init(event, muleContext);
        } catch (Exception e) {
            throw new DefaultMuleException(e);
        }
    }
    return event;
}
Also used : DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException) PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent) ObjectStoreException(org.mule.runtime.api.store.ObjectStoreException) ResponseTimeoutException(org.mule.runtime.core.privileged.routing.ResponseTimeoutException) InitialisationException(org.mule.runtime.api.lifecycle.InitialisationException) MuleException(org.mule.runtime.api.exception.MuleException) RegistrationException(org.mule.runtime.core.privileged.registry.RegistrationException) DefaultMuleException(org.mule.runtime.api.exception.DefaultMuleException)

Example 49 with PrivilegedEvent

use of org.mule.runtime.core.privileged.event.PrivilegedEvent in project mule by mulesoft.

the class AbstractAsyncRequestReplyRequester method process.

@Override
public CoreEvent process(CoreEvent event) throws MuleException {
    if (replyMessageSource == null) {
        return processNext(event, empty());
    } else {
        addLock(event);
        sendAsyncRequest(event);
        PrivilegedEvent resultEvent = receiveAsyncReply(event);
        if (resultEvent != null) {
            // session properties. See MULE-5852
            if (((InternalMessage) resultEvent.getMessage()).getInboundProperty(MULE_SESSION_PROPERTY) != null) {
                ((PrivilegedEvent) event).getSession().merge(resultEvent.getSession());
            }
            resultEvent = PrivilegedEvent.builder(event).message(resultEvent.getMessage()).build();
            setCurrentEvent(resultEvent);
        }
        return resultEvent;
    }
}
Also used : PrivilegedEvent(org.mule.runtime.core.privileged.event.PrivilegedEvent)

Example 50 with PrivilegedEvent

use of org.mule.runtime.core.privileged.event.PrivilegedEvent 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

PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)56 Test (org.junit.Test)35 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)22 DataType (org.mule.runtime.api.metadata.DataType)17 Message (org.mule.runtime.api.message.Message)15 CompiledExpression (org.mule.mvel2.compiler.CompiledExpression)13 ParserContext (org.mule.mvel2.ParserContext)10 InternalMessage (org.mule.runtime.core.internal.message.InternalMessage)9 MuleException (org.mule.runtime.api.exception.MuleException)7 UTF_16 (java.nio.charset.StandardCharsets.UTF_16)6 CoreMatchers.equalTo (org.hamcrest.CoreMatchers.equalTo)6 InitialisationException (org.mule.runtime.api.lifecycle.InitialisationException)6 TypedValue (org.mule.runtime.api.metadata.TypedValue)6 Processor (org.mule.runtime.core.api.processor.Processor)6 Serializable (java.io.Serializable)5 HashMap (java.util.HashMap)5 Message.of (org.mule.runtime.api.message.Message.of)5 IOException (java.io.IOException)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4