use of com.swiftmq.swiftlet.queue.QueueReceiver 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.v942.SMQRFactory.ROUTE_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.ADJUST_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.TRANSACTION_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.COMMIT_REQ, null);
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.COMMIT_REPREQ, null);
}
use of com.swiftmq.swiftlet.queue.QueueReceiver in project swiftmq-ce by iitsoftware.
the class XADeliveryStage method init.
protected void init() {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init...");
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.START_STAGE_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + "...");
int txSize = ((Integer) routingConnection.getEntity().getProperty("inbound-transaction-size").getValue()).intValue();
int windowSize = ((Integer) routingConnection.getEntity().getProperty("inbound-window-size").getValue()).intValue();
AdjustRequest rc = new AdjustRequest(txSize, windowSize);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + ", sending request=" + rc);
routingConnection.getOutboundQueue().enqueue(rc);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.SEND_ROUTE_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + "...");
RouteRequest rc = new RouteRequest(((SendRouteRequest) request).getRoute());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + ", sending request=" + rc);
routingConnection.getOutboundQueue().enqueue(rc);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.ROUTE_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + "...");
RouteRequest rc = (RouteRequest) request;
try {
ctx.routeExchanger.processRoute(routingConnection, rc.getRoute(ctx.routeExchanger.getRouteConverter()));
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + " exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + " exception=" + e);
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.ADJUST_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + "...");
AdjustRequest rc = (AdjustRequest) request;
routingConnection.setTransactionSize(rc.getTransactionSize());
routingConnection.setWindowSize(rc.getWindowSize());
// It then sends a request by itself to ensure the XADeliveryStage is active at the connector side.
if (listener)
getStageQueue().enqueue(new StartStageRequest());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + ", activating connection");
routingConnection.getActivationListener().activated(routingConnection);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.SMQRFactory.DELIVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + "...");
try {
DeliveryRequest rc = (DeliveryRequest) request;
QueuePullTransaction srcTx = rc.readTransaction;
QueueReceiver receiver = (QueueReceiver) consumers.get(srcTx.getQueueName());
if (receiver == null) {
receiver = ctx.queueManager.createQueueReceiver(srcTx.getQueueName(), null, null);
consumers.put(srcTx.getQueueName(), receiver);
}
QueuePullTransaction destTx = receiver.createTransaction(false);
List al = new ArrayList(rc.len);
for (int i = 0; i < rc.len; i++) {
destTx.moveToTransaction(rc.entries[i].getMessageIndex(), srcTx);
MessageImpl msg = rc.entries[i].getMessage();
if (msg.getSourceRouter() == null)
msg.setSourceRouter(ctx.routerName);
if (msg.getDestRouter() == null)
msg.setDestRouter(rc.destinationRouter);
al.add(rc.entries[i].getMessage());
}
StringBuffer b = new StringBuffer(txBase);
b.append(txNo);
XidImpl xid = new XidImpl(recoveryBranchQB, txNo, b.toString().getBytes());
xid.setRouting(true);
destTx.prepare(xid);
outboundTransactions.put(xid, destTx);
TransactionRequest txr = new TransactionRequest(txNo, xid, al);
txNo++;
if (txNo == Integer.MAX_VALUE)
txNo = 0;
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + " sending request=" + txr);
routingConnection.getOutboundQueue().enqueue(txr);
if (outboundTransactions.size() <= routingConnection.getWindowSize())
rc.callback.delivered(rc);
else
notificationList.put(xid, rc);
} catch (Exception e) {
e.printStackTrace();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + " exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + " exception=" + e + ", disconnecting");
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.COMMIT_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + "...");
CommitRequest rc = (CommitRequest) request;
XidImpl xid = rc.getXid();
QueuePullTransaction t = (QueuePullTransaction) outboundTransactions.remove(xid);
try {
t.commit(xid);
CommitReplyRequest crr = new CommitReplyRequest(xid);
crr.setOk(true);
routingConnection.getOutboundQueue().enqueue(crr);
DeliveryRequest dr = (DeliveryRequest) notificationList.remove(xid);
if (dr != null) {
dr.callback.delivered(dr);
}
} catch (Exception e) {
e.printStackTrace();
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("OUTBOUND") + "/visited, request=" + request + " exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + " exception=" + e + ", disconnecting");
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.TRANSACTION_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + "...");
TransactionRequest rc = (TransactionRequest) request;
try {
processTransactionRequest(rc);
CommitRequest cr = new CommitRequest(rc.getXid());
if (throttleQueue != null)
throttleQueue.enqueue(cr);
else
routingConnection.getOutboundQueue().enqueue(cr);
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + " exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + " exception=" + e + ", disconnecting");
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.COMMIT_REPREQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + "...");
CommitReplyRequest rc = (CommitReplyRequest) request;
try {
if (!rc.isOk())
throw new Exception("Reply states not ok: " + rc);
long delay = commitLocalXid(rc.getXid());
if (delay > 0 && throttleQueue != null)
throttleQueue.enqueue(new ThrottleRequest(delay));
} catch (Exception e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString("INBOUND") + "/visited, request=" + request + " exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XADeliveryStage.this.toString() + "/visited, request=" + request + " exception=" + e + ", disconnecting");
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
if (!listener)
getStageQueue().enqueue(new StartStageRequest());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init done");
}
use of com.swiftmq.swiftlet.queue.QueueReceiver in project swiftmq-ce by iitsoftware.
the class QueueInput method start.
@Override
public void start() throws Exception {
if (started)
return;
MessageSelector ms = null;
if (selector != null) {
ms = new MessageSelector(selector);
ms.compile();
}
QueueReceiver receiver = ctx.ctx.queueManager.createQueueReceiver(destinationName, (ActiveLogin) null, ms);
messageProcessor = new QueueMessageProcessor(ctx, this, receiver, ms);
messageProcessor.restart();
started = true;
}
use of com.swiftmq.swiftlet.queue.QueueReceiver in project swiftmq-ce by iitsoftware.
the class TopicInput method start.
@Override
public void start() throws Exception {
if (started)
return;
if (!ctx.ctx.topicManager.isTopicDefined(destinationName))
ctx.ctx.topicManager.createTopic(destinationName);
MessageSelector ms = null;
if (selector != null) {
ms = new MessageSelector(selector);
ms.compile();
}
if (durable) {
ActiveLogin dlogin = ctx.ctx.authenticationSwiftlet.createActiveLogin(clientId, "DURABLE");
dlogin.setClientId(clientId);
TopicImpl topic = ctx.ctx.topicManager.verifyTopic(new TopicImpl(destinationName));
queueName = ctx.ctx.topicManager.subscribeDurable(durableName, topic, ms, false, dlogin);
} else {
queueName = ctx.ctx.queueManager.createTemporaryQueue();
subscriberId = ctx.ctx.topicManager.subscribe(destinationName, ms, false, queueName, true);
}
QueueReceiver receiver = ctx.ctx.queueManager.createQueueReceiver(queueName, (ActiveLogin) null, null);
messageProcessor = new QueueMessageProcessor(ctx, this, receiver, null);
messageProcessor.restart();
started = true;
}
use of com.swiftmq.swiftlet.queue.QueueReceiver 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