Search in sources :

Example 1 with CollectionCorrelatorCallback

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

Aggregations

ByteArrayOutputStream (org.apache.commons.io.output.ByteArrayOutputStream)1 Message (org.mule.runtime.api.message.Message)1 SerializationException (org.mule.runtime.api.serialization.SerializationException)1 ObjectStoreException (org.mule.runtime.api.store.ObjectStoreException)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 CollectionCorrelatorCallback (org.mule.runtime.core.internal.routing.correlation.CollectionCorrelatorCallback)1 PrivilegedEvent (org.mule.runtime.core.privileged.event.PrivilegedEvent)1