Search in sources :

Example 66 with Destination

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

the class FileStore method initTxnLogger.

// Initialize txn logging class
@Override
public boolean initTxnLogger() throws BrokerException {
    boolean storeNeedsRestart = false;
    if (removeStore || !Globals.txnLogEnabled()) {
        return storeNeedsRestart;
    }
    logger.log(logger.INFO, BrokerResources.I_TXNLOG_ENABLED);
    // create txn log writers
    String filename = null;
    try {
        SizeString filesize = config.getSizeProperty(TXNLOG_FILE_SIZE_PROP, DEFAULT_TXNLOG_FILE_SIZE);
        filename = MSG_LOG_FILENAME;
        msgLogWriter = new FileTransactionLogWriter(rootDir, filename, filesize.getBytes());
        msgLogWriter.setCheckPointListener(this);
        filename = ACK_LOG_FILENAME;
        ackLogWriter = new FileTransactionLogWriter(rootDir, filename, filesize.getBytes());
        ackLogWriter.setCheckPointListener(this);
        if (resetMessage || resetStore) {
            msgLogWriter.reset();
            ackLogWriter.reset();
            txnLoggerInited = true;
            return storeNeedsRestart;
        }
    } catch (IOException ex) {
        logger.logStack(Logger.ERROR, BrokerResources.E_CREATE_TXNLOG_FILE_FAILED, filename, ex);
        throw new BrokerException(br.getString(BrokerResources.E_CREATE_TXNLOG_FILE_FAILED, filename), ex);
    }
    // reconstruct persistence store if needed
    try {
        TransactionLogRecord rec;
        byte[] data;
        ByteArrayInputStream bis;
        DataInputStream dis;
        // Keep track of loaded dst
        HashSet dstLoadedSet = new HashSet();
        // Check to see if we need to process log files
        if (msgLogWriter.playBackRequired()) {
            storeNeedsRestart = true;
            logger.log(logger.FORCE, BrokerResources.I_PROCESS_MSG_TXNLOG);
            // All destinations need to be loaded
            Globals.getCoreLifecycle().initDestinations();
            Globals.getCoreLifecycle().initSubscriptions();
            int count = 0;
            Iterator itr = msgLogWriter.iterator();
            while (itr.hasNext()) {
                // Keep track the number of records processed
                count++;
                // Read in the messages
                rec = (TransactionLogRecord) itr.next();
                int recType = rec.getType();
                if (recType != TransactionLogType.PRODUCE_TRANSACTION) {
                    // Shouldn't happens
                    logger.log(logger.ERROR, BrokerResources.E_PROCESS_TXNLOG_RECORD_FAILED, String.valueOf(rec.getSequence()), "record type " + recType + " is invalid");
                    continue;
                }
                data = rec.getBody();
                bis = new ByteArrayInputStream(data);
                dis = new DataInputStream(bis);
                // Transaction ID
                long tid = dis.readLong();
                String tidStr = String.valueOf(tid);
                logger.log(logger.FORCE, BrokerResources.I_PROCESS_TXNLOG_RECORD, tidStr, String.valueOf(recType));
                // Process all msgs in the txn
                processTxnRecMsgPart(dis, dstLoadedSet);
                // Check to see if we need to commit the txn
                if (tid > 0) {
                    TransactionUID tuid = new TransactionUID(tid);
                    TransactionState state = tidList.getTransactionState(tuid);
                    if (state.getState() != TransactionState.NULL && state.getState() != TransactionState.COMMITTED) {
                        logger.log(logger.FORCE, BrokerResources.I_COMMIT_TXNLOG_RECORD, tidStr);
                        tidList.updateTransactionState(tuid, state, false);
                    }
                }
                dis.close();
                bis.close();
            }
            logger.log(logger.FORCE, BrokerResources.I_LOAD_MSG_TXNLOG, String.valueOf(count));
            logger.flush();
        }
        // of record, we'll just store it the same file for simplicity.
        if (ackLogWriter.playBackRequired()) {
            storeNeedsRestart = true;
            logger.log(logger.FORCE, BrokerResources.I_PROCESS_ACK_TXNLOG);
            // All destinations need to be loaded
            Globals.getCoreLifecycle().initDestinations();
            Globals.getCoreLifecycle().initSubscriptions();
            int count = 0;
            Iterator itr = ackLogWriter.iterator();
            while (itr.hasNext()) {
                // Keep track the number of records processed
                count++;
                // Read in the acks or msgs & acks
                rec = (TransactionLogRecord) itr.next();
                int recType = rec.getType();
                if (!(recType == TransactionLogType.CONSUME_TRANSACTION || recType == TransactionLogType.PRODUCE_AND_CONSUME_TRANSACTION)) {
                    // shouldn't happens
                    logger.log(logger.ERROR, BrokerResources.E_PROCESS_TXNLOG_RECORD_FAILED, String.valueOf(rec.getSequence()), "record type " + recType + " is invalid");
                    continue;
                }
                data = rec.getBody();
                bis = new ByteArrayInputStream(data);
                dis = new DataInputStream(bis);
                // Transaction ID
                long tid = dis.readLong();
                String tidStr = String.valueOf(tid);
                logger.log(logger.FORCE, BrokerResources.I_PROCESS_TXNLOG_RECORD, tidStr, String.valueOf(recType));
                if (recType == TransactionLogType.PRODUCE_AND_CONSUME_TRANSACTION) {
                    // Process all msgs in the txn first!
                    processTxnRecMsgPart(dis, dstLoadedSet);
                }
                // Process all acks in the txn
                processTxnRecAckPart(dis, dstLoadedSet);
                // Check to see if we need to commit the txn
                TransactionUID tuid = new TransactionUID(tid);
                TransactionState state = tidList.getTransactionState(tuid);
                if (state.getState() != TransactionState.NULL && state.getState() != TransactionState.COMMITTED) {
                    logger.log(logger.FORCE, BrokerResources.I_COMMIT_TXNLOG_RECORD, tidStr);
                    tidList.updateTransactionState(tuid, state, false);
                }
                dis.close();
                bis.close();
            }
            logger.log(logger.FORCE, BrokerResources.I_LOAD_ACK_TXNLOG, String.valueOf(count));
            logger.flush();
        }
        if (storeNeedsRestart) {
            // Now unload all the destinations that we've loaded so msgs can be routed correctly later on by the broker
            Iterator itr = dstLoadedSet.iterator();
            while (itr.hasNext()) {
                Destination d = (Destination) itr.next();
                // Sync changes to disk
                syncDestination(d);
                d.unload(true);
            }
            dstLoadedSet = null;
            // Sync changes to txn tables
            tidList.sync(null);
            tidList.syncTransactionAck(null);
            // Reset the txn log after the store is updated & synced
            msgLogWriter.reset();
            ackLogWriter.reset();
            logger.log(logger.FORCE, BrokerResources.I_RECONSTRUCT_STORE_DONE);
            logger.flush();
        }
    } catch (Throwable t) {
        logger.logStack(Logger.ERROR, BrokerResources.E_RECONSTRUCT_STORE_FAILED, t);
        throw new BrokerException(br.getString(BrokerResources.E_RECONSTRUCT_STORE_FAILED), t);
    }
    txnLoggerInited = true;
    return storeNeedsRestart;
}
Also used : TransactionState(com.sun.messaging.jmq.jmsserver.data.TransactionState) Destination(com.sun.messaging.jmq.jmsserver.core.Destination) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) TransactionUID(com.sun.messaging.jmq.jmsserver.data.TransactionUID) SizeString(com.sun.messaging.jmq.util.SizeString) ByteArrayInputStream(java.io.ByteArrayInputStream) TransactionLogRecord(com.sun.messaging.jmq.io.txnlog.TransactionLogRecord) Iterator(java.util.Iterator) FileTransactionLogWriter(com.sun.messaging.jmq.io.txnlog.file.FileTransactionLogWriter) HashSet(java.util.HashSet)

