Search in sources :

Example 26 with QmfException

use of org.apache.qpid.qmf2.common.QmfException in project qpid by apache.

the class QpidQueueStats method onEvent.

/**
 * Main Event handler. Checks if the WorkItem is a SubscriptionIndicationWorkItem, if it is it stores the object
 * in a Map and uses this to maintain state so we can record deltas such as enqueue and dequeue rates.
 * <p>
 * The AgentHeartbeatWorkItem is used to periodically compare the elapsed time against the Subscription duration
 * so that we can refresh the Subscription (or create a new one if necessary) in order to continue receiving
 * queue Management Object data from the broker.
 * <p>
 * When the AgentRestartedWorkItem is received we clear the state to remove any stale queue Management Objects.
 * @param wi a QMF2 WorkItem object
 */
public void onEvent(final WorkItem wi) {
    if (wi instanceof AgentHeartbeatWorkItem && _subscriptionId != null) {
        long elapsed = (long) Math.round((System.currentTimeMillis() - _startTime) / 1000.0f);
        if (elapsed > _subscriptionDuration) {
            try {
                _console.refreshSubscription(_subscriptionId);
                _startTime = System.currentTimeMillis();
            } catch (QmfException qmfe) {
                System.err.println("QmfException " + qmfe.getMessage() + " caught in QpidQueueStats onEvent");
                createQueueSubscription();
            }
        }
    } else if (wi instanceof AgentRestartedWorkItem) {
        _objects.clear();
    } else if (wi instanceof SubscriptionIndicationWorkItem) {
        SubscriptionIndicationWorkItem item = (SubscriptionIndicationWorkItem) wi;
        SubscribeIndication indication = item.getSubscribeIndication();
        String correlationId = indication.getConsoleHandle();
        if (correlationId.equals("queueStatsHandle")) {
            // If it is (and it should be!!) then it's our queue object Subscription
            List<QmfConsoleData> data = indication.getData();
            for (QmfConsoleData record : data) {
                ObjectId id = record.getObjectId();
                if (record.isDeleted()) {
                    // If the object was deleted by the Agent we remove it from out Map
                    _objects.remove(id);
                } else {
                    if (_objects.containsKey(id)) {
                        // If the object is already in the Map it's likely to be a statistics push from the broker.
                        Stats stats = _objects.get(id);
                        String name = stats.getName();
                        boolean matches = false;
                        for (Pattern x : _filter) {
                            // Check the queue name against the regexes in the filter List (if any)
                            Matcher m = x.matcher(name);
                            if (m.find()) {
                                matches = true;
                                break;
                            }
                        }
                        if (_filter.isEmpty() || matches) {
                            // If there's no filter enabled or the filter matches the queue name we display statistics.
                            QmfConsoleData lastSample = stats.getData();
                            stats.setData(record);
                            float deltaTime = record.getUpdateTime() - lastSample.getUpdateTime();
                            if (deltaTime > 1000000000.0f) {
                                float deltaEnqueues = record.getLongValue("msgTotalEnqueues") - lastSample.getLongValue("msgTotalEnqueues");
                                float deltaDequeues = record.getLongValue("msgTotalDequeues") - lastSample.getLongValue("msgTotalDequeues");
                                long msgDepth = record.getLongValue("msgDepth");
                                float enqueueRate = deltaEnqueues / (deltaTime / 1000000000.0f);
                                float dequeueRate = deltaDequeues / (deltaTime / 1000000000.0f);
                                System.out.printf("%-46s%10.2f%11d%13.2f%13.2f\n", name, deltaTime / 1000000000, msgDepth, enqueueRate, dequeueRate);
                            }
                        }
                    } else {
                        // If the object isn't in the Map it's likely to be a properties push from the broker.
                        if (!record.hasValue("name")) {
                            // This probably won't happen, but if it does we refresh the object to get its full state.
                            try {
                                record.refresh();
                            } catch (QmfException qmfe) {
                            }
                        }
                        String queueName = record.getStringValue("name");
                        _objects.put(id, new Stats(queueName, record));
                    }
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) AgentRestartedWorkItem(org.apache.qpid.qmf2.console.AgentRestartedWorkItem) SubscriptionIndicationWorkItem(org.apache.qpid.qmf2.console.SubscriptionIndicationWorkItem) ObjectId(org.apache.qpid.qmf2.common.ObjectId) Matcher(java.util.regex.Matcher) AgentHeartbeatWorkItem(org.apache.qpid.qmf2.console.AgentHeartbeatWorkItem) SubscribeIndication(org.apache.qpid.qmf2.console.SubscribeIndication) QmfConsoleData(org.apache.qpid.qmf2.console.QmfConsoleData) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 27 with QmfException

use of org.apache.qpid.qmf2.common.QmfException in project qpid by apache.

the class Agent method removeConnection.

// end of setConnection()
/**
 * Remove the AMQP connection from the Agent. Un-does the setConnection() operation.
 *
 * @param conn a javax.jms.Connection.
 */
public final void removeConnection(final Connection conn) throws QmfException {
    if (conn != _connection) {
        throw new QmfException("Attempt to delete unknown connection");
    }
    try {
        _timer.cancel();
        _connection.close();
    } catch (JMSException jmse) {
        throw new QmfException("Failed to remove connection, caught JMSException " + jmse.getMessage());
    }
    _connection = null;
}
Also used : JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 28 with QmfException

use of org.apache.qpid.qmf2.common.QmfException in project qpid by apache.

the class Agent method addObject.

/**
 * Passes a reference to an instance of a managed QMF object to the Agent.
 * <p>
 * The object's name must uniquely identify this object among all objects known to this Agent.
 * <p>
 * This method creates an ObjectId for the QmfAgentData being added, it does this by first checking
 * the schema.
 * <p>
 * If an associated schema exists we look for the set of property names that have been
 * specified as idNames. If idNames exists we look for their values within the object and use that
 * to create the objectName. If we can't create a sensible name we use a randomUUID.
 * @param object the QmfAgentData object to be added
 */
public void addObject(final QmfAgentData object) throws QmfException {
    // There are some cases where a QmfAgentData Object might have already set its ObjectId, for example where
    // it may need to have a "well known" ObjectId. This is the case with the Java Broker Management Agent
    // where tools such as qpid-config might have made assumptions about its ObjectId rather than doing "discovery".
    ObjectId addr = object.getObjectId();
    if (addr == null) {
        SchemaClassId classId = object.getSchemaClassId();
        SchemaClass schema = _schemaCache.get(classId);
        // Try to create an objectName using the property names that have been specified as idNames in the schema
        StringBuilder buf = new StringBuilder();
        // Initialise idNames as an empty array as we want to check if a key has been used to construct the name.
        String[] idNames = {};
        if (schema != null && schema instanceof SchemaObjectClass) {
            idNames = ((SchemaObjectClass) schema).getIdNames();
            for (String property : idNames) {
                buf.append(object.getStringValue(property));
            }
        }
        String objectName = buf.toString();
        // exchange has name == "")
        if (objectName.length() == 0 && idNames.length == 0)
            objectName = UUID.randomUUID().toString();
        // Finish up the name by incorporating package and class names
        objectName = classId.getPackageName() + ":" + classId.getClassName() + ":" + objectName;
        // Now we've got a good name for the object we create its ObjectId and add that to the object
        addr = new ObjectId(_name, objectName, _epoch);
        object.setObjectId(addr);
    }
    QmfAgentData foundObject = _objectIndex.get(addr);
    if (foundObject != null) {
        // If a duplicate object has actually been Deleted we can reuse the address.
        if (!foundObject.isDeleted()) {
            throw new QmfException("Duplicate QmfAgentData Address");
        }
    }
    _objectIndex.put(addr, object);
    // Does the new object match any Subscriptions? If so add a reference to the matching Subscription and publish.
    for (Subscription subscription : _subscriptions.values()) {
        QmfQuery query = subscription.getQuery();
        if (query.getObjectId() != null) {
            if (query.getObjectId().equals(addr)) {
                object.addSubscription(subscription.getSubscriptionId(), subscription);
                object.publish();
            }
        } else if (query.evaluate(object)) {
            object.addSubscription(subscription.getSubscriptionId(), subscription);
            object.publish();
        }
    }
}
Also used : ObjectId(org.apache.qpid.qmf2.common.ObjectId) SchemaClass(org.apache.qpid.qmf2.common.SchemaClass) SchemaObjectClass(org.apache.qpid.qmf2.common.SchemaObjectClass) SchemaClassId(org.apache.qpid.qmf2.common.SchemaClassId) QmfQuery(org.apache.qpid.qmf2.common.QmfQuery) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 29 with QmfException

use of org.apache.qpid.qmf2.common.QmfException in project qpid by apache.

the class Console method cancelSubscription.

// end of refreshSubscription()
/**
 * Terminates the given subscription.
 *
 * @param subscriptionId the ID of the subscription to be cancelled
 */
public void cancelSubscription(final String subscriptionId) throws QmfException {
    if (subscriptionId == null) {
        throw new QmfException("Called cancelSubscription() with null subscriptionId");
    }
    SubscriptionManager subscription = _subscriptionById.get(subscriptionId);
    if (subscription == null) {
        throw new QmfException("Called cancelSubscription() with invalid subscriptionId");
    }
    String consoleHandle = subscription.getConsoleHandle();
    Agent agent = subscription.getAgent();
    if (!agent.isActive()) {
        throw new QmfException("Called cancelSubscription() with inactive agent");
    }
    String agentName = agent.getName();
    try {
        MapMessage request = _syncSession.createMapMessage();
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("method", "request");
        request.setStringProperty("qmf.opcode", "_subscribe_cancel_indication");
        request.setStringProperty("qpid.subject", agentName);
        request.setObject("_subscription_id", subscriptionId);
        synchronized (this) {
            if (!_subscriptionEmulationEnabled || !agentName.equals(_brokerAgentName)) {
                _requester.send(request);
            }
        }
        subscription.cancel();
    } catch (JMSException jmse) {
        _log.info("JMSException {} caught in cancelSubscription()", jmse.getMessage());
    }
}
Also used : MapMessage(javax.jms.MapMessage) JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Example 30 with QmfException

use of org.apache.qpid.qmf2.common.QmfException in project qpid by apache.

the class Console method removeConnection.

/**
 * Remove the AMQP connection from the console. Un-does the addConnection() operation, and releases
 * any Agents associated with the connection. All blocking methods are unblocked and given a failure
 * status. All outstanding asynchronous operations are cancelled without producing WorkItems.
 *
 * @param conn a javax.jms.Connection
 */
public void removeConnection(final Connection conn) throws QmfException {
    if (conn != _connection) {
        throw new QmfException("Attempt to delete unknown connection");
    }
    try {
        _timer.cancel();
        // Should we close() the connection here or just stop() it ???
        _connection.close();
    } catch (JMSException jmse) {
        throw new QmfException("Failed to remove connection, caught JMSException " + jmse.getMessage());
    }
    _connection = null;
}
Also used : JMSException(javax.jms.JMSException) QmfException(org.apache.qpid.qmf2.common.QmfException)

Aggregations

QmfException (org.apache.qpid.qmf2.common.QmfException)24 QmfData (org.apache.qpid.qmf2.common.QmfData)13 JMSException (javax.jms.JMSException)9 ObjectId (org.apache.qpid.qmf2.common.ObjectId)8 HashMap (java.util.HashMap)6 QmfAgentData (org.apache.qpid.qmf2.agent.QmfAgentData)6 QmfQuery (org.apache.qpid.qmf2.common.QmfQuery)5 SchemaObjectClass (org.apache.qpid.qmf2.common.SchemaObjectClass)5 QmfConsoleData (org.apache.qpid.qmf2.console.QmfConsoleData)5 Destination (javax.jms.Destination)4 MapMessage (javax.jms.MapMessage)4 Handle (org.apache.qpid.qmf2.common.Handle)4 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 SchemaClassId (org.apache.qpid.qmf2.common.SchemaClassId)3 SchemaMethod (org.apache.qpid.qmf2.common.SchemaMethod)3 SchemaProperty (org.apache.qpid.qmf2.common.SchemaProperty)3 IOException (java.io.IOException)2 Timer (java.util.Timer)2 Message (javax.jms.Message)2