Search in sources :

Example 21 with MessageSelector

use of com.swiftmq.ms.MessageSelector in project swiftmq-ce by iitsoftware.

the class QueueMemory method select.

@Override
public Memory select(String selector) throws Exception {
    MessageSelector sel = new MessageSelector(selector);
    sel.compile();
    Memory child = new HeapMemory(ctx);
    for (int i = 0; i < messageStore.size(); i++) {
        Message message = at(i);
        if (message.isSelected(sel))
            child.add(message);
    }
    return child;
}
Also used : Message(com.swiftmq.impl.streams.comp.message.Message) MessageSelector(com.swiftmq.ms.MessageSelector)

Example 22 with MessageSelector

use of com.swiftmq.ms.MessageSelector in project swiftmq-ce by iitsoftware.

the class QueueMemory method remove.

@Override
public Memory remove(String selector) throws Exception {
    MessageSelector sel = new MessageSelector(selector);
    sel.compile();
    List<KeyEntry> list = messageStore.all();
    for (int i = 0; i < list.size(); i++) {
        KeyEntry keyEntry = list.get(i);
        if (keyEntry.messageIndex != null) {
            MessageEntry entry = abstractQueue.getMessageByIndex(keyEntry.messageIndex);
            if (sel.isSelected(entry.getMessage())) {
                messageStore.remove(keyEntry.key);
                txBookKeeper.remove(keyEntry.key, keyEntry);
                removeFromIndexes(keyEntry.key, ctx.messageBuilder.wrap(entry.getMessage()));
            }
        }
    }
    txBookKeeper.remove(sel);
    return this;
}
Also used : MessageSelector(com.swiftmq.ms.MessageSelector)

Example 23 with MessageSelector

use of com.swiftmq.ms.MessageSelector in project swiftmq-ce by iitsoftware.

the class BrowserManager method createBrowser.

public void createBrowser(CreateBrowserRequest request) {
    CreateBrowserReply reply = (CreateBrowserReply) request.createReply();
    QueueImpl queue = request.getQueue();
    String messageSelector = request.getMessageSelector();
    MessageSelector msel = null;
    String queueName = null;
    try {
        queueName = queue.getQueueName();
    } catch (JMSException ignored) {
    }
    try {
        if (messageSelector != null) {
            msel = new MessageSelector(messageSelector);
            msel.compile();
        }
        if (!ctx.queueManager.isQueueRunning(queueName)) {
            ctx.logSwiftlet.logWarning("sys$jms", ctx.tracePrefix + "/" + toString() + ": Invalid destination: " + queueName);
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/" + toString() + ": Invalid destination: " + queue);
            reply.setOk(false);
            reply.setException(new InvalidDestinationException("Invalid destination: " + queueName));
        } else {
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/" + toString() + ": Creating browser with selector: " + msel);
            com.swiftmq.swiftlet.queue.QueueBrowser queueBrowser = ctx.queueManager.createQueueBrowser(queueName, ctx.activeLogin, msel);
            int idx = ArrayListTool.setFirstFreeOrExpand(queueBrowsers, queueBrowser);
            reply.setOk(true);
            reply.setQueueBrowserId(idx);
            if (browserEntityList != null) {
                Entity browserEntity = browserEntityList.createEntity();
                browserEntity.setName(queueName + "-" + idx);
                browserEntity.setDynamicObject(queueBrowser);
                browserEntity.createCommands();
                Property prop = browserEntity.getProperty("queue");
                prop.setValue(queueName);
                prop.setReadOnly(true);
                prop = browserEntity.getProperty("selector");
                if (msel != null) {
                    prop.setValue(msel.getConditionString());
                }
                prop.setReadOnly(true);
                browserEntityList.addEntity(browserEntity);
            }
        }
    } catch (InvalidSelectorException e) {
        ctx.logSwiftlet.logWarning("sys$jms", ctx.tracePrefix + "/" + toString() + ": CreateBrowser has invalid Selector: " + e);
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/" + toString() + ": CreateBrowser has invalid Selector: " + e);
        reply.setOk(false);
        reply.setException(e);
    } catch (Exception e) {
        ctx.logSwiftlet.logWarning("sys$jms", ctx.tracePrefix + "/" + toString() + ": Exception during createQueueBrowser: " + e);
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$jms", ctx.tracePrefix + "/" + toString() + ": Exception during createQueueBrowser: " + e);
        reply.setOk(false);
        reply.setException(e);
    }
    reply.send();
}
Also used : Entity(com.swiftmq.mgmt.Entity) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) QueueImpl(com.swiftmq.jms.QueueImpl) InvalidSelectorException(javax.jms.InvalidSelectorException) InvalidDestinationException(javax.jms.InvalidDestinationException) JMSException(javax.jms.JMSException) MessageSelector(com.swiftmq.ms.MessageSelector) Property(com.swiftmq.mgmt.Property)

