Search in sources :

Example 91 with MessageImpl

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

the class TransactedQueueSession method visit.

public void visit(CommitRequest req) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitCommitRequest");
    CommitReply reply = (CommitReply) req.createReply();
    reply.setOk(true);
    try {
        // first: produce all messages
        List ml = req.getMessages();
        ctx.incMsgsSent(ml.size());
        long fcDelay = 0;
        RingBuffer tempProducers = null;
        for (int i = 0; i < ml.size(); i++) {
            DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) ml.get(i));
            int producerId = dis.readInt();
            int type = dis.readInt();
            MessageImpl msg = MessageImpl.createInstance(type);
            msg.readContent(dis);
            dis.close();
            long ttl = msg.getJMSExpiration();
            if (ttl > 0)
                msg.setJMSExpiration(System.currentTimeMillis() + ttl);
            Producer producer = null;
            if (producerId == -1) {
                String queueName = ((QueueImpl) msg.getJMSDestination()).getQueueName();
                if (!ctx.queueManager.isQueueRunning(queueName))
                    throw new InvalidDestinationException("Invalid destination: " + queueName);
                producer = new QueueProducer(ctx, queueName);
                if (tempProducers == null)
                    tempProducers = new RingBuffer(8);
                tempProducers.add(producer);
                transactionManager.addTransactionFactory(producer);
            } else {
                producer = (Producer) producerList.get(producerId);
            }
            QueuePushTransaction transaction = producer.getTransaction();
            transaction.putMessage(msg);
            fcDelay = Math.max(fcDelay, transaction.getFlowControlDelay());
            if (producerId == -1)
                producer.markForClose();
        }
        // Next: do the commit
        transactionManager.commit();
        if (tempProducers != null) {
            int size = tempProducers.getSize();
            for (int i = 0; i < size; i++) {
                ((Producer) tempProducers.remove()).close();
            }
        }
        reply.setDelay(fcDelay);
        purgeMarkedProducers();
        purgeMarkedConsumers();
    } catch (Exception e) {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/commit produced messages failed: " + e.getMessage());
        reply.setOk(false);
        reply.setException(e);
    }
    reply.send();
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) InvalidDestinationException(javax.jms.InvalidDestinationException) RingBuffer(com.swiftmq.tools.collection.RingBuffer) DataByteArrayInputStream(com.swiftmq.tools.util.DataByteArrayInputStream) QueueImpl(com.swiftmq.jms.QueueImpl) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException) EntityList(com.swiftmq.mgmt.EntityList) List(java.util.List) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 92 with MessageImpl

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

the class CompositeQueue method putMessage.

