use of com.swiftmq.tools.requestreply.Request in project swiftmq-client by iitsoftware.
the class XAResourceImpl method rollback.
public synchronized void rollback(Xid xid) throws XAException {
if (logWriter != null)
log(toString() + "/rollback, xid=" + xid);
XidImpl sxid = toSwiftMQXid(xid);
xidMapping.remove(xid);
XAResRollbackReply reply = null;
try {
int connectionId = session.getSessionImpl().getMyConnection().getConnectionId();
session.getSessionImpl().startRecoverConsumers();
session.getAndClearCurrentTransaction();
List recoveryList = null;
if (endRequestInDoubt())
recoveryList = XARecoverRegistry.getInstance().getRequestList(sxid);
Request request = new XAResRollbackRequest(this, session.getDispatchId(), sxid, false, recoveryList, session.getSessionImpl().getRecoveryEpoche());
request.setConnectionId(connectionId);
reply = (XAResRollbackReply) session.request(request);
} catch (Exception e) {
if (completionListener != null)
completionListener.transactionAborted(sxid);
XAException ex = new XAException(e.toString());
ex.errorCode = XAException.XAER_RMFAIL;
throw ex;
}
if (reply.isOk()) {
session.getSessionImpl().endRecoverConsumersXA();
try {
session.getSessionImpl().closeDelayedProducers();
} catch (JMSException e) {
e.printStackTrace();
}
if (completionListener != null)
completionListener.transactionAborted(sxid);
XARecoverRegistry.getInstance().clear(sxid);
} else {
if (completionListener != null)
completionListener.transactionAborted(sxid);
XAException ex = new XAException(reply.getException().getMessage());
if (reply.getErrorCode() != 0)
ex.errorCode = reply.getErrorCode();
else
ex.errorCode = XAException.XAER_RMFAIL;
throw ex;
}
}
use of com.swiftmq.tools.requestreply.Request in project swiftmq-ce by iitsoftware.
the class InboundReader method dataAvailable.
public void dataAvailable(Connection connection, InputStream inputStream) throws IOException {
dis.setInputStream(inputStream);
Dumpable obj = Dumpalizer.construct(dis, dumpableFactory);
if (traceSpace.enabled)
traceSpace.trace("smqp", "read object: " + obj);
if (obj.getDumpId() != SMQPFactory.DID_KEEPALIVE_REQ) {
if (obj.getDumpId() == SMQPFactory.DID_BULK_REQ) {
SMQPBulkRequest bulkRequest = (SMQPBulkRequest) obj;
for (int i = 0; i < bulkRequest.len; i++) {
Request req = (Request) bulkRequest.dumpables[i];
if (req.getDumpId() != SMQPFactory.DID_KEEPALIVE_REQ)
dispatch(req);
else
resetKeepaliveCount();
}
} else
dispatch((Request) obj);
} else
resetKeepaliveCount();
}
use of com.swiftmq.tools.requestreply.Request in project swiftmq-ce by iitsoftware.
the class ThrottleQueue method process.
protected void process(Object[] objects, int len) {
for (int i = 0; i < len; i++) {
Request r = (Request) objects[i];
if (r.getDumpId() == SMQRFactory.THROTTLE_REQ) {
sem.reset();
sem.waitHere(((ThrottleRequest) r).getDelay());
} else {
outboundQueue.enqueue(r);
}
if (closed)
return;
}
}
use of com.swiftmq.tools.requestreply.Request 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.tools.requestreply.Request in project swiftmq-ce by iitsoftware.
the class XARecoveryStage 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(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryRequest rc = new RecoveryRequest();
rc.setBranchQualifier(recoveryBranchQ);
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", sending request=" + rc);
routingConnection.getOutboundQueue().enqueue(rc);
routingConnection.setXaSelected(true);
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.RECOVERY_REPREQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + "...");
RecoveryReplyRequest reply = (RecoveryReplyRequest) request;
if (reply.isOk()) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", do recovery");
// do recovery
List localXids = getPreparedXids(new Filter(recoveryBranchQ));
List remoteXids = reply.getXidList();
doRecover(localXids, remoteXids);
localRecovered = true;
if (remoteRecovered) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", start remote delivery");
// start delivery
routingConnection.getOutboundQueue().enqueue(new StartDeliveryRequest());
} else {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", disconnect");
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/exception: " + reply.getException());
ctx.networkSwiftlet.getConnectionManager().removeConnection(routingConnection.getConnection());
}
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.RECOVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request);
// It then sends a request by itself to ensure the XARecoveryStage is active at the connector side.
if (listener)
getStageQueue().enqueue(new StartStageRequest());
RecoveryRequest pr = (RecoveryRequest) request;
RecoveryReplyRequest reply = new RecoveryReplyRequest();
reply.setOk(true);
// fill xid list
reply.setXidList(getPreparedXids(new Filter(pr.getBranchQualifier())));
routingConnection.getOutboundQueue().enqueue(reply);
remoteRecovered = true;
}
});
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(), XARecoveryStage.this.toString() + "/visited, request=" + request);
CommitRequest cr = (CommitRequest) request;
XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(cr.getXid());
try {
xac.commit(false);
} catch (XAContextException e) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/visited, request=" + request + ", exception=" + e);
}
ctx.xaResourceManagerSwiftlet.removeXAContext(cr.getXid());
}
});
visitor.setRequestHandler(com.swiftmq.impl.routing.single.smqpr.v942.SMQRFactory.STARTDELIVERY_REQ, new RequestHandler() {
public void visited(Request request) {
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), XARecoveryStage.this.toString() + "/visited, request=" + request + ", launching delivery stage");
if (localRecovered && remoteRecovered)
getStageQueue().setStage(new XADeliveryStage(ctx, routingConnection));
}
});
if (!listener)
getStageQueue().enqueue(new StartStageRequest());
if (ctx.traceSpace.enabled)
ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/init done");
}
Aggregations