Search in sources :

Example 61 with ConsumerUID

use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.

the class TransactionWorkMessageAck method readWork.

public void readWork(DataInputStream dis) throws IOException, BrokerException {
    String dest = dis.readUTF();
    destUID = new DestinationUID(dest);
    sysMessageID = new SysMessageID();
    sysMessageID.readID(dis);
    long cid = dis.readLong();
    consumerID = new ConsumerUID(cid);
}
Also used : DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) SysMessageID(com.sun.messaging.jmq.io.SysMessageID)

Example 62 with ConsumerUID

use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.

the class TxnConverter method getConsumedMessages.

void getConsumedMessages(TransactionInformation txnInfo, TransactionWork txnWork) throws BrokerException {
    // get acks for this txn
    LinkedHashMap<SysMessageID, List<ConsumerUID>> cm = txnInfo.getConsumedMessages(false);
    Iterator<Map.Entry<SysMessageID, List<ConsumerUID>>> iter = cm.entrySet().iterator();
    HashMap cuidToStored = transactionList.retrieveStoredConsumerUIDs(txnInfo.tid);
    while (iter.hasNext()) {
        Map.Entry<SysMessageID, List<ConsumerUID>> entry = iter.next();
        SysMessageID mid = entry.getKey();
        List<ConsumerUID> consumers = entry.getValue();
        PacketReference packRef = DL.get((PartitionedStore) store, mid);
        if (packRef == null) {
            String msg = getPrefix() + " convertLocalToTxnLogFormat: can not find packet for consumed msg" + mid + " in txn " + txnInfo;
            logger.log(Logger.WARNING, msg);
        } else {
            DestinationUID destUID = packRef.getDestination().getDestinationUID();
            if (consumers != null) {
                for (int i = 0; i < consumers.size(); i++) {
                    ConsumerUID cid = consumers.get(i);
                    ConsumerUID storedcid = (ConsumerUID) cuidToStored.get(cid);
                    if (storedcid == null) {
                        if (ToTxnLogConverter.DEBUG) {
                            String msg = getPrefix() + " storedcid=null for " + cid;
                            logger.log(Logger.DEBUG, msg);
                        }
                        storedcid = cid;
                    }
                    TransactionWorkMessageAck twma = new TransactionWorkMessageAck(destUID, mid, storedcid);
                    if (ToTxnLogConverter.DEBUG) {
                        String msg = getPrefix() + " convertLocalToTxnLogFormat: converting messageAck:" + " mid=" + mid + " destID=" + destUID + " consumerID=" + cid + " storedCid=" + storedcid + " txid=" + txnInfo.tid;
                        logger.log(Logger.DEBUG, msg);
                    }
                    txnWork.addMessageAcknowledgement(twma);
                }
            }
        }
    }
}
Also used : ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) PacketReference(com.sun.messaging.jmq.jmsserver.core.PacketReference) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) DestinationList(com.sun.messaging.jmq.jmsserver.core.DestinationList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 63 with ConsumerUID

use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.

the class TxnConverter method getSentMessages.

void getSentMessages(TransactionInformation txnInfo, TransactionWork txnWork) throws BrokerException {
    // get messages for this txn
    List<SysMessageID> sentMessageIds = txnInfo.getPublishedMessages();
    Iterator<SysMessageID> msgIter = sentMessageIds.iterator();
    while (msgIter.hasNext()) {
        SysMessageID mid = msgIter.next();
        PacketReference packRef = DL.get((PartitionedStore) store, mid);
        if (packRef == null) {
            String msg = getPrefix() + " convertLocalToTxnLogFormat: can not find packet for sent msg " + mid + " in txn " + txnInfo;
            logger.log(Logger.WARNING, msg);
        } else {
            DestinationUID destUID = packRef.getDestination().getDestinationUID();
            ConsumerUID[] interests = ((PartitionedStore) store).getConsumerUIDs(destUID, mid);
            TransactionWorkMessage twm = new TransactionWorkMessage();
            twm.setStoredInterests(interests);
            twm.setPacketReference(packRef);
            twm.setDestUID(destUID);
            txnWork.addMessage(twm);
        }
    }
}
Also used : DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) PacketReference(com.sun.messaging.jmq.jmsserver.core.PacketReference) SysMessageID(com.sun.messaging.jmq.io.SysMessageID) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)