Example 67 with Destination

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

the class MsgStore method messageEnumeration.

/**
 * Return an enumeration of all persisted messages. Use the Enumeration methods on the returned object to fetch and load
 * each message sequentially. Not synchronized.
 *
 * <p>
 * This method is to be used at broker startup to load persisted messages on demand.
 *
 * @return an enumeration of all persisted messages.
 */
Enumeration messageEnumeration() {
    Enumeration msgEnum = new Enumeration() {

        // get all destinations
        Iterator dstitr = parent.getDstStore().getDestinations().iterator();

        Enumeration tempenum = null;

        Object nextToReturn = null;

        // will enumerate through all messages of all destinations
        @Override
        public boolean hasMoreElements() {
            while (true) {
                if (tempenum != null) {
                    if (tempenum.hasMoreElements()) {
                        // got the next message
                        nextToReturn = tempenum.nextElement();
                        return true;
                    } else {
                        // continue to get the next enumeration
                        tempenum = null;
                    }
                } else {
                    // get next enumeration
                    while (dstitr.hasNext()) {
                        Destination dst = (Destination) dstitr.next();
                        try {
                            // got the next enumeration
                            tempenum = messageEnumeration(dst.getDestinationUID());
                            break;
                        } catch (BrokerException e) {
                            // log error and try to load messages for
                            // the next destionation
                            logger.log(logger.ERROR, br.X_LOAD_MESSAGES_FOR_DST_FAILED, dst.getDestinationUID(), e);
                        }
                    }
                    if (tempenum == null) {
                        // no more
                        return false;
                    }
                }
            }
        }

        @Override
        public Object nextElement() {
            if (nextToReturn != null) {
                Object tmp = nextToReturn;
                nextToReturn = null;
                return tmp;
            } else {
                throw new NoSuchElementException();
            }
        }
    };
    return msgEnum;
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) Enumeration(java.util.Enumeration) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException)