Example 24 with MessageSelector

use of com.swiftmq.ms.MessageSelector 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 25 with MessageSelector

use of com.swiftmq.ms.MessageSelector in project swiftmq-ce by iitsoftware.

the class Exporter 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.EXPORT + " <queuename> <localdir> [-remove] [-xml] [-selector <selector>]" };
    int nMsgs = 0;
    try {
        String queueName = cmd[1];
        String localDir = cmd[2];
        boolean delete = false;
        boolean xml = false;
        String selector = null;
        if (cmd.length > 3) {
            StringBuffer selBuffer = null;
            for (int i = 3; i < cmd.length; i++) {
                if (selBuffer != null) {
                    if (selBuffer.length() > 0)
                        selBuffer.append(' ');
                    selBuffer.append(cmd[i]);
                } else if (cmd[i].equals("-delete"))
                    delete = true;
                else if (cmd[i].equals("-xml"))
                    xml = true;
                else if (cmd[i].equals("-selector"))
                    selBuffer = new StringBuffer();
                else
                    throw new Exception("Invalid option: " + cmd[i]);
            }
            if (selBuffer != null)
                selector = selBuffer.toString();
        }
        XStream xStream = null;
        File outputDir = new File(localDir);
        if (!outputDir.exists()) {
            if (!outputDir.mkdir())
                throw new Exception("Unable to create output directory: " + localDir);
        }
        if (xml) {
            xStream = new XStream(new Dom4JDriver());
            xStream.allowTypesByWildcard(new String[] { ".*" });
        }
        MessageSelector msel = null;
        if (selector != null) {
            msel = new MessageSelector(selector);
            msel.compile();
        }
        QueueReceiver receiver = ctx.queueManager.createQueueReceiver(queueName, null, msel);
        QueuePullTransaction pullTx = receiver.createTransaction(false);
        try {
            MessageEntry entry = null;
            while ((entry = pullTx.getMessage(0, msel)) != null) {
                MessageImpl msg = delete ? entry.getMessage() : copyMessage(entry.getMessage());
                msg.clearSwiftMQAllProps();
                store(queueName, xStream, outputDir, nMsgs++, msg);
                if (delete) {
                    pullTx.commit();
                    pullTx = receiver.createTransaction(false);
                }
            }
        } finally {
            try {
                pullTx.rollback();
            } catch (Exception e) {
            }
            try {
                receiver.close();
            } catch (Exception e) {
            }
        }
    } catch (Exception e) {
        return new String[] { TreeCommands.ERROR, e.getMessage() };
    }
    return new String[] { TreeCommands.INFO, nMsgs + " messages exported." };
}
Also used : XStream(com.thoughtworks.xstream.XStream) Dom4JDriver(com.thoughtworks.xstream.io.xml.Dom4JDriver) QueuePullTransaction(com.swiftmq.swiftlet.queue.QueuePullTransaction) QueueReceiver(com.swiftmq.swiftlet.queue.QueueReceiver) MessageEntry(com.swiftmq.swiftlet.queue.MessageEntry) MessageSelector(com.swiftmq.ms.MessageSelector) File(java.io.File) MessageImpl(com.swiftmq.jms.MessageImpl)

Aggregations

MessageSelector (com.swiftmq.ms.MessageSelector)31 InvalidSelectorException (javax.jms.InvalidSelectorException)11 QueueImpl (com.swiftmq.jms.QueueImpl)9 InvalidDestinationException (javax.jms.InvalidDestinationException)8 JMSException (javax.jms.JMSException)8 Entity (com.swiftmq.mgmt.Entity)7 Property (com.swiftmq.mgmt.Property)7 MessageImpl (com.swiftmq.jms.MessageImpl)5 TopicImpl (com.swiftmq.jms.TopicImpl)4 IllegalStateException (javax.jms.IllegalStateException)4 QueueReceiver (com.swiftmq.swiftlet.queue.QueueReceiver)3 JobException (com.swiftmq.swiftlet.scheduler.JobException)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 Message (com.swiftmq.impl.streams.comp.message.Message)2 QueueMessageProcessor (com.swiftmq.impl.streams.processor.QueueMessageProcessor)2 EntityRemoveException (com.swiftmq.mgmt.EntityRemoveException)2 ActiveLogin (com.swiftmq.swiftlet.auth.ActiveLogin)2 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)2 AbstractQueue (com.swiftmq.swiftlet.queue.AbstractQueue)2