Search in sources :

Example 21 with XidImpl

use of com.swiftmq.jms.XidImpl 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");
}
Also used : QueuePushTransaction(com.swiftmq.swiftlet.queue.QueuePushTransaction) XidImpl(com.swiftmq.jms.XidImpl) QueueSender(com.swiftmq.swiftlet.queue.QueueSender) MessageImpl(com.swiftmq.jms.MessageImpl) JMSException(javax.jms.JMSException) XAContextException(com.swiftmq.swiftlet.xa.XAContextException)

Example 22 with XidImpl

use of com.swiftmq.jms.XidImpl 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");
}
Also used : XidImpl(com.swiftmq.jms.XidImpl) Request(com.swiftmq.tools.requestreply.Request) QueuePullTransaction(com.swiftmq.swiftlet.queue.QueuePullTransaction) JMSException(javax.jms.JMSException) XAContextException(com.swiftmq.swiftlet.xa.XAContextException) QueueReceiver(com.swiftmq.swiftlet.queue.QueueReceiver) MessageImpl(com.swiftmq.jms.MessageImpl)

Example 23 with XidImpl

use of com.swiftmq.jms.XidImpl in project swiftmq-ce by iitsoftware.

the class XARecoveryStage method doRecover.

private void doRecover(List localXids, List remoteXids) {
    // Nothing to recover
    if (localXids == null && remoteXids == null) {
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, nothing to do");
        return;
    }
    List remoteRecoveryList = new ArrayList();
    // Check local vs remote
    if (localXids != null) {
        for (int i = 0; i < localXids.size(); i++) {
            XidImpl lXid = (XidImpl) localXids.get(i);
            if (containsXid(remoteXids, lXid)) {
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + lXid + "], local prepared, remote prepared, commit 1:local, 2:remote");
                // local prepared, remote prepared, commit 1:local, 2:remote
                XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(lXid);
                try {
                    xac.commit(false);
                } catch (XAContextException e) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, exception=" + e);
                    ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/doRecover, commit, exception=" + e);
                }
                ctx.xaResourceManagerSwiftlet.removeXAContext(lXid);
                remoteRecoveryList.add(new CommitRequest(lXid));
            } else {
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + lXid + "], local prepared, remote unknown, rollback local");
                // local prepared, remote unknown, rollback local
                XAContext xac = ctx.xaResourceManagerSwiftlet.getXAContext(lXid);
                try {
                    xac.rollback();
                } catch (XAContextException e) {
                    if (ctx.traceSpace.enabled)
                        ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover, exception=" + e);
                    ctx.logSwiftlet.logError(ctx.routingSwiftlet.getName(), toString() + "/doRecover, rollback, exception=" + e);
                }
                ctx.xaResourceManagerSwiftlet.removeXAContext(lXid);
            }
        }
    }
    // Check remote Xids not known locally
    if (remoteXids != null) {
        for (int i = 0; i < remoteXids.size(); i++) {
            XidImpl rXid = (XidImpl) remoteXids.get(i);
            if (!containsXid(localXids, rXid)) {
                if (ctx.traceSpace.enabled)
                    ctx.traceSpace.trace(ctx.routingSwiftlet.getName(), toString() + "/doRecover [" + rXid + "], remote prepared, locally already committed, commit remote");
                // remote prepared, locally already committed, commit remote
                remoteRecoveryList.add(new CommitRequest(rXid));
            }
        }
    }
    if (remoteRecoveryList.size() > 0) {
        Collections.sort(remoteRecoveryList, new Comparator() {

            public int compare(Object o1, Object o2) {
                int i1 = ((CommitRequest) o1).getXid().getFormatId();
                int i2 = ((CommitRequest) o2).getXid().getFormatId();
                return i1 == i2 ? 0 : i1 < i2 ? -1 : 1;
            }
        });
        for (int i = 0; i < remoteRecoveryList.size(); i++) {
            CommitRequest r = (CommitRequest) remoteRecoveryList.get(i);
            routingConnection.getOutboundQueue().enqueue(r);
        }
    }
}
Also used : CommitRequest(com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest) XAContext(com.swiftmq.swiftlet.xa.XAContext) XidImpl(com.swiftmq.jms.XidImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) XAContextException(com.swiftmq.swiftlet.xa.XAContextException) Comparator(java.util.Comparator)

