Search in sources :

Example 51 with QueuePushTransaction

use of com.swiftmq.swiftlet.queue.QueuePushTransaction 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 52 with QueuePushTransaction

use of com.swiftmq.swiftlet.queue.QueuePushTransaction in project swiftmq-ce by iitsoftware.

the class TransactedTopicSession method visitCommitRequest.

public void visitCommitRequest(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
        Object[] wrapper = req.getMessages();
        ctx.incMsgsSent(wrapper.length);
        req.setMessages(null);
        long fcDelay = 0;
        RingBuffer tempProducers = null;
        for (int i = 0; i < wrapper.length; i++) {
            DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) wrapper[i]);
            int producerId = dis.readInt();
            int type = dis.readInt();
            MessageImpl msg = MessageImpl.createInstance(type);
            msg.readContent(dis);
            dis.close();
            Producer producer = null;
            if (producerId == -1) {
                TopicImpl topic = (TopicImpl) msg.getJMSDestination();
                if (topic.getType() != DestinationFactory.TYPE_TEMPTOPIC)
                    ctx.authSwiftlet.verifyTopicSenderSubscription(topic.getTopicName(), ctx.activeLogin.getLoginId());
                producer = new TopicProducer(ctx, topic);
                if (tempProducers == null)
                    tempProducers = new RingBuffer(8);
                tempProducers.add(producer);
                transactionManager.addTransactionFactory(producer);
            } else {
                producer = (Producer) producerList.get(producerId);
            }
            QueuePushTransaction transaction = (QueuePushTransaction) 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 instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
    }
    reply.send();
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) JMSException(javax.jms.JMSException) RingBuffer(com.swiftmq.tools.collection.RingBuffer) DataByteArrayInputStream(com.swiftmq.tools.util.DataByteArrayInputStream) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException) TopicImpl(com.swiftmq.jms.TopicImpl) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 53 with QueuePushTransaction

use of com.swiftmq.swiftlet.queue.QueuePushTransaction in project swiftmq-ce by iitsoftware.

the class TransactedUnifiedSession method visitCommitRequest.

public void visitCommitRequest(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
        Object[] wrapper = req.getMessages();
        ctx.incMsgsSent(wrapper.length);
        req.setMessages(null);
        long fcDelay = 0;
        RingBuffer tempProducers = null;
        for (int i = 0; i < wrapper.length; i++) {
            DataByteArrayInputStream dis = new DataByteArrayInputStream((byte[]) wrapper[i]);
            int producerId = dis.readInt();
            int type = dis.readInt();
            MessageImpl msg = MessageImpl.createInstance(type);
            msg.readContent(dis);
            dis.close();
            Producer producer = null;
            if (producerId == -1) {
                DestinationImpl destImpl = (DestinationImpl) msg.getJMSDestination();
                switch(destImpl.getType()) {
                    case DestinationFactory.TYPE_QUEUE:
                    case DestinationFactory.TYPE_TEMPQUEUE:
                        String queueName = ((QueueImpl) destImpl).getQueueName();
                        if (!ctx.queueManager.isQueueRunning(queueName))
                            throw new InvalidDestinationException("Invalid destination: " + queueName);
                        producer = new QueueProducer(ctx, queueName);
                        break;
                    case DestinationFactory.TYPE_TOPIC:
                    case DestinationFactory.TYPE_TEMPTOPIC:
                        TopicImpl topic = (TopicImpl) msg.getJMSDestination();
                        if (topic.getType() != DestinationFactory.TYPE_TEMPTOPIC)
                            ctx.authSwiftlet.verifyTopicSenderSubscription(topic.getTopicName(), ctx.activeLogin.getLoginId());
                        producer = new TopicProducer(ctx, topic);
                        break;
                }
                if (tempProducers == null)
                    tempProducers = new RingBuffer(8);
                tempProducers.add(producer);
                transactionManager.addTransactionFactory(producer);
            } else {
                producer = (Producer) producerList.get(producerId);
            }
            QueuePushTransaction transaction = (QueuePushTransaction) 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 instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
    }
    reply.send();
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) RingBuffer(com.swiftmq.tools.collection.RingBuffer) DataByteArrayInputStream(com.swiftmq.tools.util.DataByteArrayInputStream) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException) com.swiftmq.jms(com.swiftmq.jms)

Example 54 with QueuePushTransaction

