Search in sources :

Example 81 with MessageImpl

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

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

the class Copier method execute.

public String[] execute(String[] context, Entity entity, String[] cmd) {
    if (cmd.length < 3 || !cmd[2].equals("-queue") && !cmd[2].equals("-topic"))
        return new String[] { TreeCommands.ERROR, "Invalid command, please try '" + _getPattern() + "'" };
    String[] result = null;
    try {
        if (cmd.length >= 6 && cmd[4].equals("-selector"))
            result = copyWithSelector(cmd);
        else {
            if (!ctx.queueManager.isQueueDefined(cmd[1]))
                throw new Exception("Unknown queue: " + cmd[1]);
            AbstractQueue aq = ctx.queueManager.getQueueForInternalUse(cmd[1]);
            if (!(aq instanceof MessageQueue))
                throw new Exception("Operation not supported on this type of queue!");
            MessageQueue sourceQueue = (MessageQueue) aq;
            QueueSender sender = null;
            QueueImpl targetQueueAddr = null;
            if (cmd[2].equals("-queue")) {
                sender = ctx.queueManager.createQueueSender(cmd[3], null);
                targetQueueAddr = new QueueImpl(cmd[3]);
            } else {
                String qft = ctx.topicManager.getQueueForTopic(cmd[3]);
                sender = ctx.queueManager.createQueueSender(qft, null);
                targetQueueAddr = new TopicImpl(cmd[3]);
            }
            SortedSet content = sourceQueue.getQueueIndex();
            int max = getMaxLimit(cmd);
            int start = 0;
            int stop = Integer.MAX_VALUE;
            if (cmd.length >= 7 && cmd[4].equals("-index")) {
                start = Integer.parseInt(cmd[5]);
                stop = Integer.parseInt(cmd[6]);
                if (stop < start)
                    throw new Exception("Stop index is less than start index.");
            } else {
                if (cmd.length != 4 && max == Integer.MAX_VALUE)
                    return new String[] { TreeCommands.ERROR, "Invalid command, please try '" + _getCommand() + " <source> -queue|-topic <target> -index <start> <stop>'" };
            }
            int i = 0, cnt = 0;
            for (Iterator iter = content.iterator(); iter.hasNext(); ) {
                MessageIndex mi = (MessageIndex) iter.next();
                if (i >= start && i <= stop) {
                    try {
                        MessageImpl msg = copyMessage(sourceQueue.getMessageByIndex(mi).getMessage());
                        msg.setJMSDestination(targetQueueAddr);
                        msg.setSourceRouter(null);
                        msg.setDestRouter(null);
                        QueuePushTransaction t = sender.createTransaction();
                        t.putMessage(msg);
                        t.commit();
                        cnt++;
                        if (remove)
                            sourceQueue.removeMessageByIndex(mi);
                        if (cnt == max)
                            break;
                    } catch (MessageLockedException ignored) {
                    }
                }
                if (i > stop)
                    break;
                i++;
            }
            return new String[] { TreeCommands.INFO, cnt + " messages processed." };
        }
    } catch (Exception e) {
        result = new String[] { TreeCommands.ERROR, e.getMessage() };
    }
    return result;
}
Also used : SortedSet(java.util.SortedSet) QueueImpl(com.swiftmq.jms.QueueImpl) Iterator(java.util.Iterator) TopicImpl(com.swiftmq.jms.TopicImpl) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 83 with MessageImpl

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

the class Copier method copyWithSelector.

private String[] copyWithSelector(String[] cmd) throws Exception {
    int max = getMaxLimit(cmd);
    int decr = max == Integer.MAX_VALUE ? 0 : 2;
    StringBuffer b = new StringBuffer();
    for (int i = 5; i < cmd.length - decr; i++) {
        if (i > 5)
            b.append(' ');
        b.append(cmd[i]);
    }
    MessageSelector selector = new MessageSelector(b.toString());
    selector.compile();
    QueueReceiver receiver = ctx.queueManager.createQueueReceiver(cmd[1], null, selector);
    QueuePullTransaction pullTx = receiver.createTransaction(false);
    QueueSender sender = null;
    QueueImpl targetQueueAddr = null;
    if (cmd[2].equals("-queue")) {
        sender = ctx.queueManager.createQueueSender(cmd[3], null);
        targetQueueAddr = new QueueImpl(cmd[3]);
    } else {
        String qft = ctx.topicManager.getQueueForTopic(cmd[3]);
        sender = ctx.queueManager.createQueueSender(qft, null);
        targetQueueAddr = new TopicImpl(cmd[3]);
    }
    QueuePushTransaction pushTx = sender.createTransaction();
    int cnt = 0;
    try {
        MessageEntry entry = null;
        while ((entry = pullTx.getMessage(0, selector)) != null && cnt < max) {
            MessageImpl msg = copyMessage(entry.getMessage());
            msg.setJMSDestination(targetQueueAddr);
            msg.setSourceRouter(null);
            msg.setDestRouter(null);
            pushTx.putMessage(msg);
            cnt++;
            pushTx.commit();
            if (remove) {
                pullTx.commit();
                pullTx = receiver.createTransaction(false);
            }
            pushTx = sender.createTransaction();
        }
    } finally {
        try {
            pullTx.rollback();
        } catch (Exception e) {
        }
        try {
            receiver.close();
        } catch (Exception e) {
        }
        try {
            pushTx.rollback();
        } catch (Exception e) {
        }
        try {
            sender.close();
        } catch (Exception e) {
        }
    }
    return new String[] { TreeCommands.INFO, cnt + " messages processed." };
}
Also used : QueueImpl(com.swiftmq.jms.QueueImpl) MessageSelector(com.swiftmq.ms.MessageSelector) TopicImpl(com.swiftmq.jms.TopicImpl) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 84 with MessageImpl

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

the class Copier 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 85 with MessageImpl

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

the class Exporter 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)

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