Search in sources :

Example 56 with QueueImpl

use of com.swiftmq.jms.QueueImpl 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 57 with QueueImpl

use of com.swiftmq.jms.QueueImpl 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 58 with QueueImpl

use of com.swiftmq.jms.QueueImpl 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 59 with QueueImpl

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

the class SessionStore method add.

public synchronized void add(String clientid, MQTTSession session) throws Exception {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", add, clientid=" + clientid);
    QueueSender sender = ctx.queueManager.createQueueSender(STORE_QUEUE, null);
    QueuePushTransaction t = sender.createTransaction();
    TextMessageImpl message = new TextMessageImpl();
    message.setJMSDestination(new QueueImpl(STORE_QUEUE));
    message.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
    message.setJMSPriority(Message.DEFAULT_PRIORITY);
    message.setJMSExpiration(Message.DEFAULT_TIME_TO_LIVE);
    message.setJMSMessageID(MSGID + IdGenerator.getInstance().nextId('/'));
    message.setStringProperty(PROP_CLIENTID, clientid);
    message.setText(xStream.toXML(session.getSessionStoreEntry()));
    t.putMessage(message);
    t.commit();
    sender.close();
}
Also used : TextMessageImpl(com.swiftmq.jms.TextMessageImpl) QueueImpl(com.swiftmq.jms.QueueImpl)

Example 60 with QueueImpl

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

the class TransactedQueueSession method visit.

public void visit(CreateProducerRequest req) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/visitCreateProducerRequest");
    CreateProducerReply reply = (CreateProducerReply) req.createReply();
    try {
        ctx.activeLogin.getResourceLimitGroup().incProducers();
    } catch (ResourceLimitException e) {
        reply.setOk(false);
        reply.setException(new JMSException(e.toString()));
        reply.send();
        return;
    }
    QueueImpl queue = req.getQueue();
    try {
        int producerId;
        QueueProducer producer;
        producerId = ArrayListTool.setFirstFreeOrExpand(producerList, null);
        producer = new QueueProducer(ctx, queue.getQueueName());
        producerList.set(producerId, producer);
        reply.setQueueProducerId(producerId);
        reply.setOk(true);
        if (senderEntityList != null) {
            Entity senderEntity = senderEntityList.createEntity();
            senderEntity.setName(queue.getQueueName() + "-" + producerId);
            senderEntity.setDynamicObject(producer);
            senderEntity.createCommands();
            Property prop = senderEntity.getProperty("queue");
            prop.setValue(queue.getQueueName());
            prop.setReadOnly(true);
            senderEntityList.addEntity(senderEntity);
        }
        // enlist it at the transaction manager
        transactionManager.addTransactionFactory(producer);
    } catch (Exception e) {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/exception creating producer: " + e.getMessage());
        ctx.logSwiftlet.logError("sys$jms", ctx.tracePrefix + "/exception creating producer: " + e.getMessage());
        reply.setOk(false);
        reply.setException(e);
        ctx.activeLogin.getResourceLimitGroup().decProducers();
    }
    reply.send();
}
Also used : Entity(com.swiftmq.mgmt.Entity) JMSException(javax.jms.JMSException) Property(com.swiftmq.mgmt.Property) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) QueueImpl(com.swiftmq.jms.QueueImpl) JMSException(javax.jms.JMSException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException)

Aggregations

QueueImpl (com.swiftmq.jms.QueueImpl)78 InvalidSelectorException (javax.jms.InvalidSelectorException)51 JMSException (javax.jms.JMSException)51 InvalidDestinationException (javax.jms.InvalidDestinationException)49 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)42 Entity (com.swiftmq.mgmt.Entity)35 Property (com.swiftmq.mgmt.Property)35 MessageImpl (com.swiftmq.jms.MessageImpl)21 QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)16 MessageSelector (com.swiftmq.ms.MessageSelector)9 RingBuffer (com.swiftmq.tools.collection.RingBuffer)7 DataByteArrayInputStream (com.swiftmq.tools.util.DataByteArrayInputStream)7 List (java.util.List)6 EntityList (com.swiftmq.mgmt.EntityList)5 BytesMessageImpl (com.swiftmq.jms.BytesMessageImpl)4 TopicImpl (com.swiftmq.jms.TopicImpl)4 SwiftletException (com.swiftmq.swiftlet.SwiftletException)3 TextMessageImpl (com.swiftmq.jms.TextMessageImpl)2 JNDIRequest (com.swiftmq.jndi.protocol.v400.JNDIRequest)2 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)2