Example usage for org.springframework.integration.store MessageGroup canAdd

List of usage examples for org.springframework.integration.store MessageGroup canAdd

Introduction

In this page you can find the example usage for org.springframework.integration.store MessageGroup canAdd.

Prototype

boolean canAdd(Message<?> message);

Source Link

Document

Query if the message can be added.

Usage

From source file:org.springframework.integration.aggregator.AbstractCorrelatingMessageHandler.java

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    Object correlationKey = correlationStrategy.getCorrelationKey(message);
    Assert.state(correlationKey != null,
            "Null correlation not allowed.  Maybe the CorrelationStrategy is failing?");

    if (logger.isDebugEnabled()) {
        logger.debug("Handling message with correlationKey [" + correlationKey + "]: " + message);
    }/*  ww  w . j  a v  a 2  s. c  o m*/

    // TODO: INT-1117 - make the lock global?
    Lock lock = this.lockRegistry.obtain(UUIDConverter.getUUID(correlationKey).toString());

    lock.lockInterruptibly();
    try {
        MessageGroup messageGroup = messageStore.getMessageGroup(correlationKey);
        if (this.sequenceAware) {
            messageGroup = new SequenceAwareMessageGroup(messageGroup);
        }

        if (!messageGroup.isComplete() && messageGroup.canAdd(message)) {
            if (logger.isTraceEnabled()) {
                logger.trace("Adding message to group [ " + messageGroup + "]");
            }
            messageGroup = this.store(correlationKey, message);

            if (releaseStrategy.canRelease(messageGroup)) {
                Collection<Message<?>> completedMessages = null;
                try {
                    completedMessages = this.completeGroup(message, correlationKey, messageGroup);
                } finally {
                    // Always clean up even if there was an exception
                    // processing messages
                    this.afterRelease(messageGroup, completedMessages);
                }
            }
        } else {
            discardChannel.send(message);
        }
    } finally {
        lock.unlock();
    }
}

From source file:org.springframework.integration.aggregator.CorrelatingMessageHandler.java

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    Object correlationKey = correlationStrategy.getCorrelationKey(message);
    Assert.state(correlationKey != null,
            "Null correlation not allowed.  Maybe the CorrelationStrategy is failing?");

    if (logger.isDebugEnabled()) {
        logger.debug("Handling message with correlationKey [" + correlationKey + "]: " + message);
    }/*ww  w.  j av a2  s. com*/

    // TODO: INT-1117 - make the lock global?
    Object lock = getLock(correlationKey);
    synchronized (lock) {
        MessageGroup group = messageStore.getMessageGroup(correlationKey);
        if (group.canAdd(message)) {
            group = store(correlationKey, message);
            if (releaseStrategy.canRelease(group)) {
                Collection<Message> completedMessages = null;
                try {
                    completedMessages = completeGroup(message, correlationKey, group);
                } finally {
                    // Always clean up even if there was an exception
                    // processing messages
                    cleanUpForReleasedGroup(group, completedMessages);
                }
            } else if (group.isComplete()) {
                try {
                    // If not releasing any messages the group might still
                    // be complete
                    for (Message<?> discard : group.getUnmarked()) {
                        discardChannel.send(discard);
                    }
                } finally {
                    remove(group);
                }
            }
        } else {
            discardChannel.send(message);
        }
    }
}