Search in sources :

Example 6 with CompositeStoreTransaction

use of com.swiftmq.swiftlet.store.CompositeStoreTransaction in project swiftmq-ce by iitsoftware.

the class TopicTransaction method noParentTxCommit.

private long noParentTxCommit() throws Exception {
    long delay = 0;
    List durPersSubs = new ArrayList(subscriberTransactions.length);
    for (int i = 0; i < subscriberTransactions.length; i++) {
        TopicSubscriberTransaction sub = subscriberTransactions[i];
        if (sub != null) {
            TopicSubscription subscription = sub.getTopicSubscription();
            if (subscription.isRemote() || !subscription.isDurable() || !sub.isPersistentMessageIncluded()) {
                try {
                    delay = Math.max(delay, sub.commit());
                } catch (QueueTransactionClosedException ignored) {
                }
            } else
                durPersSubs.add(sub);
        }
    }
    int size = durPersSubs.size();
    if (size > 0) {
        if (size == 1) {
            TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(0);
            try {
                delay = Math.max(delay, sub.commit());
            } catch (QueueTransactionClosedException ignored) {
            }
        } else {
            int lastLocked = -1;
            boolean spaceLeft = true;
            for (int i = 0; i < size; i++) {
                TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                sub.getTransaction().lockQueue();
                lastLocked = i;
                if (!sub.getTransaction().hasSpaceLeft()) {
                    spaceLeft = false;
                    break;
                }
            }
            if (!spaceLeft) {
                for (int i = 0; i <= lastLocked; i++) {
                    TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                    sub.getTransaction().unlockQueue(false);
                }
                throw new QueueLimitException("Maximum Messages in at least one Durable Subscriber Queue reached!");
            }
            CompositeStoreTransaction compositeTx = ctx.storeSwiftlet.createCompositeStoreTransaction();
            try {
                for (int i = 0; i < size; i++) {
                    TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                    try {
                        sub.getTransaction().setCompositeStoreTransaction(compositeTx);
                        delay = Math.max(delay, sub.commit());
                        sub.getTransaction().setCompositeStoreTransaction(null);
                    } catch (QueueTransactionClosedException ignored) {
                    }
                }
                compositeTx.commitTransaction();
            } finally {
                for (int i = 0; i < size; i++) {
                    TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                    sub.getTransaction().unlockQueue(false);
                }
            }
        }
    }
    subscriberTransactions = null;
    return delay;
}
Also used : QueueTransactionClosedException(com.swiftmq.swiftlet.queue.QueueTransactionClosedException) QueueLimitException(com.swiftmq.swiftlet.queue.QueueLimitException) CompositeStoreTransaction(com.swiftmq.swiftlet.store.CompositeStoreTransaction) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with CompositeStoreTransaction

use of com.swiftmq.swiftlet.store.CompositeStoreTransaction in project swiftmq-ce by iitsoftware.

the class TopicTransaction method commit.

protected void commit(AsyncCompletionCallback callback) {
    final DelayCollector delayCollector = new DelayCollector(callback);
    if (subscriberTransactions != null) {
        final List durPersSubs = new ArrayList(subscriberTransactions.length);
        for (int i = 0; i < subscriberTransactions.length; i++) {
            TopicSubscriberTransaction sub = subscriberTransactions[i];
            if (sub != null) {
                TopicSubscription subscription = sub.getTopicSubscription();
                if (subscription.isRemote() || !subscription.isDurable() || !sub.isPersistentMessageIncluded()) {
                    delayCollector.incNumberCallbacks();
                    sub.commit(new AsyncCompletionCallback() {

                        public void done(boolean success) {
                            delayCollector.done(this, success);
                        }
                    });
                } else
                    durPersSubs.add(sub);
            }
        }
        final int size = durPersSubs.size();
        long delay = 0;
        if (size > 0) {
            // Only 1 subscriber, direct commit
            if (size == 1) {
                TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(0);
                delayCollector.incNumberCallbacks();
                sub.commit(new AsyncCompletionCallback() {

                    public void done(boolean success) {
                        delayCollector.done(this, success);
                    }
                });
            } else {
                // Multiple subscriber, composite tx
                int lastLocked = -1;
                boolean spaceLeft = true;
                for (int i = 0; i < size; i++) {
                    TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                    sub.getTransaction().lockQueue();
                    lastLocked = i;
                    if (!sub.getTransaction().hasSpaceLeft()) {
                        spaceLeft = false;
                        break;
                    }
                }
                if (!spaceLeft) {
                    for (int i = 0; i <= lastLocked; i++) {
                        TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                        sub.getTransaction().unlockQueue(false);
                    }
                    callback.setException(new QueueLimitException("Maximum Messages in at least one Durable Subscriber Queue reached!"));
                    callback.notifyCallbackStack(false);
                    return;
                }
                CompositeStoreTransaction compositeTx = ctx.storeSwiftlet.createCompositeStoreTransaction();
                for (int i = 0; i < size; i++) {
                    TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                    sub.getTransaction().setCompositeStoreTransaction(compositeTx);
                    try {
                        delay = Math.max(delay, sub.commit());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    sub.getTransaction().setCompositeStoreTransaction(null);
                    sub.getTransaction().unlockQueue(true);
                }
                delayCollector.incNumberCallbacks();
                AsyncCompletionCallback cb = new AsyncCompletionCallback() {

                    public void done(boolean success) {
                        for (int i = 0; i < size; i++) {
                            TopicSubscriberTransaction sub = (TopicSubscriberTransaction) durPersSubs.get(i);
                            sub.getTransaction().unmarkAsyncActive();
                        }
                        delayCollector.done(this, success);
                    }
                };
                cb.setResult(Long.valueOf(delay));
                compositeTx.commitTransaction(cb);
            }
        }
        subscriberTransactions = null;
    }
    delayCollector.setBlocked(false);
}
Also used : QueueLimitException(com.swiftmq.swiftlet.queue.QueueLimitException) AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) CompositeStoreTransaction(com.swiftmq.swiftlet.store.CompositeStoreTransaction) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) QueueLimitException(com.swiftmq.swiftlet.queue.QueueLimitException) QueueTransactionClosedException(com.swiftmq.swiftlet.queue.QueueTransactionClosedException)

Aggregations

CompositeStoreTransaction (com.swiftmq.swiftlet.store.CompositeStoreTransaction)7 QueueTransactionClosedException (com.swiftmq.swiftlet.queue.QueueTransactionClosedException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 QueueLimitException (com.swiftmq.swiftlet.queue.QueueLimitException)3 AsyncCompletionCallback (com.swiftmq.tools.concurrent.AsyncCompletionCallback)2 Iterator (java.util.Iterator)2 QueueException (com.swiftmq.swiftlet.queue.QueueException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1