Search in sources :

Example 51 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method reportDelivered.

private void reportDelivered(Message message) {
    try {
        MessageIndex messageIndex = ((MessageImpl) message).getMessageIndex();
        requestRegistry.request(new MessageDeliveredRequest(dispatchId, serverQueueConsumerId, messageIndex));
    } catch (Exception e) {
    }
}
Also used : MessageIndex(com.swiftmq.swiftlet.queue.MessageIndex) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 52 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-client by iitsoftware.

the class MessageConsumerImpl method receiveMessage.

synchronized Message receiveMessage(boolean block, long timeout) throws JMSException {
    verifyState();
    if (messageListener != null) {
        throw new JMSException("receive not allowed while a message listener has been set");
    }
    if (!consumerStarted)
        fillCache();
    if (messageCache.getSize() == 0) {
        if (block) {
            receiverWaiting = true;
            if (timeout == 0) {
                UninterruptableWaiter.doWait(this);
            } else {
                UninterruptableWaiter.doWait(this, timeout);
            }
        } else {
            if (fillCachePending && receiveNoWaitFirstCall) {
                UninterruptableWaiter.doWait(this, 1000);
            }
        }
    }
    receiverWaiting = false;
    if (messageCache.getSize() == 0 || isClosed())
        return null;
    AsyncMessageDeliveryRequest request = (AsyncMessageDeliveryRequest) messageCache.remove();
    MessageEntry messageEntry = request.getMessageEntry();
    MessageImpl msg = messageEntry.getMessage();
    messageEntry.moveMessageAttributes();
    msg.setMessageConsumerImpl(this);
    msg.reset();
    msg.setReadOnly(true);
    msg.setUseThreadContextCL(useThreadContextCL);
    if (request.isRequiresRestart())
        fillCache();
    if (reportDelivered)
        reportDelivered(msg);
    if (doAck) {
        try {
            acknowledgeMessage(msg.getMessageIndex(), false);
        } catch (JMSException e) {
        }
    }
    return msg;
}
Also used : MessageEntry(com.swiftmq.swiftlet.queue.MessageEntry) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 53 with MessageImpl

use of com.swiftmq.jms.MessageImpl 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 54 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.

the class MessageQueue method prepare.

public void prepare(Object localTxId, XidImpl globalTxId) throws QueueException {
    lockAndWaitAsyncFinished();
    try {
        if (!running)
            throw new QueueException("queue " + getQueueName() + " is not running");
        TransactionId transactionId = (TransactionId) localTxId;
        if (ctx.queueSpace.enabled)
            ctx.queueSpace.trace(getQueueName(), "prepare: " + transactionId + ", globalTxId: " + globalTxId);
        try {
            StoreTransaction storeTransaction = null;
            List preparedList = new ArrayList();
            List txList = transactionId.getTxList();
            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(), "prepare: " + transactionId + ", duplicate message rejected: " + message);
                            if (ctx.queueManager.isLogDuplicates())
                                ctx.logSwiftlet.logWarning(getQueueName(), "prepare: " + transactionId + ", duplicate message rejected: " + message);
                        } else
                            storeTransaction = insertMessage((StoreWriteTransaction) storeTransaction, message, preparedList);
                    }
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "prepare: " + transactionId + " SUCCESSFUL");
                } else {
                    // PULL_TRANSACTION
                    for (int i = 0; i < txList.size(); i++) storeTransaction = removeMessage((StoreReadTransaction) storeTransaction, (StoreId) txList.get(i), true);
                    preparedList.addAll(txList);
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "prepare: " + transactionId + " SUCCESSFUL");
                }
                txList.clear();
            }
            beforeTransactionComplete();
            if (storeTransaction != null)
                storeTransaction.prepare(globalTxId);
            transactionId.setPrepared(true);
            transactionId.setStoreTransaction(storeTransaction);
            transactionId.setGlobalTxId(globalTxId);
            transactionId.setPreparedList(preparedList);
        } catch (QueueException e) {
            throw e;
        } catch (Exception e1) {
            throw new QueueException(e1.toString());
        }
    } finally {
        queueLock.unlock();
    }
}
Also used : MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException)