Example 64 with ConsumerUID

use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.

the class AckHandler method handle.

/**
 * Method to handle Acknowledgement messages
 */
@Override
public boolean handle(IMQConnection con, Packet msg) throws BrokerException {
    int size = msg.getMessageBodySize();
    int ackcount = size / ACK_BLOCK_SIZE;
    int mod = size % ACK_BLOCK_SIZE;
    int status = Status.OK;
    String reason = null;
    if (DEBUG) {
        logger.log(Logger.INFO, "AckHandler: processing packet " + msg.toString() + ", on connection " + con);
    }
    PartitionedStore pstore = con.getPartitionedStore();
    TransactionList[] tls = DL.getTransactionList(pstore);
    TransactionList translist = tls[0];
    if (!con.isAdminConnection() && fi.FAULT_INJECTION) {
        // for fault injection
        ackProcessCnt++;
    } else {
        ackProcessCnt = 0;
    }
    if (ackcount == 0) {
        logger.log(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "Internal Error: Empty Ack Message " + msg.getSysMessageID().toString());
        reason = "Empty ack message";
        status = Status.ERROR;
    }
    if (mod != 0) {
        logger.log(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "Internal Error: Invalid Ack Message Size " + size + " for message " + msg.getSysMessageID().toString());
        reason = "corrupted ack message";
        status = Status.ERROR;
    }
    TransactionUID tid = null;
    if (msg.getTransactionID() != 0) {
        // HANDLE TRANSACTION
        try {
            tid = new TransactionUID(msg.getTransactionID());
        } catch (Exception ex) {
            logger.logStack(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "Internal Error: can not create transactionID for " + msg, ex);
            status = Status.ERROR;
        }
    }
    ArrayList<PacketReference> cleanList = new ArrayList<>();
    try {
        // cleanList
        Hashtable props = null;
        Throwable deadthr = null;
        String deadcmt = null;
        int deadrs = DEAD_REASON_UNDELIVERABLE;
        int deliverCnt = 0;
        boolean deliverCntUpdateOnly = false;
        int ackType = ACKNOWLEDGE_REQUEST;
        boolean JMQValidate = false;
        try {
            props = msg.getProperties();
            Integer iackType = (props == null ? null : (Integer) props.get("JMQAckType"));
            ackType = (iackType == null ? ACKNOWLEDGE_REQUEST : iackType.intValue());
            Boolean validateFlag = (props == null ? null : (Boolean) props.get("JMQValidate"));
            JMQValidate = validateFlag != null && validateFlag.booleanValue();
            checkRequestType(ackType);
            if (ackType == DEAD_REQUEST) {
                deadthr = (Throwable) props.get("JMQException");
                deadcmt = (String) props.get("JMQComment");
                Integer val = (Integer) props.get("JMQDeadReason");
                if (val != null) {
                    deadrs = val.intValue();
                }
            }
            if (props != null) {
                if (ackType == DEAD_REQUEST || ackType == UNDELIVERABLE_REQUEST || tid != null) {
                    // Client runtime retry count
                    Integer val = (Integer) props.get("JMSXDeliveryCount");
                    deliverCnt = (val == null ? -1 : val.intValue());
                    if (tid == null) {
                        if (deliverCnt >= 0) {
                            deliverCnt += 1;
                        }
                    }
                }
                if (ackType == UNDELIVERABLE_REQUEST) {
                    Boolean val = (Boolean) props.get("JMSXDeliveryCountUpdateOnly");
                    deliverCntUpdateOnly = val != null && val.booleanValue();
                }
            }
        } catch (Exception ex) {
            // assume not dead
            logger.logStack(Logger.INFO, "Internal Error: bad protocol", ex);
            ackType = ACKNOWLEDGE_REQUEST;
        }
        // OK .. handle Fault Injection
        if (!con.isAdminConnection() && fi.FAULT_INJECTION) {
            Map m = new HashMap();
            if (props != null) {
                m.putAll(props);
            }
            m.put("mqAckCount", Integer.valueOf(ackProcessCnt));
            m.put("mqIsTransacted", Boolean.valueOf(tid != null));
            fi.checkFaultAndExit(FaultInjection.FAULT_ACK_MSG_1, m, 2, false);
        }
        boolean remoteStatus = false;
        StringBuilder remoteConsumerUIDs = null;
        SysMessageID[] ids = null;
        ConsumerUID[] cids = null;
        try {
            if (status == Status.OK) {
                DataInputStream is = new DataInputStream(msg.getMessageBodyStream());
                // pull out the messages into two lists
                ids = new SysMessageID[ackcount];
                cids = new ConsumerUID[ackcount];
                for (int i = 0; i < ackcount; i++) {
                    long newid = is.readLong();
                    cids[i] = new ConsumerUID(newid);
                    cids[i].setConnectionUID(con.getConnectionUID());
                    ids[i] = new SysMessageID();
                    ids[i].readID(is);
                }
                if (JMQValidate) {
                    if (ackType == DEAD_REQUEST || ackType == UNDELIVERABLE_REQUEST) {
                        status = Status.BAD_REQUEST;
                        reason = "Can not use JMQValidate with ackType of " + ackType;
                    } else if (tid == null) {
                        status = Status.BAD_REQUEST;
                        reason = "Can not use JMQValidate with no tid ";
                    } else if (!validateMessages(translist, tid, ids, cids)) {
                        status = Status.NOT_FOUND;
                        reason = "Acknowledgement not processed";
                    }
                } else if (ackType == DEAD_REQUEST) {
                    handleDeadMsgs(con, ids, cids, deadrs, deadthr, deadcmt, deliverCnt, cleanList);
                } else if (ackType == UNDELIVERABLE_REQUEST) {
                    handleUndeliverableMsgs(con, ids, cids, cleanList, deliverCnt, deliverCntUpdateOnly);
                } else {
                    if (tid != null) {
                        handleTransaction(translist, con, tid, ids, cids, deliverCnt);
                    } else {
                        handleAcks(con, ids, cids, msg.getSendAcknowledge(), cleanList);
                    }
                }
            }
        } catch (Throwable thr) {
            status = Status.ERROR;
            if (thr instanceof BrokerException) {
                status = ((BrokerException) thr).getStatusCode();
                remoteStatus = ((BrokerException) thr).isRemote();
                if (remoteStatus && ids != null && cids != null) {
                    remoteConsumerUIDs = new StringBuilder();
                    remoteConsumerUIDs.append(((BrokerException) thr).getRemoteConsumerUIDs());
                    remoteConsumerUIDs.append(' ');
                    String cidstr = null;
                    ArrayList remoteConsumerUIDa = new ArrayList();
                    for (int i = 0; i < ids.length; i++) {
                        PacketReference ref = DL.get(pstore, ids[i]);
                        Consumer c = Consumer.getConsumer(cids[i]);
                        if (c == null) {
                            continue;
                        }
                        ConsumerUID sid = c.getStoredConsumerUID();
                        if (sid == null || sid.equals(cids[i])) {
                            continue;
                        }
                        BrokerAddress ba = (ref == null ? null : ref.getBrokerAddress());
                        BrokerAddress rba = (BrokerAddress) ((BrokerException) thr).getRemoteBrokerAddress();
                        if (ref != null && ba != null && rba != null && ba.equals(rba)) {
                            cidstr = String.valueOf(c.getConsumerUID().longValue());
                            if (!remoteConsumerUIDa.contains(cidstr)) {
                                remoteConsumerUIDa.add(cidstr);
                                remoteConsumerUIDs.append(cidstr);
                                remoteConsumerUIDs.append(' ');
                            }
                        }
                    }
                }
            }
            reason = thr.getMessage();
            if (status == Status.ERROR) {
                // something went wrong
                logger.logStack(Logger.ERROR, BrokerResources.E_INTERNAL_BROKER_ERROR, "-------------------------------------------" + "Internal Error: Invalid Acknowledge Packet processing\n" + " " + (msg.getSendAcknowledge() ? " notifying client\n" : " can not notify the client") + com.sun.messaging.jmq.io.PacketUtil.dumpPacket(msg) + "--------------------------------------------", thr);
            }
        }
        // OK .. handle Fault Injection
        if (!con.isAdminConnection() && fi.FAULT_INJECTION) {
            Map m = new HashMap();
            if (props != null) {
                m.putAll(props);
            }
            m.put("mqAckCount", Integer.valueOf(ackProcessCnt));
            m.put("mqIsTransacted", Boolean.valueOf(tid != null));
            fi.checkFaultAndExit(FaultInjection.FAULT_ACK_MSG_2, m, 2, false);
        }
        // send the reply (if necessary)
        if (msg.getSendAcknowledge()) {
            Packet pkt = new Packet(con.useDirectBuffers());
            pkt.setPacketType(PacketType.ACKNOWLEDGE_REPLY);
            pkt.setConsumerID(msg.getConsumerID());
            Hashtable hash = new Hashtable();
            hash.put("JMQStatus", Integer.valueOf(status));
            if (reason != null) {
                hash.put("JMQReason", reason);
            }
            if (remoteStatus) {
                hash.put("JMQRemote", Boolean.TRUE);
                if (remoteConsumerUIDs != null) {
                    hash.put("JMQRemoteConsumerIDs", remoteConsumerUIDs.toString());
                }
            }
            if (((IMQBasicConnection) con).getDumpPacket() || ((IMQBasicConnection) con).getDumpOutPacket()) {
                hash.put("JMQReqID", msg.getSysMessageID().toString());
            }
            pkt.setProperties(hash);
            con.sendControlMessage(pkt);
        }
        // OK .. handle Fault Injection
        if (!con.isAdminConnection() && fi.FAULT_INJECTION) {
            Map m = new HashMap();
            if (props != null) {
                m.putAll(props);
            }
            m.put("mqAckCount", Integer.valueOf(ackProcessCnt));
            m.put("mqIsTransacted", Boolean.valueOf(tid != null));
            fi.checkFaultAndExit(FaultInjection.FAULT_ACK_MSG_3, m, 2, false);
        }
    } finally {
        // we dont need to clear the memory up until after we reply
        cleanUp(cleanList);
    }
    return true;
}
Also used : BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) PacketReference(com.sun.messaging.jmq.jmsserver.core.PacketReference) PartitionedStore(com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) TransactionList(com.sun.messaging.jmq.jmsserver.data.TransactionList) TransactionAckExistException(com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException) UnknownTransactionException(com.sun.messaging.jmq.jmsserver.util.UnknownTransactionException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) BrokerAddress(com.sun.messaging.jmq.jmsserver.core.BrokerAddress) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID)