Example 68 with Destination

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

the class GetConsumersHandler method handle.

/**
 * Handle the incomming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    if (DEBUG) {
        logger.log(Logger.DEBUG, this.getClass().getName() + ": " + cmd_props);
    }
    String destination = (String) cmd_props.get(MessageType.JMQ_DESTINATION);
    Integer destType = (Integer) cmd_props.get(MessageType.JMQ_DEST_TYPE);
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    int status = Status.OK;
    Vector v = new Vector();
    String errMsg = null;
    if ((destination == null) || (destType == null)) {
        errMsg = "Destination name and type not specified";
        logger.log(Logger.ERROR, errMsg);
        status = Status.ERROR;
    }
    if (destination != null) {
        try {
            Destination[] ds = DL.getDestination(null, destination, DestType.isQueue(destType.intValue()));
            // PART
            Destination d = ds[0];
            if (d != null) {
                Iterator cons = d.getConsumers();
                while (cons.hasNext()) {
                    Consumer oneCon = (Consumer) cons.next();
                    HashMap h = constructConsumerInfo(oneCon.getConsumerUID(), d);
                    v.add(h);
                }
            } else {
                errMsg = rb.getString(rb.X_DESTINATION_NOT_FOUND, destination);
                status = Status.NOT_FOUND;
            }
        } catch (Exception ex) {
            logger.logStack(Logger.ERROR, ex.getMessage(), ex);
            status = Status.ERROR;
            assert false;
        }
    }
    setProperties(reply, MessageType.GET_CONSUMERS_REPLY, status, errMsg);
    setBodyObject(reply, v);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) Consumer(com.sun.messaging.jmq.jmsserver.core.Consumer) HashMap(java.util.HashMap) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 69 with Destination

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

the class GetDestinationsHandler method handle.

/**
 * Handle the incomming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    if (DEBUG) {
        logger.log(Logger.INFO, "GetDestiantionsHandler: " + cmd_props);
    }
    Vector v = new Vector();
    int status = Status.OK;
    String errMsg = null;
    String destination = (String) cmd_props.get(MessageType.JMQ_DESTINATION);
    Integer destType = (Integer) cmd_props.get(MessageType.JMQ_DEST_TYPE);
    Boolean val = (Boolean) cmd_props.get(MessageType.JMQ_SHOW_PARTITION);
    boolean showpartition = (val != null && val.booleanValue());
    val = (Boolean) cmd_props.get(MessageType.JMQ_LOAD_DESTINATION);
    boolean load = (val != null && val.booleanValue());
    assert destination == null || destType != null;
    if (destination != null) {
        try {
            Destination[] ds = DL.getDestination(null, destination, DestType.isQueue(destType.intValue()));
            Destination d = null;
            DestinationInfo dinfo = null;
            for (int i = 0; i < ds.length; i++) {
                d = ds[i];
                if (d != null) {
                    if (DEBUG) {
                        d.debug();
                    }
                    if (load) {
                        d.load();
                    }
                    dinfo = getDestinationInfo(d, dinfo, showpartition);
                    if (showpartition) {
                        v.add(dinfo);
                    }
                }
            }
            if (dinfo == null) {
                throw new BrokerException(rb.getString(rb.X_DESTINATION_NOT_FOUND, destination), Status.NOT_FOUND);
            }
            if (!showpartition) {
                v.add(dinfo);
            }
        } catch (Exception ex) {
            status = Status.ERROR;
            errMsg = ex.getMessage();
            if (ex instanceof BrokerException) {
                status = ((BrokerException) ex).getStatusCode();
            }
            logger.logStack(Logger.ERROR, errMsg, ex);
        }
    } else {
        try {
            LinkedHashMap<DestinationUID, DestinationInfo> map = new LinkedHashMap<>();
            Iterator[] itrs = DL.getAllDestinations(null);
            int cnt = itrs.length;
            DestinationInfo dinfo = null;
            DestinationUID duid = null;
            Destination d = null;
            for (int i = 0; i < cnt; i++) {
                Iterator itr = itrs[i];
                while (itr.hasNext()) {
                    d = (Destination) itr.next();
                    if (load) {
                        d.load();
                    }
                    duid = d.getDestinationUID();
                    dinfo = map.get(d.getDestinationUID());
                    dinfo = getDestinationInfo(d, dinfo, showpartition);
                    map.put(duid, dinfo);
                    if (showpartition) {
                        v.add(dinfo);
                    }
                }
            }
            if (!showpartition) {
                Iterator<DestinationInfo> itr = map.values().iterator();
                while (itr.hasNext()) {
                    v.add(itr.next());
                }
            }
        } catch (Exception ex) {
            status = Status.ERROR;
            errMsg = ex.getMessage();
            if (ex instanceof BrokerException) {
                status = ((BrokerException) ex).getStatusCode();
            }
            logger.logStack(Logger.ERROR, errMsg, ex);
        }
    }
    // Send reply
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    setProperties(reply, MessageType.GET_DESTINATIONS_REPLY, status, errMsg);
    setBodyObject(reply, v);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) DestinationInfo(com.sun.messaging.jmq.util.admin.DestinationInfo) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) SizeString(com.sun.messaging.jmq.util.SizeString) BrokerException(com.sun.messaging.jmq.jmsserver.util.BrokerException) LinkedHashMap(java.util.LinkedHashMap) DestinationUID(com.sun.messaging.jmq.jmsserver.core.DestinationUID) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 70 with Destination

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

the class GetMessagesHandler method handle.

/**
 * Handle the incomming administration message.
 *
 * @param con The Connection the message came in on.
 * @param cmd_msg The administration message
 * @param cmd_props The properties from the administration message
 */