Example 55 with MessageImpl

use of com.swiftmq.jms.MessageImpl in project swiftmq-ce by iitsoftware.

the class QueueMoverJob method start.

public void start(Properties properties, JobTerminationListener jobTerminationListener) throws JobException {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.queueManager.getName(), toString() + "/start, properties=" + properties + " ...");
    this.properties = properties;
    int cnt = 0;
    String queueName = properties.getProperty("Source Queue");
    if (!queueName.startsWith("tpc$")) {
        MessageSelector selector = null;
        String s = properties.getProperty("Message Selector");
        if (s != null) {
            selector = new MessageSelector(s);
            try {
                selector.compile();
            } catch (InvalidSelectorException e) {
                throw new JobException(e.toString(), e, false);
            }
        }
        try {
            receiver = ctx.queueManager.createQueueReceiver(queueName, null, selector);
            pullTx = receiver.createTransaction(false);
            targetQueue = new QueueImpl(properties.getProperty("Target Queue"));
            sender = ctx.queueManager.createQueueSender(targetQueue.getQueueName(), null);
            pushTx = sender.createTransaction();
        } catch (Exception e) {
            throw new JobException(e.toString(), e, false);
        }
        if (stopCalled) {
            terminate();
            return;
        }
        try {
            MessageEntry entry = null;
            while ((entry = pullTx.getMessage(0, selector)) != null) {
                MessageImpl msg = entry.getMessage();
                msg.setJMSDestination(targetQueue);
                msg.setSourceRouter(null);
                msg.setDestRouter(null);
                pushTx.putMessage(msg);
                cnt++;
                pushTx.commit();
                pullTx.commit();
                if (stopCalled) {
                    terminate();
                    return;
                }
                pullTx = receiver.createTransaction(false);
                pushTx = sender.createTransaction();
            }
        } catch (Exception e) {
            terminate();
            throw new JobException(e.toString(), e, false);
        }
    }
    terminate();
    jobTerminationListener.jobTerminated(cnt + " Messages moved");
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.queueManager.getName(), toString() + "/start, properties=" + properties + " done, cnt=" + cnt);
}
Also used : JobException(com.swiftmq.swiftlet.scheduler.JobException) InvalidSelectorException(javax.jms.InvalidSelectorException) MessageSelector(com.swiftmq.ms.MessageSelector) MessageImpl(com.swiftmq.jms.MessageImpl) QueueImpl(com.swiftmq.jms.QueueImpl) InvalidSelectorException(javax.jms.InvalidSelectorException) JobException(com.swiftmq.swiftlet.scheduler.JobException)

Aggregations

MessageImpl (com.swiftmq.jms.MessageImpl)106 JMSException (javax.jms.JMSException)42 QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)33 InvalidSelectorException (javax.jms.InvalidSelectorException)31 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)28 QueueImpl (com.swiftmq.jms.QueueImpl)21 DataByteArrayInputStream (com.swiftmq.tools.util.DataByteArrayInputStream)20 TopicImpl (com.swiftmq.jms.TopicImpl)17 MessageEntry (com.swiftmq.swiftlet.queue.MessageEntry)15 MessageIndex (com.swiftmq.swiftlet.queue.MessageIndex)15 RingBuffer (com.swiftmq.tools.collection.RingBuffer)14 InvalidDestinationException (javax.jms.InvalidDestinationException)14 List (java.util.List)13 EntityList (com.swiftmq.mgmt.EntityList)10 Message (javax.jms.Message)10 XidImpl (com.swiftmq.jms.XidImpl)6 DataByteArrayOutputStream (com.swiftmq.tools.util.DataByteArrayOutputStream)6 MessageSelector (com.swiftmq.ms.MessageSelector)5 XAContextException (com.swiftmq.swiftlet.xa.XAContextException)4 ArrayList (java.util.ArrayList)4