Example 65 with ConsumerUID

use of com.sun.messaging.jmq.jmsserver.core.ConsumerUID in project openmq by eclipse-ee4j.

the class AckHandler method handleTransaction.

public void handleTransaction(TransactionList translist, IMQConnection con, TransactionUID tid, SysMessageID[] ids, ConsumerUID[] cids, int deliverCnt) throws BrokerException {
    for (int i = 0; i < ids.length; i++) {
        Consumer consumer = null;
        try {
            // lookup the session by consumerUID
            Session s = Session.getSession(cids[i]);
            // look up the consumer by consumerUID
            consumer = Consumer.getConsumer(cids[i]);
            if (consumer == null) {
                throw new BrokerException(Globals.getBrokerResources().getKString(BrokerResources.I_ACK_FAILED_NO_CONSUMER, cids[i]) + "[TID=" + tid + "]", Status.NOT_FOUND);
            }
            // try and find the stored consumerUID
            ConsumerUID sid = consumer.getStoredConsumerUID();
            // if we still dont have a session, attempt to find one
            if (s == null) {
                SessionUID suid = consumer.getSessionUID();
                s = Session.getSession(suid);
            }
            if (s == null) {
                if (BrokerStateHandler.isShutdownStarted()) {
                    throw new BrokerException(BrokerResources.I_ACK_FAILED_BROKER_SHUTDOWN);
                }
                throw new BrokerException(br.getKString(br.I_ACK_FAILED_NO_SESSION, "[" + ids[i] + ", " + cids[i] + "]TID=" + tid));
            }
            if (DEBUG) {
                logger.log(logger.INFO, "handleTransaction.addAck[" + i + ", " + ids.length + "]:tid=" + tid + ", sysid=" + ids[i] + ", cid=" + cids[i] + ", sid=" + sid + " on connection " + con);
            }
            boolean isxa = translist.addAcknowledgement(tid, ids[i], cids[i], sid);
            BrokerAddress addr = (BrokerAddress) s.ackInTransaction(cids[i], ids[i], tid, isxa, deliverCnt);
            if (addr != null && addr != Globals.getMyAddress()) {
                translist.setAckBrokerAddress(tid, ids[i], cids[i], addr);
            }
            if (fi.FAULT_INJECTION) {
                if (fi.checkFault(fi.FAULT_TXN_ACK_1_5, null)) {
                    fi.unsetFault(fi.FAULT_TXN_ACK_1_5);
                    TransactionAckExistException tae = new TransactionAckExistException("FAULT:" + fi.FAULT_TXN_ACK_1_5, Status.GONE);
                    tae.setRemoteConsumerUIDs(String.valueOf(cids[i].longValue()));
                    tae.setRemote(true);
                    consumer.recreationRequested();
                    throw tae;
                }
            }
        } catch (Exception ex) {
            String emsg = "[" + ids[i] + ", " + cids[i] + "]TUID=" + tid;
            if ((ex instanceof BrokerException) && ((BrokerException) ex).getStatusCode() != Status.ERROR) {
                logger.log(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.E_TRAN_ACK_PROCESSING_FAILED, emsg, ex.getMessage()), ex);
            } else {
                logger.log(Logger.ERROR, Globals.getBrokerResources().getKString(BrokerResources.E_TRAN_ACK_PROCESSING_FAILED, emsg, ex.getMessage()), ex);
            }
            int state = -1;
            JMQXid xid = null;
            try {
                TransactionState ts = translist.retrieveState(tid);
                if (ts != null) {
                    state = ts.getState();
                    xid = ts.getXid();
                }
                translist.updateState(tid, TransactionState.FAILED, true);
            } catch (Exception e) {
                if (!(e instanceof UnknownTransactionException)) {
                    String[] args = { TransactionState.toString(state), TransactionState.toString(TransactionState.FAILED), tid.toString(), (xid == null ? "null" : xid.toString()) };
                    logger.log(Logger.WARNING, Globals.getBrokerResources().getKString(BrokerResources.W_UPDATE_TRAN_STATE_FAIL, args));
                }
            }
            if (ex instanceof TransactionAckExistException) {
                PacketReference ref = DL.get(null, ids[i]);
                if (ref != null && (ref.isOverrided() || ref.isOverriding())) {
                    ((BrokerException) ex).overrideStatusCode(Status.GONE);
                    ((BrokerException) ex).setRemoteConsumerUIDs(String.valueOf(cids[i].longValue()));
                    ((BrokerException) ex).setRemote(true);
                    consumer.recreationRequested();
                }
            }
            if (ex instanceof BrokerException) {
                throw (BrokerException) ex;
            }
            throw new BrokerException("Internal Error: Unable to " + " complete processing acknowledgements in a tranaction: " + ex, ex);
        }
    }
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) ConsumerUID(com.sun.messaging.jmq.jmsserver.core.ConsumerUID) SessionUID(com.sun.messaging.jmq.jmsserver.core.SessionUID) JMQXid(com.sun.messaging.jmq.util.JMQXid) BrokerAddress(com.sun.messaging.jmq.jmsserver.core.BrokerAddress) TransactionAckExistException(com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException) UnknownTransactionException(com.sun.messaging.jmq.jmsserver.util.UnknownTransactionException) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) TransactionAckExistException(com.sun.messaging.jmq.jmsserver.util.TransactionAckExistException) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) UnknownTransactionException(com.sun.messaging.jmq.jmsserver.util.UnknownTransactionException) PacketReference(com.sun.messaging.jmq.jmsserver.core.PacketReference) Session(com.sun.messaging.jmq.jmsserver.core.Session)

Aggregations

ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)83 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)29 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)28 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)27 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)22 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)21 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)21 Iterator (java.util.Iterator)21 HashMap (java.util.HashMap)19 Destination (com.sun.messaging.jmq.jmsserver.core.Destination)17 TransactionUID (com.sun.messaging.jmq.jmsserver.data.TransactionUID)16 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)15 Map (java.util.Map)15 List (java.util.List)13 BrokerAddress (com.sun.messaging.jmq.jmsserver.core.BrokerAddress)10 DestinationList (com.sun.messaging.jmq.jmsserver.core.DestinationList)10 Session (com.sun.messaging.jmq.jmsserver.core.Session)10 TransactionList (com.sun.messaging.jmq.jmsserver.data.TransactionList)10 AckEntryNotFoundException (com.sun.messaging.jmq.jmsserver.util.AckEntryNotFoundException)9