@Override
public boolean handle(IMQConnection con, Packet cmd_msg, Hashtable cmd_props) {
    if (DEBUG) {
        logger.log(Logger.DEBUG, this.getClass().getName() + ": " + "Getting messages: " + cmd_props);
    }
    Vector v = new Vector();
    int status = Status.OK;
    String errMsg = null;
    String destination = (String) cmd_props.get(MessageType.JMQ_DESTINATION);
    Integer destType = (Integer) cmd_props.get(MessageType.JMQ_DEST_TYPE);
    Long startIndex = (Long) cmd_props.get(MessageType.JMQ_START_MESSAGE_INDEX), maxNumMsgs = (Long) cmd_props.get(MessageType.JMQ_MAX_NUM_MSGS_RETRIEVED);
    Boolean getBody = (Boolean) cmd_props.get(MessageType.JMQ_GET_MSG_BODY);
    String msgID = (String) cmd_props.get(MessageType.JMQ_MESSAGE_ID);
    HashMap destNameType = new HashMap();
    if ((destination == null) || (destType == null)) {
        errMsg = "Destination name and type not specified";
        logger.log(Logger.ERROR, errMsg);
        status = Status.ERROR;
    }
    if (getBody == null) {
        getBody = Boolean.FALSE;
    }
    if (destination != null) {
        try {
            Destination[] ds = DL.getDestination(null, destination, DestType.isQueue(destType.intValue()));
            Destination d = ds[0];
            if (d != null) {
                if (DEBUG) {
                    d.debug();
                }
                if (msgID != null) {
                    d.load();
                    SysMessageID sysMsgID = SysMessageID.get(msgID);
                    PacketReference pr = getPacketReference(sysMsgID);
                    if (pr != null) {
                        HashMap h = constructMessageInfo(sysMsgID, getBody.booleanValue(), destNameType);
                        if (h == null) {
                            pr = null;
                        } else {
                            v.add(h);
                        }
                    }
                    if (pr == null) {
                        /*
                             * errMsg= rb.getString(rb.X_MSG_NOT_FOUND, msgID);
                             */
                        errMsg = "Could not locate message " + msgID + " in destination " + destination;
                        status = Status.NOT_FOUND;
                    }
                } else {
                    SysMessageID[] sysMsgIDs = d.getSysMessageIDs(startIndex, maxNumMsgs);
                    for (int i = 0; i < sysMsgIDs.length; ++i) {
                        HashMap h = constructMessageInfo(sysMsgIDs[i], getBody.booleanValue(), destNameType);
                        if (h == null) {
                            continue;
                        }
                        v.add(h);
                    }
                }
            } else {
                errMsg = rb.getString(rb.X_DESTINATION_NOT_FOUND, destination);
                status = Status.NOT_FOUND;
            }
        } catch (Exception ex) {
            logger.logStack(Logger.ERROR, ex.getMessage(), ex);
            ex.printStackTrace();
            status = Status.ERROR;
            assert false;
        }
    }
    // Send reply
    Packet reply = new Packet(con.useDirectBuffers());
    reply.setPacketType(PacketType.OBJECT_MESSAGE);
    setProperties(reply, MessageType.GET_MESSAGES_REPLY, status, errMsg);
    setBodyObject(reply, v);
    parent.sendReply(con, cmd_msg, reply);
    return true;
}
Also used : Destination(com.sun.messaging.jmq.jmsserver.core.Destination) HashMap(java.util.HashMap) PacketReference(com.sun.messaging.jmq.jmsserver.core.PacketReference) Vector(java.util.Vector)

