Search in sources :

Example 6 with AsyncCompletionCallback

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

the class LogManager method process.

protected void process(Object[] bulk, int n) {
    if (closed && !checkPointPending)
        return;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$store", toString() + "/process,length=" + n);
    try {
        if (logManagerListener != null)
            logManagerListener.startProcessing();
        boolean checkPointNow = false;
        for (int i = 0; i < n; i++) {
            LogOperation oper = (LogOperation) bulk[i];
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, operation=" + oper);
            switch(oper.getOperationType()) {
                case LogOperation.OPER_LOG_REC:
                    LogRecord lr = (LogRecord) oper;
                    logFile.write(lr);
                    Semaphore s = lr.getSemaphore();
                    if (s != null)
                        semList.add(s);
                    CacheReleaseListener crl = lr.getCacheReleaseListener();
                    if (crl != null)
                        releaseList.add(crl);
                    AsyncCompletionCallback cb = lr.getCallback();
                    if (cb != null)
                        callbackList.add(cb);
                    // Releasing locks on shared message pages
                    List messagePageRefs = lr.getMessagePageRefs();
                    if (messagePageRefs != null) {
                        for (int j = 0; j < messagePageRefs.size(); j++) {
                            ((MessagePageReference) messagePageRefs.get(j)).unMarkActive();
                        }
                    }
                    break;
                case LogOperation.OPER_CLOSE_LOG:
                    closeSem = ((CloseLogOperation) oper).getSemaphore();
                    closed = true;
                    break;
                case LogOperation.OPER_INITIATE_SYNC:
                    checkPointInitiated = true;
                    break;
                case LogOperation.OPER_SYNC_LOG:
                    checkPointNow = true;
                    break;
            }
        }
        // write the log to the log file
        if (logFile.getFlushSize() > 0)
            logFile.flush(forceSync);
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, processed log objects=" + n);
        if (checkPointNow) {
            if (checkPointVerbose)
                System.out.println("+++ CHECKPOINT ... ");
            long start = System.currentTimeMillis();
            // Trigger CP Handler to flush the cache.
            checkPointHandler.performCheckPoint();
            // Start new log file
            logFile.reset(forceSync);
            // continue normal operation
            checkPointHandler.checkPointDone();
            checkPointPending = false;
            if (checkPointVerbose)
                System.out.println("+++ CHECKPOINT DONE in " + (System.currentTimeMillis() - start) + " milliseconds");
            if (closed) {
                if (logManagerListener != null)
                    logManagerListener.stopProcessing();
                closeSem.notifySingleWaiter();
                return;
            }
        }
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, releaseList.size=" + releaseList.size() + ", semList.size=" + semList.size() + ", callbackList.size=" + callbackList.size());
        // call back cache release listeners
        if (releaseList.size() > 0) {
            for (int i = 0; i < releaseList.size(); i++) {
                CacheReleaseListener crl = (CacheReleaseListener) releaseList.get(i);
                crl.releaseCache();
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + releaseList.size() + " CacheReleaseListeners called");
            releaseList.clear();
        }
        if (logManagerListener != null)
            logManagerListener.stopProcessing();
        // notify Semaphores
        if (semList.size() > 0) {
            for (int i = 0; i < semList.size(); i++) {
                Semaphore ls = (Semaphore) semList.get(i);
                ls.notifySingleWaiter();
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + semList.size() + " semaphores notified");
            semList.clear();
        }
        // notify callbacks
        if (callbackList.size() > 0) {
            for (int i = 0; i < callbackList.size(); i++) {
                AsyncCompletionCallback cb = (AsyncCompletionCallback) callbackList.get(i);
                cb.notifyCallbackStack(true);
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + callbackList.size() + " AsyncCompletionCallbacks called");
            callbackList.clear();
        }
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, logFile.length=" + logFile.getPosition());
        // SyncLogOperation into the queue.
        if (!checkPointPending && (closed || checkPointInitiated || logFile.getPosition() >= maxLogSize)) {
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, checkPointHandler.lockForCheckPoint()");
            checkPointHandler.lockForCheckPoint();
            checkPointPending = true;
            checkPointInitiated = false;
        }
    } catch (Exception e) {
        // PANIC
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, exception occurred=" + e);
        ctx.logSwiftlet.logError("sys$store", toString() + "/process, PANIC! EXITING! Exception occurred=" + e);
        e.printStackTrace();
        SwiftletManager.getInstance().disableShutdownHook();
        System.exit(-1);
    }
}
Also used : MessagePageReference(com.swiftmq.impl.store.standard.index.MessagePageReference) AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Semaphore(com.swiftmq.tools.concurrent.Semaphore) CacheReleaseListener(com.swiftmq.impl.store.standard.cache.CacheReleaseListener)