public void putMessage(Object object, MessageImpl message) throws QueueException {
    if (ctx.queueSpace.enabled)
        ctx.queueSpace.trace(getQueueName(), "putMessage, cTxId=" + object + " ...");
    boolean needCopy = false;
    boolean hasDefaultDelivery = false;
    int timesDelivered = 0;
    List entries = ((CompositeTransactionId) object).getEntries();
    for (int i = 0; i < entries.size(); i++) {
        CompositeTransactionIdEntry entry = (CompositeTransactionIdEntry) entries.get(i);
        if (entry.isDefaultBinding)
            hasDefaultDelivery = true;
        if (!entry.isDefaultBinding && (entry.selector == null || entry.selector.isSelected(message))) {
            try {
                MessageImpl m = message;
                if (needCopy)
                    m = copyMessage(message);
                else
                    needCopy = true;
                if (entry.generateNewMessageId)
                    m.setJMSMessageID(nextId());
                if (entry.changeDestination)
                    m.setJMSDestination(new QueueImpl(ctx.queueManager.fqn(entry.originalName)));
                if (!entry.isQueue)
                    m.setJMSDestination(new TopicImpl(entry.originalName));
                if (ctx.queueSpace.enabled)
                    ctx.queueSpace.trace(getQueueName(), "putMessage on binding '" + entry.queue.getQueueName());
                entry.queue.putMessage(entry.txId, m);
                timesDelivered++;
            } catch (Exception e) {
                ctx.logSwiftlet.logError(ctx.queueManager.getName(), "Composite Queue '" + getQueueName() + "': putMessage on binding queue '" + entry.queue.getQueueName() + "', exception=" + e);
                if (ctx.queueSpace.enabled)
                    ctx.queueSpace.trace(getQueueName(), "putMessage on binding queue '" + entry.queue.getQueueName() + "', exception=" + e);
            }
        }
    }
    if (ctx.queueSpace.enabled)
        ctx.queueSpace.trace(getQueueName(), "putMessage delivered to " + timesDelivered + " bindings, hasDefaultDelivery=" + hasDefaultDelivery);
    if (timesDelivered == 0 && hasDefaultDelivery) {
        if (ctx.queueSpace.enabled)
            ctx.queueSpace.trace(getQueueName(), "putMessage, let's check the default bindings ...");
        for (int i = 0; i < entries.size(); i++) {
            CompositeTransactionIdEntry entry = (CompositeTransactionIdEntry) entries.get(i);
            if (entry.isDefaultBinding) {
                try {
                    MessageImpl m = message;
                    if (needCopy)
                        m = copyMessage(message);
                    else
                        needCopy = true;
                    if (entry.generateNewMessageId)
                        m.setJMSMessageID(nextId());
                    if (entry.changeDestination)
                        m.setJMSDestination(new QueueImpl(ctx.queueManager.fqn(entry.originalName)));
                    if (!entry.isQueue)
                        m.setJMSDestination(new TopicImpl(entry.originalName));
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "putMessage on DEFAULT binding '" + entry.queue.getQueueName());
                    entry.queue.putMessage(entry.txId, m);
                } catch (Exception e) {
                    ctx.logSwiftlet.logError(ctx.queueManager.getName(), "Composite Queue '" + getQueueName() + "': putMessage on binding queue '" + entry.queue.getQueueName() + "', exception=" + e);
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "putMessage on binding queue '" + entry.queue.getQueueName() + "', exception=" + e);
                }
            }
        }
    }
    if (ctx.queueSpace.enabled)
        ctx.queueSpace.trace(getQueueName(), "putMessage, cTxId=" + object + " done");
}
Also used : List(java.util.List) TopicImpl(com.swiftmq.jms.TopicImpl) MessageImpl(com.swiftmq.jms.MessageImpl) QueueImpl(com.swiftmq.jms.QueueImpl) UnknownHostException(java.net.UnknownHostException) QueueException(com.swiftmq.swiftlet.queue.QueueException) InvalidSelectorException(javax.jms.InvalidSelectorException)

Example 93 with MessageImpl

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

the class CompositeQueue method copyMessage.

private MessageImpl copyMessage(MessageImpl msg) throws Exception {
    DataByteArrayOutputStream dbos = new DataByteArrayOutputStream();
    DataByteArrayInputStream dbis = new DataByteArrayInputStream();
    msg.writeContent(dbos);
    dbis.reset();
    dbis.setBuffer(dbos.getBuffer(), 0, dbos.getCount());
    MessageImpl msgCopy = MessageImpl.createInstance(dbis.readInt());
    msgCopy.readContent(dbis);
    return msgCopy;
}
Also used : DataByteArrayOutputStream(com.swiftmq.tools.util.DataByteArrayOutputStream) DataByteArrayInputStream(com.swiftmq.tools.util.DataByteArrayInputStream) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 94 with MessageImpl

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

the class Importer method execute.