Aggregations

Destination (com.sun.messaging.jmq.jmsserver.core.Destination)76 BrokerException (com.sun.messaging.jmq.jmsserver.util.BrokerException)39 Iterator (java.util.Iterator)29 DestinationUID (com.sun.messaging.jmq.jmsserver.core.DestinationUID)25 PacketReference (com.sun.messaging.jmq.jmsserver.core.PacketReference)25 ConsumerUID (com.sun.messaging.jmq.jmsserver.core.ConsumerUID)20 SelectorFormatException (com.sun.messaging.jmq.util.selector.SelectorFormatException)18 HashMap (java.util.HashMap)18 Consumer (com.sun.messaging.jmq.jmsserver.core.Consumer)17 IOException (java.io.IOException)16 SysMessageID (com.sun.messaging.jmq.io.SysMessageID)15 ArrayList (java.util.ArrayList)12 List (java.util.List)11 Packet (com.sun.messaging.jmq.io.Packet)9 AckEntryNotFoundException (com.sun.messaging.jmq.jmsserver.util.AckEntryNotFoundException)9 Map (java.util.Map)9 DestinationList (com.sun.messaging.jmq.jmsserver.core.DestinationList)8 SizeString (com.sun.messaging.jmq.util.SizeString)8 PartitionedStore (com.sun.messaging.jmq.jmsserver.persist.api.PartitionedStore)7 ConsumerAlreadyAddedException (com.sun.messaging.jmq.jmsserver.util.ConsumerAlreadyAddedException)7