final String correlationId = getCorrelationID(exchange, in);
if (correlationId == null || correlationId.length() == 0) {
throw new IllegalArgumentException("Could not retrieve correlation id for incoming exchange");
}
// Load existing aggregation
Lock lock = getLockManager().getLock(correlationId);
lock.lock();
try {
Object aggregation = store.load(correlationId);
Date timeout = null;
// Create a new aggregate
if (aggregation == null) {
if (isAggregationClosed(correlationId)) {
// TODO: should we return an error here ?
} else {
aggregation = createAggregation(correlationId);
timeout = getTimeout(aggregation);
}
} else if (isRescheduleTimeouts()) {
timeout = getTimeout(aggregation);
}
// If the aggregation is not closed
if (aggregation != null) {
if (addMessage(aggregation, in, exchange)) {
sendAggregate(correlationId, aggregation, false);
} else {
store.store(correlationId, aggregation);
if (timeout != null) {
if (log.isDebugEnabled()) {
log.debug("Scheduling timeout at " + timeout + " for aggregate " + correlationId);
}
getTimerManager().schedule(new TimerListener() {
public void timerExpired(Timer timer) {
AbstractAggregator.this.onTimeout(correlationId);
}
}, timeout);
}
}
}
done(exchange);
} finally {
lock.unlock();
}
}
// Handle an ACTIVE exchange as a CONSUMER
} else if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
done(exchange);