Example 7 with AsyncCompletionCallback

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

the class ClusteredTransactionId method commit.

public void commit(AsyncCompletionCallback callback) {
    if (messageBasedDispatch) {
        if (queueMap.size() == 0) {
            callback.notifyCallbackStack(true);
            return;
        }
        final DelayCollector delayCollector = new DelayCollector(callback);
        for (Iterator iter = queueMap.entrySet().iterator(); iter.hasNext(); ) {
            Entry entry = (Entry) ((Map.Entry) iter.next()).getValue();
            delayCollector.incNumberCallbacks();
            entry.abstractQueue.commit(entry.transaction, new AsyncCompletionCallback() {

                public void done(boolean success) {
                    delayCollector.done(this, success);
                }
            });
        }
        delayCollector.setBlocked(false);
    } else {
        if (baseQueue == null)
            return;
        baseQueue.commit(baseTx, callback);
    }
}
Also used : AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) Iterator(java.util.Iterator) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with AsyncCompletionCallback

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

the class MessageQueue method commit.

public void commit(Object tId, AsyncCompletionCallback callback) {
    lockAndWaitAsyncFinished();
    try {
        if (!running) {
            callback.setException(new QueueException("queue " + getQueueName() + " is not running"));
            callback.notifyCallbackStack(false);
            return;
        }
        final TransactionId transactionId = (TransactionId) tId;
        List txList = transactionId.getTxList();
        if (ctx.queueSpace.enabled)
            ctx.queueSpace.trace(getQueueName(), "commit: " + transactionId);
        try {
            StoreTransaction storeTransaction = compositeTx;
            if (txList != null && txList.size() > 0) {
                if (transactionId.getTransactionType() == TransactionId.PUSH_TRANSACTION) {
                    if (maxMessages > 0 && queueContent.size() + txList.size() > maxMessages) {
                        throw new QueueLimitException("Maximum Messages in Queue reached!");
                    }
                    for (int i = 0; i < txList.size(); i++) {
                        MessageImpl message = (MessageImpl) txList.get(i);
                        if (checkDuplicate(message)) {
                            if (ctx.queueSpace.enabled)
                                ctx.queueSpace.trace(getQueueName(), "commit: " + transactionId + ", duplicate message rejected: " + message);
                            if (ctx.queueManager.isLogDuplicates())
                                ctx.logSwiftlet.logWarning(getQueueName(), "commit: " + transactionId + ", duplicate message rejected: " + message);
                        } else
                            storeTransaction = insertMessage((StoreWriteTransaction) storeTransaction, message);
                    }
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "commit: " + transactionId + " SUCCESSFUL");
                    if (flowController != null)
                        flowController.setSentMessageCount(txList.size());
                } else {
                    // PULL_TRANSACTION
                    for (int i = 0; i < txList.size(); i++) storeTransaction = removeMessage((StoreReadTransaction) storeTransaction, (StoreId) txList.get(i));
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "commit: " + transactionId + " SUCCESSFUL");
                    if (flowController != null)
                        flowController.setReceiveMessageCount(txList.size());
                    removeTxId(transactionId);
                }
            }
            beforeTransactionComplete();
            if (storeTransaction != null && compositeTx == null) {
                asyncActive = true;
                storeTransaction.commit(new AsyncCompletionCallback(callback) {

                    public void done(boolean success) {
                        queueLock.lock();
                        try {
                            transactionId.clear();
                            if (flowController != null) {
                                flowController.setQueueSize(queueContent.size());
                                if (success)
                                    next.setResult(Long.valueOf(flowController.getNewDelay()));
                                else
                                    next.setException(getException());
                            }
                        } finally {
                            asyncActive = false;
                            asyncFinished.signalAll();
                            // notify waiting get's
                            if (transactionId.getTransactionType() == TransactionId.PUSH_TRANSACTION)
                                notifyWaiters();
                            queueLock.unlock();
                        }
                    }
                });
            } else {
                transactionId.clear();
                // notify waiting get's
                if (transactionId.getTransactionType() == TransactionId.PUSH_TRANSACTION)
                    notifyWaiters();
                if (flowController != null) {
                    flowController.setQueueSize(queueContent.size());
                    callback.setResult(Long.valueOf(flowController.getNewDelay()));
                }
                callback.notifyCallbackStack(true);
            }
        } catch (Exception e) {
            removeTxId(transactionId);
            callback.setException(e);
            callback.notifyCallbackStack(false);
            asyncActive = false;
            asyncFinished.signalAll();
            return;
        }
    } finally {
        queueLock.unlock();
    }
}
Also used : AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException)

