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;
}
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;
}
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();
}
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." };
}
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." };
}
Aggregations