Search in sources :

Example 16 with AsyncCompletionCallback

use of com.swiftmq.tools.concurrent.AsyncCompletionCallback 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)

Example 17 with AsyncCompletionCallback

use of com.swiftmq.tools.concurrent.AsyncCompletionCallback in project swiftmq-ce by iitsoftware.

the class TopicBroker method rollback.

public void rollback(Object transactionId, boolean setRedelivered, AsyncCompletionCallback callback) {
    lockAndWaitAsyncFinished();
    try {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$topicmanager", tracePrefix + "rollback");
        final TopicTransaction transaction = (TopicTransaction) transactionId;
        asyncActive.set(true);
        transaction.rollback(new AsyncCompletionCallback(callback) {

            public void done(boolean success) {
                lock.lock();
                try {
                    transactions.set(transaction.getTransactionId(), null);
                    if (!success)
                        next.setException(getException());
                } finally {
                    asyncActive.set(false);
                    asyncFinished.signalAll();
                    lock.unlock();
                }
            }
        });
        transactions.set(transaction.getTransactionId(), null);
    } finally {
        lock.unlock();
    }
}
Also used : AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback)

Example 18 with AsyncCompletionCallback

use of com.swiftmq.tools.concurrent.AsyncCompletionCallback in project swiftmq-ce by iitsoftware.

the class TopicBroker method commit.

public void commit(Object transactionId, AsyncCompletionCallback callback) {
    lockAndWaitAsyncFinished();
    try {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$topicmanager", tracePrefix + "commit (callback)");
        final TopicTransaction transaction = (TopicTransaction) transactionId;
        asyncActive.set(true);
        transaction.commit(new AsyncCompletionCallback(callback) {

            public void done(boolean success) {
                lock.lock();
                try {
                    if (success) {
                        Long delay = (Long) getResult();
                        if (delay != null) {
                            TopicFlowController fc = (TopicFlowController) getFlowController();
                            if (fc != null) {
                                next.setResult(delay);
                                fc.setLastDelay(delay.longValue());
                            }
                        }
                        transactions.set(transaction.getTransactionId(), null);
                    } else
                        next.setException(getException());
                } finally {
                    asyncActive.set(false);
                    ;
                    asyncFinished.signalAll();
                    lock.unlock();
                }
            }
        });
    } finally {
        lock.unlock();
    }
}
Also used : AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback)

Aggregations

AsyncCompletionCallback (com.swiftmq.tools.concurrent.AsyncCompletionCallback)18 ArrayList (java.util.ArrayList)5 QueueTransactionClosedException (com.swiftmq.swiftlet.queue.QueueTransactionClosedException)4 List (java.util.List)4 JMSException (javax.jms.JMSException)4 CommitLogRecord (com.swiftmq.impl.store.standard.log.CommitLogRecord)3 StoreException (com.swiftmq.swiftlet.store.StoreException)3 MessagePageReference (com.swiftmq.impl.store.standard.index.MessagePageReference)2 QueueLimitException (com.swiftmq.swiftlet.queue.QueueLimitException)2 CompositeStoreTransaction (com.swiftmq.swiftlet.store.CompositeStoreTransaction)2 AmqpError (com.swiftmq.amqp.v100.generated.transport.definitions.AmqpError)1 ConnectionError (com.swiftmq.amqp.v100.generated.transport.definitions.ConnectionError)1 Error (com.swiftmq.amqp.v100.generated.transport.definitions.Error)1 CacheReleaseListener (com.swiftmq.impl.store.standard.cache.CacheReleaseListener)1 QueueIndexEntry (com.swiftmq.impl.store.standard.index.QueueIndexEntry)1 AbortLogRecord (com.swiftmq.impl.store.standard.log.AbortLogRecord)1 MessageImpl (com.swiftmq.jms.MessageImpl)1 Semaphore (com.swiftmq.tools.concurrent.Semaphore)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1