Example 9 with AsyncCompletionCallback

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

the class MessageQueue method rollback.

public void rollback(Object tId, boolean setRedelivered, AsyncCompletionCallback callback) {
    lockAndWaitAsyncFinished();
    try {
        if (!running) {
            callback.setException(new QueueException("queue " + getQueueName() + " is not running"));
            callback.notifyCallbackStack(false);
            return;
        }
        final TransactionId transactionId = (TransactionId) tId;
        if (ctx.queueSpace.enabled)
            ctx.queueSpace.trace(getQueueName(), "rollback: " + transactionId);
        try {
            List txList = transactionId.getTxList();
            if (txList != null) {
                if (transactionId.getTransactionType() == TransactionId.PULL_TRANSACTION) {
                    StoreReadTransaction srt = null;
                    if (pStore != null && setRedelivered)
                        srt = pStore.createReadTransaction(setRedelivered);
                    // Unlock storeId's
                    for (int i = 0; i < txList.size(); i++) {
                        StoreId storeId = (StoreId) txList.get(i);
                        storeId.setLocked(false);
                        if (setRedelivered) {
                            try {
                                if (ctx.queueSpace.enabled)
                                    ctx.queueSpace.trace(getQueueName(), "rollback: " + transactionId + ", pStore=" + pStore + ", srt=" + srt + ", setRedelivered: " + storeId);
                                storeId.setDeliveryCount(storeId.getDeliveryCount() + 1);
                                if (storeId.isPersistent() && srt != null)
                                    srt.remove(((StoreEntry) storeId.getPersistentKey()).key);
                                else {
                                    StoreEntry pk = (StoreEntry) storeId.getPersistentKey();
                                    if (pk != null)
                                        nStore.updateDeliveryCount(pk.key, storeId.getDeliveryCount());
                                }
                            } catch (Exception e) {
                                if (ctx.queueSpace.enabled)
                                    ctx.queueSpace.trace(getQueueName(), "rollback failed: " + transactionId + ", Exception: " + e.getMessage());
                                throw new QueueException("rollback failed: " + transactionId + ", Exception: " + e.getMessage());
                            }
                        }
                    }
                    beforeTransactionComplete();
                    if (srt != null) {
                        asyncActive = true;
                        srt.abort(new AsyncCompletionCallback(callback) {

                            public void done(boolean success) {
                                queueLock.lock();
                                try {
                                    notifyWaiters();
                                    clearTransaction(transactionId);
                                    if (!success)
                                        next.setException(getException());
                                } finally {
                                    asyncActive = false;
                                    asyncFinished.signalAll();
                                    queueLock.unlock();
                                }
                            }
                        });
                    } else {
                        // notify others
                        if (ctx.queueSpace.enabled)
                            ctx.queueSpace.trace(getQueueName(), "rollback: " + transactionId + ", notifyWaiters");
                        notifyWaiters();
                        clearTransaction(transactionId);
                        callback.notifyCallbackStack(true);
                    }
                } else {
                    clearTransaction(transactionId);
                    callback.notifyCallbackStack(true);
                }
            }
        } catch (Exception e) {
            clearTransaction(transactionId);
            asyncActive = false;
            callback.setException(new QueueException(e.toString()));
            callback.notifyCallbackStack(false);
            return;
        }
    } finally {
        queueLock.unlock();
    }
}
Also used : AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) JMSException(javax.jms.JMSException)

Example 10 with AsyncCompletionCallback

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

the class AMQPHandler method visit.

public void visit(POSendClose po) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " ...");
    Close close = new Close();
    if (po.getErrorCondition() != -1) {
        close.setReplyCode(po.getErrorCondition());
        close.setReplyText(po.getDescription());
        connectionDisabled = true;
    } else {
        close.setReplyCode(Constants.REPLY_SUCCESS);
        close.setReplyText("OK");
    }
    Frame frame = new Frame(Frame.TYPE_METHOD, 0, 0, null);
    toPayload(frame, close);
    frame.setCallback(new AsyncCompletionCallback() {

        public void done(boolean b) {
            ctx.networkSwiftlet.getConnectionManager().removeConnection(versionedConnection.getConnection());
        }
    });
    versionedConnection.send(frame);
    closeFrameSent = true;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.amqpSwiftlet.getName(), toString() + ", visit, po=" + po + " done");
}
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