Example 24 with XidImpl

use of com.swiftmq.jms.XidImpl in project swiftmq-ce by iitsoftware.

the class CommitReplyRequest method readContent.

public void readContent(DataInput input) throws IOException {
    super.readContent(input);
    xid = new XidImpl();
    xid.readContent(input);
}
Also used : XidImpl(com.swiftmq.jms.XidImpl)

Example 25 with XidImpl

use of com.swiftmq.jms.XidImpl in project swiftmq-client by iitsoftware.

the class XAResourceImpl method commit.

public synchronized void commit(Xid xid, boolean onePhase) throws XAException {
    if (logWriter != null)
        log(toString() + "/commit, xid=" + xid + ", onePhase=" + onePhase);
    XidImpl sxid = toSwiftMQXid(xid);
    xidMapping.remove(xid);
    XAResCommitReply reply = null;
    try {
        int connectionId = session.getSessionImpl().getMyConnection().getConnectionId();
        XAResCommitRequest req = new XAResCommitRequest(this, session.getDispatchId(), sxid, onePhase, false, onePhase && endRequestInDoubt() ? XARecoverRegistry.getInstance().getRequestList(sxid) : null);
        req.setConnectionId(connectionId);
        reply = (XAResCommitReply) session.request(req);
    } catch (Exception e) {
        if (completionListener != null)
            completionListener.transactionCommitted(sxid);
        XAException ex = new XAException(e.toString());
        ex.errorCode = XAException.XAER_RMFAIL;
        throw ex;
    }
    if (!reply.isOk()) {
        if (completionListener != null)
            completionListener.transactionCommitted(sxid);
        XAException ex = new XAException(reply.getException().getMessage());
        ex.errorCode = reply.getErrorCode();
        throw ex;
    } else {
        try {
            session.getSessionImpl().afterCommit();
        } catch (JMSException e) {
            XAException ex = new XAException(e.toString());
            ex.errorCode = XAException.XAER_RMFAIL;
            throw ex;
        }
        if (reply.getDelay() > 0) {
            try {
                Thread.sleep(reply.getDelay());
            } catch (Exception ignored) {
            }
        }
        if (completionListener != null)
            completionListener.transactionCommitted(sxid);
        XARecoverRegistry.getInstance().clear(sxid);
    }
}
Also used : XAException(javax.transaction.xa.XAException) XidImpl(com.swiftmq.jms.XidImpl) JMSException(javax.jms.JMSException) ValidationException(com.swiftmq.tools.requestreply.ValidationException) JMSException(javax.jms.JMSException) XAException(javax.transaction.xa.XAException)

Aggregations

XidImpl (com.swiftmq.jms.XidImpl)85 XAException (javax.transaction.xa.XAException)37 JMSException (javax.jms.JMSException)28 ValidationException (com.swiftmq.tools.requestreply.ValidationException)22 Request (com.swiftmq.tools.requestreply.Request)16 List (java.util.List)12 ArrayList (java.util.ArrayList)10 XAContextException (com.swiftmq.swiftlet.xa.XAContextException)9 MessageImpl (com.swiftmq.jms.MessageImpl)6 XAContext (com.swiftmq.swiftlet.xa.XAContext)5 QueuePullTransaction (com.swiftmq.swiftlet.queue.QueuePullTransaction)4 QueueReceiver (com.swiftmq.swiftlet.queue.QueueReceiver)4 QueueSender (com.swiftmq.swiftlet.queue.QueueSender)4 BytesMessageImpl (com.swiftmq.jms.BytesMessageImpl)2 QueuePushTransaction (com.swiftmq.swiftlet.queue.QueuePushTransaction)2 Comparator (java.util.Comparator)2 CommitRequest (com.swiftmq.impl.routing.single.smqpr.v400.CommitRequest)1 CommitRequest (com.swiftmq.impl.routing.single.smqpr.v942.CommitRequest)1 QueueImpl (com.swiftmq.jms.QueueImpl)1 Entity (com.swiftmq.mgmt.Entity)1