use of com.swiftmq.swiftlet.queue.QueueSender in project swiftmq-ce by iitsoftware.
the class RequestVisitor method visit.
public void visit(LookupRequest request) {
String name = request.getName();
Object object = ctx.jndiSwiftlet.getJNDIObject(name);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": request '" + name + " returns: " + object);
if (object == null) {
if (forwardToTopic) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": not found, forward to topic " + JNDISwiftlet.JNDI_TOPIC);
try {
QueueSender sender = ctx.queueManager.createQueueSender(ctx.topicManager.getQueueForTopic(JNDISwiftlet.JNDI_TOPIC), null);
QueuePushTransaction transaction = sender.createTransaction();
Versionable versionable = new Versionable();
versionable.addVersioned(400, createVersioned(400, request), "com.swiftmq.jndi.protocol.v400.JNDIRequestFactory");
BytesMessageImpl msg = createMessage(versionable, replyToQueue);
transaction.putMessage(msg);
transaction.commit();
sender.close();
} catch (Exception e1) {
e1.printStackTrace();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": exception occurred while forwarding lookup request: " + e1);
}
}
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": replyTo " + replyToQueue);
if (replyToQueue != null) {
try {
QueueSender sender = ctx.queueManager.createQueueSender(replyToQueue.getQueueName(), null);
QueuePushTransaction transaction = sender.createTransaction();
MessageImpl reply = createReply(object);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": sending reply " + reply);
transaction.putMessage(reply);
transaction.commit();
sender.close();
} catch (Exception e1) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": exception occurred while sending reply: " + e1);
}
} else if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": replyTo is not set. Unable to send.");
}
}
use of com.swiftmq.swiftlet.queue.QueueSender in project swiftmq-ce by iitsoftware.
the class RequestVisitor method visit.
public void visit(RebindRequest request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request);
String name = request.getName();
QueueImpl queue = request.getQueue();
String msg = null;
boolean registered = false;
try {
String queueName = queue.getQueueName();
if (queueName.endsWith(SwiftletManager.getInstance().getRouterName())) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + " is local");
registered = true;
if (ctx.jndiSwiftlet.getJNDIObject(name) != null)
throw new Exception("rebind failed, object '" + name + "' is already registered!");
String oldName = ctx.jndiSwiftlet.getJNDIObjectName(queue);
if (oldName != null) {
Object ro = ctx.jndiSwiftlet.getJNDIObject(oldName);
if (ro != null) {
if (ro.getClass() != queue.getClass())
throw new Exception("rebind failed; attempt to overwrite a different registration class!");
ctx.jndiSwiftlet.deregisterJNDIObject(oldName);
}
}
ctx.jndiSwiftlet.registerJNDIObject(name, queue);
}
} catch (Exception e) {
msg = e.getMessage();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": exception: " + msg);
}
if (registered) {
if (replyToQueue != null) {
try {
QueueSender sender = ctx.queueManager.createQueueSender(replyToQueue.getQueueName(), null);
QueuePushTransaction transaction = sender.createTransaction();
TextMessageImpl reply = new TextMessageImpl();
reply.setJMSDeliveryMode(javax.jms.DeliveryMode.NON_PERSISTENT);
reply.setJMSDestination(replyToQueue);
reply.setJMSPriority(MessageImpl.MAX_PRIORITY);
reply.setText(msg);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": sending reply " + reply);
transaction.putMessage(reply);
transaction.commit();
sender.close();
} catch (Exception e1) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": exception occurred while sending reply: " + e1);
}
} else if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.jndiSwiftlet.getName(), "visitor " + request + ": replyTo is not set. Unable to send.");
}
}
use of com.swiftmq.swiftlet.queue.QueueSender 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." };
}
use of com.swiftmq.swiftlet.queue.QueueSender in project swiftmq-ce by iitsoftware.
the class XADeliveryStage method processTransactionRequest.
private void processTransactionRequest(TransactionRequest request) throws Exception {
XidImpl xid = request.getXid();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + " ...");
Tx tx = new Tx(xid);
inboundTransactions.put(xid, tx);
List messageList = request.getMessageList();
for (int i = 0; i < messageList.size(); i++) {
boolean msgValid = true;
MessageImpl msg = (MessageImpl) messageList.get(i);
String queueName = null;
if (msg.getDestRouter().equals(ctx.routerName))
queueName = msg.getDestQueue();
else
queueName = SchedulerRegistry.QUEUE_PREFIX + msg.getDestRouter();
QueueSender sender = (QueueSender) producers.get(queueName);
if (sender == null) {
try {
sender = ctx.queueManager.createQueueSender(queueName, null);
producers.put(queueName, sender);
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", exception creating sender, queue=" + queueName);
if (ctx.queueManager.isTemporaryQueue(queueName)) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", temp queue, forget it");
msgValid = false;
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", using unroutable queue");
sender = (QueueSender) producers.get(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
msg.setStringProperty(MessageImpl.PROP_UNROUTABLE_REASON, e.toString());
}
}
}
if (msgValid) {
try {
QueuePushTransaction t = tx.getTransaction(queueName);
if (t == null) {
t = sender.createTransaction();
tx.addTransaction(queueName, t);
}
t.putMessage(msg);
} catch (Exception e) {
try {
if (!sender.getQueueName().startsWith(RoutingSwiftletImpl.UNROUTABLE_QUEUE))
sender.close();
} catch (Exception e1) {
}
producers.remove(queueName);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", exception put message, queue=" + queueName);
if (ctx.queueManager.isTemporaryQueue(queueName)) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", temp queue, forget it");
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + ", using unroutable queue");
sender = (QueueSender) producers.get(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
msg.setStringProperty(MessageImpl.PROP_UNROUTABLE_REASON, e.toString());
QueuePushTransaction t = tx.getTransaction(RoutingSwiftletImpl.UNROUTABLE_QUEUE);
if (t == null) {
t = sender.createTransaction();
tx.addTransaction(RoutingSwiftletImpl.UNROUTABLE_QUEUE, t);
}
t.putMessage(msg);
}
}
}
}
try {
tx.prepare();
} catch (Exception e) {
tx.rollback();
inboundTransactions.remove(xid);
throw e;
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString("INBOUND") + "/processTransactionRequest, xid=" + xid + " done");
}
use of com.swiftmq.swiftlet.queue.QueueSender in project swiftmq-ce by iitsoftware.
the class XADeliveryStage method close.
public void close() {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close ...");
super.close();
closed = true;
if (notificationList.size() > 0) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, final notify ...");
for (Iterator iter = notificationList.entrySet().iterator(); iter.hasNext(); ) {
Map.Entry entry = (Map.Entry) iter.next();
DeliveryRequest dr = (DeliveryRequest) entry.getValue();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, final notify: " + dr);
dr.callback.delivered(dr);
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, final notify done");
notificationList.clear();
}
if (outboundTransactions.size() > 0) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, passing prepared outbound tx to XAResourceManager ...");
for (Iterator iter = outboundTransactions.entrySet().iterator(); iter.hasNext(); ) {
try {
Map.Entry entry = (Map.Entry) iter.next();
XidImpl xid = (XidImpl) entry.getKey();
XAContext xac = ctx.xaResourceManagerSwiftlet.createXAContext(xid);
int id = xac.register(toString());
QueuePullTransaction t = (QueuePullTransaction) entry.getValue();
xac.addTransaction(id, t.getQueueName(), t);
xac.unregister(id, false);
xac.setPrepared(true);
} catch (XAContextException e) {
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/close, passing prepared outbound tx to XAResourceManager, exception: " + e);
}
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, passing prepared outbound tx to XAResourceManager done");
outboundTransactions.clear();
}
if (inboundTransactions.size() > 0) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, passing prepared inbound tx to XAResourceManager ...");
for (Iterator iter = inboundTransactions.entrySet().iterator(); iter.hasNext(); ) {
((Tx) ((Map.Entry) iter.next()).getValue()).handOver();
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/close, passing prepared inbound tx to XAResourceManager done");
}
for (Iterator iter = producers.entrySet().iterator(); iter.hasNext(); ) {
QueueSender sender = (QueueSender) ((Map.Entry) iter.next()).getValue();
try {
sender.close();
} catch (Exception e) {
}
}
producers.clear();
for (Iterator iter = consumers.entrySet().iterator(); iter.hasNext(); ) {
QueueReceiver receiver = (QueueReceiver) ((Map.Entry) iter.next()).getValue();
try {
receiver.close();
} catch (Exception e) {
}
}
consumers.clear();
if (throttleQueue != null)
throttleQueue.close();
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.START_STAGE_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.SEND_ROUTE_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.DELIVERY_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.ROUTE_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.ADJUST_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.TRANSACTION_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.COMMIT_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v400.SMQRFactory.COMMIT_REPREQ, null);
}
Aggregations