use of com.swiftmq.swiftlet.queue.QueuePushTransaction in project swiftmq-ce by iitsoftware.

the class NontransactedQueueSession method visitProduceMessageRequest.

public void visitProduceMessageRequest(ProduceMessageRequest req) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitProduceMessageRequest");
    ctx.incMsgsSent(1);
    ProduceMessageReply reply = null;
    if (req.isReplyRequired())
        reply = (ProduceMessageReply) req.createReply();
    MessageImpl msg = req.getMessage();
    int producerId = req.getQueueProducerId();
    Producer producer = null;
    try {
        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);
        } else {
            producer = (Producer) producerList.get(producerId);
        }
        QueuePushTransaction transaction = (QueuePushTransaction) producer.createTransaction();
        transaction.putMessage(msg);
        transaction.commit();
        if (req.isReplyRequired()) {
            reply.setDelay(transaction.getFlowControlDelay());
            reply.setOk(true);
        }
        if (producerId == -1)
            producer.close();
    } catch (Exception e) {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/produce messages failed: " + e.getMessage());
        if (req.isReplyRequired()) {
            reply.setOk(false);
            reply.setException((e instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
        }
    }
    if (req.isReplyRequired())
        reply.send();
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) QueueImpl(com.swiftmq.jms.QueueImpl) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 55 with QueuePushTransaction

use of com.swiftmq.swiftlet.queue.QueuePushTransaction in project swiftmq-ce by iitsoftware.

the class NontransactedTopicSession method visitProduceMessageRequest.

public void visitProduceMessageRequest(ProduceMessageRequest req) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitProduceMessageRequest");
    ctx.incMsgsSent(1);
    ProduceMessageReply reply = null;
    if (req.isReplyRequired())
        reply = (ProduceMessageReply) req.createReply();
    MessageImpl msg = req.getMessage();
    int producerId = req.getQueueProducerId();
    Producer producer = null;
    try {
        if (producerId == -1) {
            TopicImpl topic = (TopicImpl) msg.getJMSDestination();
            if (topic.getType() != DestinationFactory.TYPE_TEMPTOPIC)
                ctx.authSwiftlet.verifyTopicSenderSubscription(topic.getTopicName(), ctx.activeLogin.getLoginId());
            producer = new TopicProducer(ctx, topic);
        } else {
            producer = (Producer) producerList.get(producerId);
        }
        QueuePushTransaction transaction = (QueuePushTransaction) producer.createTransaction();
        transaction.putMessage(msg);
        transaction.commit();
        if (req.isReplyRequired()) {
            reply.setDelay(transaction.getFlowControlDelay());
            reply.setOk(true);
        }
        if (producerId == -1)
            producer.close();
    } catch (Exception e) {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/produce messages failed: " + e.getMessage());
        if (req.isReplyRequired()) {
            reply.setOk(false);
            reply.setException((e instanceof JMSException) ? e : new javax.jms.JMSException(e.toString()));
        }
    }
    if (req.isReplyRequired())
        reply.send();
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) JMSException(javax.jms.JMSException) TopicImpl(com.swiftmq.jms.TopicImpl) MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException)

Aggregations

QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)57 JMSException (javax.jms.JMSException)44 InvalidSelectorException (javax.jms.InvalidSelectorException)43 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)42 MessageImpl (com.swiftmq.jms.MessageImpl)33 InvalidDestinationException (javax.jms.InvalidDestinationException)28 RingBuffer (com.swiftmq.tools.collection.RingBuffer)21 DataByteArrayInputStream (com.swiftmq.tools.util.DataByteArrayInputStream)21 QueueImpl (com.swiftmq.jms.QueueImpl)16 List (java.util.List)16 EntityList (com.swiftmq.mgmt.EntityList)15 TopicImpl (com.swiftmq.jms.TopicImpl)14 QueueSender (com.swiftmq.swiftlet.queue.QueueSender)7 BytesMessageImpl (com.swiftmq.jms.BytesMessageImpl)5 com.swiftmq.jms (com.swiftmq.jms)4 QueueException (com.swiftmq.swiftlet.queue.QueueException)4 XAContextException (com.swiftmq.swiftlet.xa.XAContextException)3 XidImpl (com.swiftmq.jms.XidImpl)2 AbstractQueue (com.swiftmq.swiftlet.queue.AbstractQueue)2 Versionable (com.swiftmq.tools.versioning.Versionable)2