public String[] execute(String[] context, Entity entity, String[] cmd) {
    if (cmd.length < 3)
        return new String[] { TreeCommands.ERROR, "Invalid command, please try '" + TreeCommands.IMPORT + " <localdir> <queuename> [-newheader] [-delete] [-filter <regex>]" };
    int nMsgs = 0;
    try {
        String localDir = cmd[1];
        String queueName = cmd[2];
        boolean delete = false;
        boolean newId = false;
        String filter = null;
        if (cmd.length > 3) {
            StringBuffer selBuffer = null;
            for (int i = 3; i < cmd.length; i++) {
                if (selBuffer != null)
                    selBuffer.append(cmd[i]);
                else if (cmd[i].equals("-delete"))
                    delete = true;
                else if (cmd[i].equals("-newid"))
                    newId = true;
                else if (cmd[i].equals("-filter"))
                    selBuffer = new StringBuffer();
                else
                    throw new Exception("Invalid option: " + cmd[i]);
            }
            if (selBuffer != null)
                filter = selBuffer.toString();
        }
        String idPrefix = null;
        if (newId) {
            StringBuffer b = new StringBuffer(SwiftletManager.getInstance().getRouterName());
            b.append('/');
            b.append(IdGenerator.getInstance().nextId('/'));
            b.append('/');
            idPrefix = b.toString();
        }
        XStream xStream = null;
        File inputDir = new File(localDir);
        if (!inputDir.exists())
            throw new Exception("Input directory doesn't exists: " + localDir);
        xStream = new XStream(new Dom4JDriver());
        xStream.allowTypesByWildcard(new String[] { ".*" });
        QueueSender sender = ctx.queueManager.createQueueSender(queueName, null);
        try {
            MessageImpl msg = null;
            File[] files = null;
            files = inputDir.listFiles(new RegexFilter(filter) {

                public boolean accept(File file, String s) {
                    return regex == null || s.matches(regex);
                }
            });
            if (files != null && files.length > 0) {
                Arrays.sort(files);
                for (int i = 0; i < files.length; i++) {
                    msg = null;
                    if (!files[i].isDirectory()) {
                        if (files[i].getName().endsWith(".xml")) {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(files[i]));
                            msg = (MessageImpl) xStream.fromXML(bufferedReader);
                            bufferedReader.close();
                        } else if (files[i].getName().endsWith(".message")) {
                            DataStreamInputStream dis = new DataStreamInputStream(new BufferedInputStream(new FileInputStream(files[i])));
                            msg = MessageImpl.createInstance(dis.readInt());
                            msg.readContent(dis);
                            dis.close();
                        }
                        if (msg != null) {
                            if (newId) {
                                StringBuffer b = new StringBuffer(idPrefix);
                                b.append(nMsgs);
                                msg.setJMSMessageID(b.toString());
                            }
                            QueuePushTransaction pushTx = sender.createTransaction();
                            pushTx.putMessage(msg);
                            pushTx.commit();
                            if (delete)
                                files[i].delete();
                            nMsgs++;
                        }
                    }
                }
            }
        } finally {
            try {
                sender.close();
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        return new String[] { TreeCommands.ERROR, e.getMessage() };
    }
    return new String[] { TreeCommands.INFO, nMsgs + " messages imported." };
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) DataStreamInputStream(com.swiftmq.tools.util.DataStreamInputStream) XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) QueueSender(com.swiftmq.swiftlet.queue.QueueSender) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 95 with MessageImpl

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

the class MessageQueue method commit.

public void commit(Object tId) throws QueueException {
    lockAndWaitAsyncFinished();
    try {
        if (!running)
            throw new QueueException("queue " + getQueueName() + " is not running");
        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());
                    if (ctx.queueSpace.enabled)
                        ctx.queueSpace.trace(getQueueName(), "commit: " + transactionId + " queueSemaphore.notify()");
                } 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());
                }
            }
            beforeTransactionComplete();
            if (storeTransaction != null && compositeTx == null)
                storeTransaction.commit();
            clearTransaction(transactionId);
            // notify waiting get's
            if (transactionId.getTransactionType() == TransactionId.PUSH_TRANSACTION)
                notifyWaiters();
        } catch (QueueException e) {
            throw e;
        } catch (Exception e1) {
            throw new QueueException(e1.toString());
        }
        if (flowController != null)
            flowController.setQueueSize(queueContent.size());
    } finally {
        queueLock.unlock();
    }
}
Also used : MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException)

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