Search in sources :

Example 1 with UpperBound

use of org.springframework.integration.util.UpperBound in project spring-integration by spring-projects.

the class SimpleMessageStore method removeMessagesFromGroup.

@Override
public void removeMessagesFromGroup(Object groupId, Collection<Message<?>> messages) {
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        try {
            MessageGroup group = this.groupIdToMessageGroup.get(groupId);
            Assert.notNull(group, "MessageGroup for groupId '" + groupId + "' " + "can not be located while attempting to remove Message(s) from the MessageGroup");
            UpperBound upperBound = this.groupToUpperBound.get(groupId);
            Assert.state(upperBound != null, "'upperBound' must not be null.");
            boolean modified = false;
            for (Message<?> messageToRemove : messages) {
                if (group.remove(messageToRemove)) {
                    upperBound.release();
                    modified = true;
                }
            }
            if (modified) {
                group.setLastModified(System.currentTimeMillis());
            }
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Example 2 with UpperBound

use of org.springframework.integration.util.UpperBound in project spring-integration by spring-projects.

the class SimpleMessageStore method addMessagesToGroup.

@Override
public void addMessagesToGroup(Object groupId, Message<?>... messages) {
    Assert.notNull(groupId, "'groupId' must not be null");
    Assert.notNull(messages, "'messages' must not be null");
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        boolean unlocked = false;
        try {
            UpperBound upperBound;
            MessageGroup group = this.groupIdToMessageGroup.get(groupId);
            MessagingException outOfCapacityException = new MessagingException(getClass().getSimpleName() + " was out of capacity (" + this.groupCapacity + ") for group '" + groupId + "', try constructing it with a larger capacity.");
            if (group == null) {
                if (this.groupCapacity > 0 && messages.length > this.groupCapacity) {
                    throw outOfCapacityException;
                }
                group = getMessageGroupFactory().create(groupId);
                this.groupIdToMessageGroup.put(groupId, group);
                upperBound = new UpperBound(this.groupCapacity);
                for (Message<?> message : messages) {
                    upperBound.tryAcquire(-1);
                    group.add(message);
                }
                this.groupToUpperBound.put(groupId, upperBound);
            } else {
                upperBound = this.groupToUpperBound.get(groupId);
                Assert.state(upperBound != null, "'upperBound' must not be null.");
                for (Message<?> message : messages) {
                    lock.unlock();
                    if (!upperBound.tryAcquire(this.upperBoundTimeout)) {
                        unlocked = true;
                        throw outOfCapacityException;
                    }
                    lock.lockInterruptibly();
                    group.add(message);
                }
            }
            group.setLastModified(System.currentTimeMillis());
        } finally {
            if (!unlocked) {
                lock.unlock();
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Example 3 with UpperBound

use of org.springframework.integration.util.UpperBound in project spring-integration by spring-projects.

the class SimpleMessageStore method removeMessageGroup.

@Override
public void removeMessageGroup(Object groupId) {
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        try {
            MessageGroup messageGroup = this.groupIdToMessageGroup.remove(groupId);
            if (messageGroup != null) {
                UpperBound upperBound = this.groupToUpperBound.remove(groupId);
                Assert.state(upperBound != null, "'upperBound' must not be null.");
                upperBound.release(this.groupCapacity);
            }
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Example 4 with UpperBound

use of org.springframework.integration.util.UpperBound in project spring-integration by spring-projects.

the class SimpleMessageStore method clearMessageGroup.

public void clearMessageGroup(Object groupId) {
    Lock lock = this.lockRegistry.obtain(groupId);
    try {
        lock.lockInterruptibly();
        try {
            MessageGroup group = this.groupIdToMessageGroup.get(groupId);
            Assert.notNull(group, "MessageGroup for groupId '" + groupId + "' " + "can not be located while attempting to complete the MessageGroup");
            group.clear();
            group.setLastModified(System.currentTimeMillis());
            UpperBound upperBound = this.groupToUpperBound.get(groupId);
            Assert.state(upperBound != null, "'upperBound' must not be null.");
            upperBound.release(this.groupCapacity);
        } finally {
            lock.unlock();
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new MessagingException("Interrupted while obtaining lock", e);
    }
}
Also used : MessagingException(org.springframework.messaging.MessagingException) UpperBound(org.springframework.integration.util.UpperBound) Lock(java.util.concurrent.locks.Lock)

Aggregations

Lock (java.util.concurrent.locks.Lock)4 UpperBound (org.springframework.integration.util.UpperBound)4 MessagingException (org.springframework.messaging.MessagingException)4