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);
}
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();
}
}
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();
}
}
Aggregations