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);
}
}
};
}
Aggregations